diff --git a/src/Akka.Hosting.Tests/RequiredActorSpecs.cs b/src/Akka.Hosting.Tests/RequiredActorSpecs.cs index 5b038f72..c52a9dd2 100644 --- a/src/Akka.Hosting.Tests/RequiredActorSpecs.cs +++ b/src/Akka.Hosting.Tests/RequiredActorSpecs.cs @@ -105,4 +105,37 @@ public async Task ShouldFailRetrieveRequiredActorWhenNotDefined() // assert shouldThrow.Should().Throw(); } + + [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(actor); + }); + }); + }) + .Build(); + + var myRequiredActor = host.Services.GetRequiredService>(); + + var task = myRequiredActor.GetAsync(); + task.IsCompletedSuccessfully.Should().BeFalse(); + + await host.StartAsync(); + _ = await task; + + // act + var cachedActorRef = await myRequiredActor.GetAsync(); + + // assert + cachedActorRef.Should().NotBeOfType(); + } } \ No newline at end of file diff --git a/src/Akka.Hosting/ActorRegistry.cs b/src/Akka.Hosting/ActorRegistry.cs index 4290263b..97e63210 100644 --- a/src/Akka.Hosting/ActorRegistry.cs +++ b/src/Akka.Hosting/ActorRegistry.cs @@ -82,13 +82,13 @@ public async Task GetAsync(CancellationToken cancellationToken = defa return _internalRef; // attempt 2 - synchronously check the registry (fast path) - if (_registry.TryGet(out _internalRef)) + if (_registry.TryGet(out var internalRef)) { - return _internalRef; + return _internalRef = internalRef; } // attempt 3 - wait for the actor to be registered - return await _registry.GetAsync(cancellationToken).ConfigureAwait(false); + return _internalRef = await _registry.GetAsync(cancellationToken).ConfigureAwait(false); } }