Skip to content

Commit

Permalink
fix: handle interrupt when shutting down executors
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jul 8, 2021
1 parent 66dd1e2 commit 46cfdfd
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,20 @@ internal class BackgroundTaskService(
// shutdown the IO executor last.
errorExecutor.shutdown()
sessionExecutor.shutdown()
errorExecutor.awaitTermination(SHUTDOWN_WAIT_MS, TimeUnit.MILLISECONDS)
sessionExecutor.awaitTermination(SHUTDOWN_WAIT_MS, TimeUnit.MILLISECONDS)

errorExecutor.awaitTerminationSafe()
sessionExecutor.awaitTerminationSafe()

// shutdown the IO executor last, waiting for any existing tasks to complete
ioExecutor.shutdown()
ioExecutor.awaitTermination(SHUTDOWN_WAIT_MS, TimeUnit.MILLISECONDS)
ioExecutor.awaitTerminationSafe()
}

private fun ThreadPoolExecutor.awaitTerminationSafe() {
try {
awaitTermination(SHUTDOWN_WAIT_MS, TimeUnit.MILLISECONDS)
} catch (ignored: InterruptedException) {
// ignore interrupted exception as the JVM is shutting down
}
}
}

0 comments on commit 46cfdfd

Please sign in to comment.