Fix race condition in BackgroundServiceExceptionTests#125570
Fix race condition in BackgroundServiceExceptionTests#125570svick merged 4 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>
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>
|
Tagging subscribers to this area: @dotnet/area-extensions-hosting |
There was a problem hiding this comment.
Pull request overview
Reduces flakiness in Microsoft.Extensions.Hosting unit tests by replacing a timing-based wait with a deterministic signal that the host has begun stopping after a background service fault, so StopAsync reliably surfaces the captured exception.
Changes:
- Replaced
Task.Delay(200ms)with waiting onIHostApplicationLifetime.ApplicationStopping(with a 10s timeout) for StopHost-behavior tests. - Updated comments in the Ignore-behavior
StopAsynctest (but the test still uses a fixed delay).
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/BackgroundServiceExceptionTests.cs
Show resolved
Hide resolved
|
/ba-g All failures seem to be iOS specific and not related to this PR. |
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.ForIgnorebehavior 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