suprnation / cats-actors

Cats Actors framework for building apps which are reactive. Cats actors uses a conceptual actor model as a higher level abstraction for concurrency.
Apache License 2.0
104 stars 9 forks source link

fix: Added error escalation to the actor pre-start. #17

Closed Mitrug closed 2 months ago

Mitrug commented 2 months ago

Description

When an error is raised in the actor pre-start, the supervision is not used and therefore the parent actor is never notified that the child actor failed to start. The exception is simply returned to the caller of the actorOf. In the example below, this behaviour would cause the application to "get stuck" waiting for the actor system to terminate. Since the parent actor, in this case the user guardian, is never notified of such an issue, the system will never terminate.

def actor: ReplyingActor[IO, String, Any] =
  new ReplyingActor[IO, String, Any] {

    override def preStart: IO[Unit] =
      IO.raiseError(new RuntimeException("Oops we failed"))
  }

(for {
  _ <- Resource.onFinalize {
    IO.delay {
      Runtime.getRuntime.halt(-1)
    }
  }
  actorSystem <- ActorSystem[IO]()
  actor <- actorSystem
    .replyingActorOf(
      actor,
      "ping"
    )
    .toResource
    .onFinalize(actorSystem.waitForTermination)

} yield (actor, actorSystem.terminate())).allocated.unsafeRunSync()