Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister temp actor if ask operation got canceled. #2381

Merged
merged 9 commits into from
Dec 3, 2016
20 changes: 20 additions & 0 deletions src/core/Akka.Tests/Actor/AskTimeoutSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,25 @@ public async Task Ask_should_honor_config_specified_timeout()
}
}

[Fact]
public async Task TimedOut_ask_should_remove_temp_actor()
{
var actor = Sys.ActorOf<SleepyActor>();

var actorCell = actor as ActorRefWithCell;
var container = actorCell.Provider.TempContainer as VirtualPathContainer;
try
{
await actor.Ask<string>("should time out");
}
catch (Exception)
{
var childCounter = 0;
container.ForEachChild(x => childCounter++);
Assert.True(childCounter==0,"Number of children in temp container should be 0.");
}

}

}
}
11 changes: 8 additions & 3 deletions src/core/Akka/Actor/Futures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ private static Task<object> Ask(ICanTell self, object message, IActorRefProvider

timeout = timeout ?? provider.Settings.AskTimeout;

//create a new tempcontainer path
ActorPath path = provider.TempPath();

if (timeout != Timeout.InfiniteTimeSpan && timeout.Value > default(TimeSpan))
{
timeoutCancellation = new CancellationTokenSource();
timeoutCancellation.Token.Register(() => result.TrySetCanceled());
timeoutCancellation.Token.Register(() =>
{
result.TrySetCanceled();
provider.UnregisterTempActor(path);
});
timeoutCancellation.CancelAfter(timeout.Value);
}

//create a new tempcontainer path
ActorPath path = provider.TempPath();
//callback to unregister from tempcontainer
Action unregister =
() =>
Expand Down