Skip to content

Commit

Permalink
Avoid caching nobody when actor doesn't exist in registry (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNorris153 authored Oct 10, 2024
1 parent 00e4b4c commit 35bedd0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/Akka.Hosting.Tests/RequiredActorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,37 @@ public async Task ShouldFailRetrieveRequiredActorWhenNotDefined()
// assert
shouldThrow.Should().Throw<MissingActorRegistryEntryException>();
}

[Fact]
public async Task ShouldNotCacheNobodyAfterWhenWaitedForRegistration()
{
// arrange
using var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddAkka("MySys", (builder, _) =>
{
builder.WithActors((system, registry) =>
{
var actor = system.ActorOf(Props.Create(() => new MyActorType()), "myactor");
registry.Register<MyActorType>(actor);
});
});
})
.Build();

var myRequiredActor = host.Services.GetRequiredService<IRequiredActor<MyActorType>>();

var task = myRequiredActor.GetAsync();
task.IsCompletedSuccessfully.Should().BeFalse();

await host.StartAsync();
_ = await task;

// act
var cachedActorRef = await myRequiredActor.GetAsync();

// assert
cachedActorRef.Should().NotBeOfType<Nobody>();
}
}
6 changes: 3 additions & 3 deletions src/Akka.Hosting/ActorRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public async Task<IActorRef> GetAsync(CancellationToken cancellationToken = defa
return _internalRef;

// attempt 2 - synchronously check the registry (fast path)
if (_registry.TryGet<TActor>(out _internalRef))
if (_registry.TryGet<TActor>(out var internalRef))
{
return _internalRef;
return _internalRef = internalRef;
}

// attempt 3 - wait for the actor to be registered
return await _registry.GetAsync<TActor>(cancellationToken).ConfigureAwait(false);
return _internalRef = await _registry.GetAsync<TActor>(cancellationToken).ConfigureAwait(false);
}
}

Expand Down

0 comments on commit 35bedd0

Please sign in to comment.