-
Couldn't load subscription status.
- Fork 1.4k
Fix race condition when interrupting applications #9827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix race condition when interrupting applications #9827
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice if it was possible to test, but...
| fiber <- workflow.interruptible.exitWith { exit0 => | ||
| val exitCode = if (exit0.isSuccess) ExitCode.success else ExitCode.failure | ||
| // If we're shutting down due to an external signal, the shutdown hook will fulfill the promise | ||
| // Otherwise it means we're shutting down due to normal completion and we need to fulfill the promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover promise comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
| "Check the logs for more details and consider overriding `Runtime.reportFatal` to capture context." | ||
| ) | ||
| } else { | ||
| try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this still need to be in a try?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an extremely low chance (I can't reproduce, but theoretically it might be possible), that the submission of the task to the executor is rejected (due to shutdown) and throws a RejectedExecutionException.
As I said, I'm not even sure if that's possible but just wanted to be extra safe just in case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment to clarify
/fixes #9807
The main issue here is that in cases where the shutdown hooks were taking some time to process, the
fiber.joinwould win the race and the thrown Exception (in this case the interruption exception) would be thrown in the main thread. The fix is to make sure that all errors inworkfloware caught (by usingexitWithinstead ofonExit, and gracefully close the application withexit(exitCode)after we join the forked fiber.I also made some cleanups in the logic so that it's easier to follow, since the code previously relied on unsafely running effects unnecessarily and was harder to follow terms of what was executed in what sequence