Search code examples
java.netakkaakka.netakka-supervision

Akka: is a DeathPactException always a programming error?


I sporadically use Akka.NET in my projects, so I know the technology, can do stuff with it, but do not consider myself an expert.

Although I use .NET, the question can certainly be answered by someone familiar with Akka on the JVM.

Recently, I came across a DeathPactException while looking at an application's log file. The cause was an actor (actor A1) that watched another actor (A2), which it had started itself. A1, however, did not handle the Terminated message sent by A2. Which was caused by A2 actually stopping itself after performing its task. Thanks to a reasonably-defined supervisor hierarchy etc. the system itself worked just fine: A1 was simply restarted by its supervisor immediately.

Is there actually any scenario where an actor would .Watch() another actor and then ignore the Terminated message? Or is the infamous DeathPactException basically always a bug in application code, similar to a NullReferenceException/NullPointerException?


Solution

  • A DeathPactException does not necessarily indicate a programming error.

    Deliberately not handling a Terminated message is a way to stop all of an actor's children when any one of that actor's children stops normally. The sequence of events is the following:

    1. Actor parent watches all of its children, c1, c2, and c3, but does not handle the Terminated message.
    2. Child c2 stops normally.
    3. parent throws a DeathPactException, which causes parent to restart (this is the default supervisor strategy, which can be overridden in parent's supervisor/parent).
    4. parent's preRestart hook is called, which by default stops all of parent's children.

    Choosing not to handle a Terminated message is effectively a means to enact an all-for-one stopping strategy for non-exceptional (i.e., not in response to a failure) cases.