The application does not exit after the completion of the actions of the Actor and ActorSystem

I have a main program that creates an ActorSystem, an actor, and sends some messages to the actor. When messages are processed, I sent PoisonPill to kill the actor. Then I complete the acting system.

Inside the Actor, I urge Aweit to wait for the future to end. The problem I am facing is that the application does not exit, even if the actor terminates with PoisonPill and the ActorSystem shuts down.

def main(args: Array[String]): Unit = {

    val actorSystem = ActorSystem("sytem")
    val creationActor = actorSystem.actorOf(Props[MyActor], "MyActor")
    ...
    creationActor ! Message    //may be called multiple times
    creationActor ! PoisonPill
    ...
}

And the Actor code

class MyActor extends Actor {

  override def receive: Receive = {
    case Message => {
       ...
      Await.result(Dataset.create(datasetId), 30 seconds) 
      //Dataset.create returns a Future. Also this method uses an
      //ExecutionContext of its own.
      ...
    }
  }

  override def postStop() = {
    context.system.shutdown()
  }
}

If I comment on the part of Await.result, the program will exit.

EDIT:
Looks like I found the root cause.

ExecutionContext, Dataset.create(...), . Dataset.create(...), Futures, .

, Dataset.create(), : implicit val defaultContext = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(100))

- , .

2: Await

val future = BQDataset.create(datasetId)
future onComplete {
  case Success(d) => ...
  case Failure(e) => ...
}

. @cem-catikkas, ExecutionContext, BQDataset.create, . jstack "pool-1-thread-1" prio=5 tid=0x00007ff49aa1e800 nid=0x4e03 waiting

+4
2

Dataset.create(…), ExecutionContext . shutdown() close() .

- , ExecutionContext.shutdown(), . .

+2

. - . , CostlyOp. , , .

+2

Source: https://habr.com/ru/post/1570413/


All Articles