Stopping the cluster config backfill runner after it was disposed does not throw #373

Closed
opened 2026-11-12 00:53:37 -05:00 by claude · 0 comments
Owner

What happens

Ninlil.Web.Tests.CurrentPasswordRateLimitTests.ChangeMyPassword_PastTheLimit_AnswersTooManyRequestsEvenForTheCorrectPassword failed once on the CI runner (pull-request run 1445, job Test .NET, 1 failed of 2353). The test's assertions passed; the failure is thrown while its second host is disposed at the end of the test (using WebApplicationFactory<Program> host = CreateHost();, line 126):

System.ObjectDisposedException : The CancellationTokenSource has been disposed.
   at Ninlil.Dns.Server.Cluster.Backfill.ClusterConfigBackfillRunner.StopAsync(CancellationToken stoppingToken)
   at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](...)
   at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.DisposeAsync()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.Dispose()

What is known

  • ClusterConfigBackfillRunner is an IHostedService and IAsyncDisposable singleton. DisposeAsync calls StopAsync and then disposes _shutdown. StopAsync calls _shutdown.CancelAsync() with no check, so a StopAsync that runs after DisposeAsync throws.
  • So in this run the container disposed the runner before the host called StopAsync on it. Which path does that is not known yet (a host that stops itself, a second StopAsync, or the order inside WebApplicationFactory.DisposeAsync).
  • This is product code: a host whose services are disposed before a late StopAsync would throw the same way at shutdown.

Wanted

  • Find out how StopAsync can run after DisposeAsync here and say so in the pull request.
  • StopAsync after DisposeAsync does nothing and does not throw; a pass can still not start after either.
  • Check the other hosted services that own a CancellationTokenSource and call StopAsync from their dispose for the same shape, and fix those that have it.
  • A regression test that fails before the fix (stop after dispose on the runner itself is enough).

Acceptance

  • The new test is red before the fix and green after it.
  • Ninlil.Dns.Server.Cluster.Tests and Ninlil.Web.Tests pass.
## What happens `Ninlil.Web.Tests.CurrentPasswordRateLimitTests.ChangeMyPassword_PastTheLimit_AnswersTooManyRequestsEvenForTheCorrectPassword` failed once on the CI runner (pull-request run 1445, job Test .NET, 1 failed of 2353). The test's assertions passed; the failure is thrown while its second host is disposed at the end of the test (`using WebApplicationFactory<Program> host = CreateHost();`, line 126): ``` System.ObjectDisposedException : The CancellationTokenSource has been disposed. at Ninlil.Dns.Server.Cluster.Backfill.ClusterConfigBackfillRunner.StopAsync(CancellationToken stoppingToken) at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](...) at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.DisposeAsync() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.Dispose() ``` ## What is known - `ClusterConfigBackfillRunner` is an `IHostedService` and `IAsyncDisposable` singleton. `DisposeAsync` calls `StopAsync` and then disposes `_shutdown`. `StopAsync` calls `_shutdown.CancelAsync()` with no check, so a `StopAsync` that runs after `DisposeAsync` throws. - So in this run the container disposed the runner before the host called `StopAsync` on it. Which path does that is not known yet (a host that stops itself, a second `StopAsync`, or the order inside `WebApplicationFactory.DisposeAsync`). - This is product code: a host whose services are disposed before a late `StopAsync` would throw the same way at shutdown. ## Wanted - Find out how `StopAsync` can run after `DisposeAsync` here and say so in the pull request. - `StopAsync` after `DisposeAsync` does nothing and does not throw; a pass can still not start after either. - Check the other hosted services that own a `CancellationTokenSource` and call `StopAsync` from their dispose for the same shape, and fix those that have it. - A regression test that fails before the fix (stop after dispose on the runner itself is enough). ## Acceptance - The new test is red before the fix and green after it. - `Ninlil.Dns.Server.Cluster.Tests` and `Ninlil.Web.Tests` pass.
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lantea-one/ninlil#373
No description provided.