Coordinated Shutdown inconsistently ignored when automatic JVM hook set to be true

Hi there,

I have akka.coordinated-shutdown.run-by-jvm-shutdown-hook set true (it’s default).
Akka Streams version 2.5.22.

For the following code:

    CoordinatedShutdown(system).addTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, "graceful-kill")(() => {
      killSwitch.shutdown()
      Future(done())
    })

    CoordinatedShutdown(system).addTask(CoordinatedShutdown.PhaseBeforeActorSystemTerminate, "wait-streams-finished")(() => {
      streams.foreach(Await.ready(_, 1.minutes))
      Future(done())
    })

    system.registerOnTermination(db.close())

    sys.addShutdownHook {
      logger.info("Terminating Actor system...")
      Await.result(system.whenTerminated, Duration.Inf)
      logger.info("Actor system terminated")
    }

sometimes the app terminates with both graceful-kill and wait-streams-finished phases executed, and sometimes with only graceful-kill, sometimes with neither. despite the hook system.registerOnTermination(db.close()) is always executed and awaited.

What am I doing wrong?

Thank you very much.

I’d recommend to not use Await there. The list of futures can be turned into a single Future that that can be returned from the task. Future.sequence

sys.addShutdownHook adds a jvm shutdown hook. CoordinatedShutdown adds another. Order of execution of such hooks are not deterministic. Not sure what the purpose of your hook would be?

hey Patrik,

sys.addShutdownHook should prevent JVM from turning off unless actor system is terminated.

system.whenTerminated returns Future[Terminated], looks like no need for Future.sequence as Terminated is not an heir of Seq.

but that is already handled by the jvm hook added by CoordinatedShutdown

Future.sequence was instead of the streams.foreach Await