Divij Vaidya created KAFKA-15205: ------------------------------------ Summary: ShutdownableThread Key: KAFKA-15205 URL: https://issues.apache.org/jira/browse/KAFKA-15205 Project: Kafka Issue Type: Bug Affects Versions: 3.4.1, 3.3.2, 3.6.0, 3.5.1 Reporter: Divij Vaidya Fix For: 3.6.0
In Shutdownable thread, during close, we call: initiateShutdown() -> which may interrupt the thread if isInterruptible is set to true during construction. After that, we wait for proper shutdown using awaitShutdown() which in-turn calls CountdownLatch#await(). On interruption, which could be caused by initiateShutdown() earlier, await() throws an InterruptedExeception. Hence, awaitShutdown() is going to exit by throwing an interrupted exception. The sequence to reproduce this will be as follows: App-thread: Name of application thread which spawns and closes Shutdownable thread Shut-thread: Name of the shutdownable thread. 1. App-thread calls ShutThread.initiateShutdown() 2. ShutThread.interrupt() is called. It informs the VM to interrupt but the actual interrupt will be async. initiateShutdown() from step 1 returns. 3. App-thread calls ShutThread.awaitShutdown() 4. App-thread waits on shutdownComplete.await() i.e. on CountdownLatch#await 5. VM decides to interrupt App-thread and there is a race condition now. Race condition: Condition 1: Shut-thread.doWork() gets interrupted exception, and decrements the CountdownLatch Condition 2: App-thread waiting on Shut-thread.shutdownComplete.await() throws an interruptedException as per the contract [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html#await--] *Solution* In ShutDownableThread#awaitShutdown(), when calling await() we should catch InterruptedException and eat it up (do nothing), if the thread has isInterruptable set to true. -- This message was sent by Atlassian Jira (v8.20.10#820010)