Skip to content
Merged
3 changes: 2 additions & 1 deletion core/src/main/scala/kafka/utils/ShutdownableThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ abstract class ShutdownableThread(val name: String, val isInterruptible: Boolean
* After calling initiateShutdown(), use this API to wait until the shutdown is complete
*/
def awaitShutdown(): Unit = {
shutdownComplete.await()
if (this.isAlive)
shutdownComplete.await()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 ways to shut down a ShutdownableThread: (1) Just call shutdown(), which blocks until the thread completes. (2) First call initiateShutdown() and then call awaitShutdown(). The reason for this style is to allow the caller to add additional logic between the 2 calls. For example, if the thread can block in a selector, one can wake up the selector between the 2 calls. If the thread wasn't started or died unexpectedly, it's reasonable not to block the shutdown in either case.

Also, just calling awaitShutdown() w/o calling initiateShutdown() first is unexpected and not supported. To protect against this, perhaps we can add a check to make sure that shutdownInitiated is already 0 when awaitShutdown() is called. Otherwise, we can throw an IllegalStateException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I believe we can cover both cases here if we check in awaitShutdown() that the thread has been started before blocking? Perhaps I'm misunderstanding though.

I'll add a check that shutdownInitiated == 0.

info("Shutdown completed")
}

Expand Down