What is the correct way to ensure that Guardian actor restarts?

I have read this doc Supervision and Monitoring • Akka Documentation, but I am unclear how to make sure my guardian actor restarts with akka typed if a child actor throws an exception, when my top level actor is wrapped in an ActorSystem:

protected lazy val supervisor: ActorSystem[MySupervisor.Query] = {
    val beh = MySupervisor(...)
    ActorSystem(beh, mySupervisorName)
  }

I tried doing this

  protected lazy val supervisor: ActorSystem[MySupervisor.Query] = {
    val beh = MySupervisor(...)
    Behaviors.supervise(beh).onFailure[Throwable](SupervisorStrategy.restart)

    ActorSystem(beh, mySupervisorName)
  }

but I am still getting [ERROR] 10:57:39.763 [LocalActorRefProvider] - guardian failed, shutting down system and then on subsequent requests

java.util.concurrent.TimeoutException: Recipient[akka://MySupervisor-1719856642364_LYUL3BZO] had already been terminated.

Do I need to create a higher level actor to supervise my top-level actor?

Thanks in advance.

1 Like

because your behavior didn’t supervised.

  protected lazy val supervisor: ActorSystem[MySupervisor.Query] = {
    val beh = MySupervisor(...)
--  Behaviors.supervise(beh).onFailure[Throwable](SupervisorStrategy.restart)
++  beh = Behaviors.supervise(beh).onFailure[Throwable](SupervisorStrategy.restart)
    ActorSystem(beh, mySupervisorName)
  }
1 Like