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
Handle Interruption After Fatal Error In FiberContext #4812
Conversation
The only issue here is that during fatal exception, we cannot guarantee success of the Another possible solution is to capture the fact that we are dealing with a fatal error somewhere, and in that event, do not try to interrupt the fiber in the shutdown hook. |
Maybe a variant of that is we just set a flag in |
Well, it's a descendant fiber that had the fatal error. The top-level fiber doesn't have that problem. The other thing we could decide to do is just "try" to do cleanup and the |
Ah, actually, your suggestion might work: although it assumes the top-level fiber successfully interrupts everything, all the way down to the fiber that had the problem. Basically, it's the whole "interrupt root and have your app shut down gracefully" that I question will actually work reliably in a real-life fatal error: because running all those effects, waiting for interruption, etc., involves a ton of allocation and thread pool work, all of which are not guaranteed to happen in the presence of a fatal error. |
Yes I totally agree. I feel like there is a risk that we optimize for these examples where a fatal exception is thrown but we are not actually in a fatal state and if we are in a fatal state nothing we do may matter much. |
All right, well, what if If it were set, it could also print out some kind of message using
|
Yes, I think that makes sense. I will revise accordingly. |
Resolves #4810.
The cause of the issue is that when we attempt to interrupt the main fiber in the shutdown hook added by
App
we suspend indefinitely.My analysis is that when we call
interrupt
it invokeskill0
inFiberContext
, which if the fiber is still in anExecuting
state adds an interrupted cause and then callsawait
, which if the fiber is in anExecuting
state registers itself as an observer of the fiber and waits for the fiber to be done. Normally this works fine because adding the interrupted cause leads to the fiber entering aDone
state and notifying the observers, allowing the interruption to successfully return.However, when a fatal exception is thrown we stop interpreting further continuations in the main run loop for that fiber, so we never get to a
Done
state and never notify the observers of that fiber, hence the interruption hanging forever.To address that, this PR implements a new
fatal
method that sets the state toDone
and notifies any pending observers at that point.