Fix race condition in BackgroundServiceExceptionTests#125570
Open
lewing wants to merge 2 commits intodotnet:mainfrom
Open
Fix race condition in BackgroundServiceExceptionTests#125570lewing wants to merge 2 commits intodotnet:mainfrom
lewing wants to merge 2 commits intodotnet:mainfrom
Conversation
The StopAsync and StopTwiceAsync tests used Task.Delay(200ms) to wait for a background service (which delays 100ms then throws) to fault. On loaded CI agents, 200ms is not always enough for the exception to propagate through the host, causing Assert.Throws to see no exception. For StopHost behavior tests: wait for ApplicationStopping, which fires after the host has captured the exception and called StopApplication(). For Ignore behavior test: use a SignalingFailureService that signals a TaskCompletionSource before throwing, so the test knows the service has faulted without relying on timing. Fixes dotnet#125550 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Improves reliability of Microsoft.Extensions.Hosting unit tests by replacing timing-based delays with deterministic synchronization points when validating background service exception handling.
Changes:
- Replaced racy
Task.Delay(...)waits withIHostApplicationLifetime.ApplicationStoppingsignaling forStopHostbehavior tests. - Updated the
Ignorebehavior test to use a signaling background service (SignalingFailureService) to deterministically coordinate when the failure path is reached. - Added 10s
WaitAsynctimeouts as CI safety nets.
You can also share your feedback on Copilot code review. Take the survey.
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/BackgroundServiceExceptionTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-hosting |
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.
Problem
BackgroundService_AsynchronousException_StopAsync_ThrowsExceptionis flaky (3 hits in 24 hours, #125550).The test starts a
BackgroundServicethat delays 100ms then throws, waitsTask.Delay(200ms), and expectsStopAsyncto surface the exception. On loaded CI agents the 200ms isn't always enough for the exception to propagate through the host, soAssert.Throwssees no exception.Fix
Replace the racy
Task.Delay(200ms)with deterministic synchronization:For
StopHostbehavior tests (StopAsync_ThrowsException,StopTwiceAsync_ThrowsException): Wait forIHostApplicationLifetime.ApplicationStopping, which fires after the host has captured the faulted service exception and calledStopApplication(). This is the correct signal because it means the host has processed the exception and is ready to surface it onStopAsync.For
Ignorebehavior test (IgnoreException_StopAsync_DoesNotThrow): Use aSignalingFailureServicethat signals aTaskCompletionSourcebefore throwing. Since this test verifiesStopAsyncdoes not throw, we just need to know the service has faulted — no host-level processing is needed.Both use a 10-second timeout as a safety net for CI.
Fixes #125550