As I know, all operations in Akka.Net are asynchronous, and Context.Stop() simply sends a Stop message to the actor. That means, actor will be alive for some time before it completely shuts down.
And if I'll call Context.Child() right after Context.Stop() with the name of the actor I just stopped, I will get the same actor.
Here is the example code
var actor = context.Child(actorName);
if (actor.Equals(ActorRefs.Nobody))
{
actor = CreateNewActor();
}
Context.Stop(actor)
actor = context.Child(actorName);
// what do we get here, same actor or ActorRefs.Nobody ?
My application creates actors to process events from terminals. Each time new terminal connected, I create new actor by calling Context.Child() using terminal name. When terminal disconnects, I stop the actor.
Problem is that some times I receive Connect message right after Disconnect for same terminal, and as result I get actor that's going be stopped. Is there any way to check that actor received Stop message and will be stopped soon?
I decide to finish up with handling
Terminatedmessages.After receiving
Disconnectmessage and stopping an actor, I keep it's name inActorsToBeStoppedHashSet, and before creating a new actor on receivingConnectmessage, I check if it exists there. If so, I keep thisConnectmessage in dictionary with actor name as key andConnectmessage as value and process it after receiving Terminated message for corresponding actor.Something like this:
EDIT
Sad thing here is that I can't write test for it, because
Akka.TestKitdoesn't allow me to createTestActorwith same name even after it was stopped:Or, maybe it wasn't stopped after
ExpectTerminated, but anyway I don't know how to wait it's termination.