fix: prevent restartable outbox relay cancellation from faulting the host on .NET 10#310
Merged
Merged
Conversation
Owner
Author
|
Successfully created backport PR for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WebApi.Tests.ExternalWeatherClientTestsintermittently failed withObjectDisposedExceptionon the shared factory'sIServiceProviderafter the host loggedHost[9] BackgroundService failed: TaskCanceledExceptionwith no user frames (latest: CI run 29039323993 attempt 1). Root cause: on .NET 10,BackgroundService.StartAsyncschedulesExecuteAsyncviaTask.Run(..., stoppingToken). When the test harness's per-test reset stops the restartable outbox relay before the thread pool has dequeued that delegate, the host-observed first execute task transitions straight to canceled withoutExecuteAsyncever running — and because the application is not shutting down, the defaultBackgroundServiceExceptionBehavior.StopHoststops the host mid-class. No in-method cancellation handling can prevent this, which is why the earlier relay-gate and consumer-retry hardening did not close the flake.OutboxBackgroundServiceno longer inheritsBackgroundService; it implementsIRestartableHostedService+IDisposableand owns its lifecycle: the execute task is scheduled with an ungatedTask.Run, cancellation is observed inside the loop and always ends in a graceful return, the host never awaits the task, and a genuine fault still stops the application viaIHostApplicationLifetime.StopApplication()(log: critical), matching theBackgroundServicedefault.OutboxRetentionBackgroundService/InboxRetentionBackgroundService: a foreignOperationCanceledExceptionthrown by the store while the application is running (for example a client library surfacing a timeout as a cancellation) is now logged and retried on the next tick instead of escaping and faulting the host; graceful shutdown ends the execute task inRanToCompletion.IRestartableHostedServiceXML docs and theVulthil.Extensions.HostingREADME now recommend implementingIHostedServicedirectly for restartable services and explain the .NET 10 hazard.No public API changes (all touched service types are internal; the interface change is documentation only).
Verification
StartingWithAnAlreadyCanceledTokenCompletesTheExecuteTaskGracefullydeterministically reproduces the pre-execution cancellation endpoint;StoppingBeforeTheRelayLoopHasRunCompletesGracefullymirrors the harness stop sequence; restart-cycle and fault-stops-application tests pin the new lifecycle; both retention suites assert a foreign cancellation does not end the execute task. Mutation-checked: reintroducing the token-gatedTask.Runand the old sweep filter makes three of them fail.Vulthil.SharedKernel.Outbox.TestsandVulthil.Messaging.Inbox.Testspass on net9.0 and net10.0.WebApi.Testspasses 8/8 end-to-end against PostgreSQL + RabbitMQ containers, exercising the per-test relay restart.Backport to v1.0: yes