Search code examples
javaakkaakka-actor

Akka: how to shut down an actor that it's not terminating


(disclaimer: I'm less than a beginner with akka)

Suppose I have an actor calling a method that never terminates (it's an extreme example, you can think about calling a method that has the chance to terminate in a long time or never).

For example (Java)

public static class InfiniteLoop{
    public static int neverReturns(){
        int x = 0;
        while(true){
        int++;
        }
        return x;
    }
}

Now if, while processing a message, an actor calls

InfiniteLoop.neverReturns()

the actor will never terminate.

Is there a way to kill it while it is still processing a message? If yes, will the loop continue in background?

(what I'm trying to understand is if there is a way to recover from a "infinite loop"-style fault in an akka system)


Solution

  • There is no way to implement such thing in Akka. All methods to stop an actor rely on sending a message to the actor. If your actor is stuck processing the current message because of the loop, it will never process the message telling it to stop. This is the base of the akka actor model, messages in the mailbox are processed in order, and I don't think you can find a way around that. Check this article for your options on stopping/killing an actor: https://petabridge.com/blog/how-to-stop-an-actor-akkadotnet/. You will see how the semantics of the stopping change for each method, but they all start by sending a message to the actor.

    It would be nice to know why you need such a thing, cause maybe your base requirement can be implemented in a more akka-ish way. For instance, the potentially blocking action could be wrapped in a future if it's okay for the actor to pass to the next message.