CoordinatedShutdown seems not being executed when an error is thrown in the user guardian actor

Given this code. I can’t see the print in the console logs. Also, I enabled the debug log and I can’t see any logs related to CoordinatedShutdown

object Example extends App {

  ActorSystem[Nothing](
    Behaviors.setup[Nothing] { context =>
      CoordinatedShutdown(context.system).addTask(PhaseBeforeActorSystemTerminate, "stop-metrics") { () =>
        println("Task!")
        Future.successful(Done)
      }
      throw new Exception("Exception!")
      Behaviors.empty
    },
    "sys"
  )

}

I was expecting that it should run. am I doing anything wrong?

If the guardian crashes on start, the system is immediately terminated, so no actor system to keep running the coordinated shutdown anymore. On the other hand I guess there was also not really any time for other things to start to be coordinatedly shut down. Might be worth a mention in the docs.

When orderly stopping of the guardian, there is special logic in place to intercept stop and let the coordinated shutdown complete first.

Thank for your reply.

I guess then, the recommended approach could be to stop the guardian actor if an error occurs while it starts up ?

If you do stuff that might fail during bootstrap of the guardian, and also start things that needs coordinated shutdown, it can make sense to try-catch-stop.

For most scenarios I think the actor system and JVM immediately terminating is likely good enough. In general coordinated shutdown will be most important for things that needs some graceful shutting down, sharding waiting for stopping entities, Akka HTTP/gRPC giving in flight requests time to complete befor closing connections, cluster leaving instead of relying on SBR to kick a node out, and those will likely not have happened before the guardian has completed starting and the node is passing a ready check/has seen some requests.

Thank you so much for the info.