Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not always reuse Actor's name after graceful stop

Tags:

scala

akka

I studied Akka's "Graceful Stop" for actors and created a small test app to test it.

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

Every now and then the following exception appears:

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

Why is that? How can I fix the app so that InvalidActorNameExceptions would not appear every now and then?

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 and with Java 8 and Akka 2.4.0.

like image 923
Simo Avatar asked Nov 26 '15 12:11

Simo


1 Answers

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

I just followed the link and found red alert here

Warning

Keep in mind that an actor stopping and its name being deregistered are separate events which happen asynchronously from each other. Therefore it may be that you will find the name still in use after gracefulStop() returned. In order to guarantee proper deregistration, only reuse names from within a supervisor you control and only in response to a Terminated message, i.e. not for top-level actors.

So make supervisor actor and start namesake only on Terminated reception

like image 91
Odomontois Avatar answered Sep 20 '22 23:09

Odomontois