Copilot commented on code in PR #24240:
URL: https://github.com/apache/kafka/pull/24240#discussion_r4586072255


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStreamThread.java:
##########
@@ -365,28 +370,36 @@ public void run() {
             );
             this.streamsUncaughtExceptionHandler.accept(e);
         } catch (final Exception e) {
+            interrupted = Thread.interrupted();
             log.error("Error happened while maintaining global state store. 
The streams application or client will now close to ERROR.", e);
             this.streamsUncaughtExceptionHandler.accept(e);
         } finally {
-            // set the state to pending shutdown first as it may be called due 
to error;
-            // its state may already be PENDING_SHUTDOWN so it will return 
false but we
-            // intentionally do not check the returned flag
-            setState(State.PENDING_SHUTDOWN);
+            interrupted |= Thread.interrupted();

Review Comment:
   Using `|=` with booleans is correct here, but it’s easy to misread as a 
bitwise operation and can obscure the intent that `Thread.interrupted()` must 
always be invoked to clear the flag. Consider rewriting this in a more 
self-explanatory form that still forces evaluation (eg, assign from 
`Thread.interrupted()` first, then combine with the existing value) to make the 
clearing/recording behavior obvious.



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStreamThreadTest.java:
##########
@@ -466,17 +539,56 @@ private HashMap<String, Object> getStreamProperties() {
     }
 
     private void initializeConsumer() {
-        mockConsumer.updatePartitions(
+        initializeConsumer(mockConsumer);
+    }
+
+    private void initializeConsumer(final MockConsumer<byte[], byte[]> 
consumer) {
+        consumer.updatePartitions(
             GLOBAL_STORE_TOPIC_NAME,
             Collections.singletonList(new PartitionInfo(
                 GLOBAL_STORE_TOPIC_NAME,
                 0,
                 null,
                 new Node[0],
                 new Node[0])));
-        
mockConsumer.updateBeginningOffsets(Collections.singletonMap(topicPartition, 
0L));
-        mockConsumer.updateEndOffsets(Collections.singletonMap(topicPartition, 
0L));
-        mockConsumer.assign(Collections.singleton(topicPartition));
+        
consumer.updateBeginningOffsets(Collections.singletonMap(topicPartition, 0L));
+        consumer.updateEndOffsets(Collections.singletonMap(topicPartition, 
0L));
+        consumer.assign(Collections.singleton(topicPartition));
+    }
+
+    private static void interruptOnNextPoll(final MockConsumer<byte[], byte[]> 
consumer) {
+        // the exception is created on the polling thread, since its 
constructor sets the interrupt status
+        consumer.schedulePollTask(() -> {
+            throw new InterruptException(new InterruptedException());
+        });
+    }
+
+    private MockConsumer<byte[], byte[]> 
consumerRecordingInterruptStatusOnClose(final AtomicBoolean interruptedOnClose) 
{
+        final MockConsumer<byte[], byte[]> consumer = new 
MockConsumer<>(AutoOffsetResetStrategy.NONE.name()) {
+            @Override
+            public void close() {
+                interruptedOnClose.set(Thread.currentThread().isInterrupted());
+                super.close();
+            }
+        };
+        initializeConsumer(consumer);
+        return consumer;
+    }
+
+    private GlobalStreamThread globalStreamThread(final MockConsumer<byte[], 
byte[]> consumer) {
+        return new GlobalStreamThread(

Review Comment:
   The helper method name `globalStreamThread(...)` is identical to the 
instance field `globalStreamThread`, which makes call sites like 
`globalStreamThread = globalStreamThread(consumer);` ambiguous to read. Rename 
the helper to something like `newGlobalStreamThread(...)` / 
`createGlobalStreamThread(...)` to avoid name collision and improve clarity.



##########
streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStreamThreadTest.java:
##########
@@ -325,6 +328,76 @@ public Set<TopicPartition> partitions() {
         assertFalse(new File(baseDirectoryName + File.separator + "testAppId" 
+ File.separator + "global").exists());
     }
 
+    @Test
+    public void 
shouldClearInterruptStatusBeforeInvokingUncaughtExceptionHandler() {
+        final AtomicBoolean interruptedInHandler = new AtomicBoolean(true);
+        initializeConsumer();
+        interruptOnNextPoll(mockConsumer);
+        globalStreamThread.setUncaughtExceptionHandler(
+            e -> 
interruptedInHandler.set(Thread.currentThread().isInterrupted())
+        );
+
+        try {
+            globalStreamThread.run();
+
+            assertFalse(interruptedInHandler.get());
+        } finally {
+            assertTrue(Thread.interrupted());
+        }
+    }
+
+    @Test
+    public void 
shouldClearInterruptStatusDuringShutdownAndRestoreItAfterwards() {
+        final AtomicBoolean interruptedOnClose = new AtomicBoolean(true);
+        final MockConsumer<byte[], byte[]> consumer = 
consumerRecordingInterruptStatusOnClose(interruptedOnClose);
+        interruptOnNextPoll(consumer);
+        globalStreamThread = globalStreamThread(consumer);

Review Comment:
   The helper method name `globalStreamThread(...)` is identical to the 
instance field `globalStreamThread`, which makes call sites like 
`globalStreamThread = globalStreamThread(consumer);` ambiguous to read. Rename 
the helper to something like `newGlobalStreamThread(...)` / 
`createGlobalStreamThread(...)` to avoid name collision and improve clarity.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to