-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Actor termination during ReceiveAsync may cause unexpected behavior #7485
Comments
So just as an explanation for why Akka.NET allows this - it's because users regularly hung their Async Cleanup Without Custom ShutdownsIf you're worried about resource leaks stemming from an actor, the right way to do this is to probably:
The trouble you're going to have with resource clean-up et al is that if the So the other option you have to addressing this is to run your I have some videos on the latter here: Design ChangesI appreciate the problems this caused for you @nkreipke or the time and effort you took to report the bug. I'm not sure we're going to be able to automatically solve this though, because when faced with the choice between:
Option 1 basically left us with all of the problems of option 2 plus some many additional ones, like cluster nodes never being able to finish termination. Actors have to be able to shutdown on short-notice. |
Appreciate the in-depth reply. It certainly is a difficult problem to solve and I get why it is done that way. My issue with it is that it's just that easy for developers to shoot themselves in the foot with this, especially because vulnerable code looks completely fine and the available workarounds are non-trivial. The |
I wonder if it's possible for us to offer a "last chance execution" on the |
Hi,
I've run into the same issue that was discussed in #3259. The issue in that ticket boils down to how task scheduling works in Akka, which is by sending instances of
ActorTaskSchedulerMessage
to itself to execute task continuations. The actor may handle system messages inbetween these messages including theTerminate
message, which causes aReceiveAsync
message handler to stop executing at anyawait
point.In the original ticket discussion this was explained as a feature, as it would terminate actors that are waiting on a continuation which will never complete.
I'm creating this ticket to re-open a discussion about this, as there is an important consequence of this not mentioned in the original discussion, which is
using
andtry-finally
statements inasync
methods.Due to how the compiler generates the async state machine,
file
will not get disposed if the actor gets terminated duringWriteAsync
. Similarly, afinally
block is not executed if a task is awaited in the correspondingtry
block. This can lead to all sorts of problems, including memory leaks. This behavior is unexpected and breaks the guarantees ofusing
andtry-finally
so it leads to bugs which are almost impossible to diagnose unless the developer knows about it.It is also not as difficult to reproduce as initially assumed ("just avoid using
Context.Stop
and send aPoisonPill
instead"): While aPoisonPill
is not a system message and therefore would not trigger this behavior, an actor receiving aPoisonPill
will sendTerminate
to all its children. This is how I've fallen victim to this in production.There are ways around this by sending custom messages instead of
PoisonPill
and manually terminating children, but I bet most users of Akka don't know about that. While it is probably impossible to fix Akka 1 without breaking other stuff, it might be worth considering adding a warning to the documentation or even discouraging usingReceiveAsync
withusing
ortry-finally
altogether.I've provided a repro for this here: https://github.com/nkreipke/AkkaUsingTest
The text was updated successfully, but these errors were encountered: