It is not always possible to reuse an actorโ€™s name after a graceful stop

I studied the Accu Graceful Stop for actors and created a small test app for testing.

The app shows that "Graceful Stop" is mentioned in http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Graceful_Stop does not always guarantee that you can reuse the actor's gracefully stopped name.

The following exception appears from time to time:

Exception in thread "main" akka.actor.InvalidActorNameException: actor name [DummyActor] is not unique! 

Why? How can I fix the application so that InvalidActorNameExceptions do not appear every time?

Here is the code:

main class ...

 import akka.actor._ import akka.pattern.gracefulStop import DummyActor.Stop import DummyActor import scala.concurrent.duration._ import scala.concurrent.{Await, Future} object AkkaTest { def main(args: Array[String]) { val actorsystem = ActorSystem("testSystem") 1 to 5 foreach { _ => // Create an actor with name: DummyActor val dummyActor = actorsystem.actorOf(Props[DummyActor], "DummyActor") // Gracefully stop the DummyActor val stopped: Future[Boolean] = gracefulStop(dummyActor, 5 seconds, Stop) Await.result(stopped, 6 seconds) } val terminated: Future[Terminated] = actorsystem.terminate() Await.result(terminated, 10 seconds) System.out.println("Finished successfully. Try again, eventually it will fail. You can also increase the amount of loops.") } } 

and the actor ...

 import akka.actor.Actor import DummyActor.Stop object DummyActor { case object Stop } class DummyActor extends Actor { override def receive: Receive = { case Stop => context stop self } } 

I have Scala 2.11.7 with both Java 8 and Akka 2.4.0.

+5
source share
1 answer

Commend has awfull formatting, so I'll copy it here

I just followed the link and found a red warning here.

Attention

Keep in mind that the actor stops and his name is canceled by registration are separate events that occur asynchronously from each other. Therefore, you may find a name that is still used after gracefulStop() back. To guarantee deregistration, only reuse of names from the head controlling you and only in response to the Terminated message, that is, not for top-level actors.

So, make an actor-supervisor and start the namesake only when taking Terminated

+7
source

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


All Articles