Repository navigation
Fix flaky NuGetPackagePrefetcher cancellation logging test - #21599
Merged
David Fowler (davidfowl) merged 1 commit intoOct 31, 2026
Merged
David Fowler (davidfowl) merged 1 commit into
David Fowler (davidfowl) merged 1 commit into
Conversation
The test's prefetch callbacks waited on the outer stoppingCts.Token instead of the token the prefetcher passes them (BackgroundService's linked stopping token). Cancel() runs the newest registrations first, so the callbacks' cancellation could resume the prefetcher on the thread pool before the linked token was cancelled. The 'when (stoppingToken.IsCancellationRequested)' filter was then false, the cancellation was logged as a non-fatal error, and the test timed out waiting for the 'was cancelled' messages. Fixes #20507 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21599Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21599" |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the test-only cancellation-token adjustment.
No open issues 🎉
What changed in this PR
Fixes the flaky CLI prefetcher shutdown-logging test associated with #20507 by aligning test cancellation with the token observed by the prefetcher. Production code and existing assertions remain unchanged.
Changes:
- Updates both test callbacks to await the supplied cancellation token rather than the outer token.
- Adds a comment explaining the cancellation-ordering race.
| File | Description |
|---|---|
tests/Aspire.Cli.Tests/NuGet/NuGetPackagePrefetcherTests.cs |
Corrects cancellation-token usage in both callbacks and documents why it prevents the logging race. |
Review effort: Balanced
Sébastien Ros (sebastienros)
approved these changes
Oct 31, 2026
4 of 14 tasks
This was referenced Oct 31, 2026
This was referenced Nov 16, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
Fixes a flaky test:
NuGetPackagePrefetcherTests.PrefetchingCancellationDueToShutdownLogsCleanMessagetimes out after 30s waiting for the "prefetching was cancelled" log messages. It has 10 recorded occurrences, 9 of them onCli (ubuntu-latest).Root cause
This is a race in the test, not a product bug.
NuGetPackagePrefetcheris aBackgroundService.StartAsync(stoppingCts.Token)creates a linked stopping token, whichExecuteAsyncpasses toGetChannelsAsyncandCheckForCliUpdatesAsync. It classifies cancellation withcatch (OperationCanceledException) when (stoppingToken.IsCancellationRequested).AsyncTestHelpers.WaitForCancellationAsync(stoppingCts.Token), the outer token.CancellationTokenSource.Cancel()runs registrations newest-first. The callbacks' registrations, made afterStartAsync, run before the one that cancels the linked token. TheirRunContinuationsAsynchronouslycontinuations can resume the prefetcher on the thread pool while the linked token isn't cancelled yet.whenfilter is false and the cancellation falls intocatch (Exception), which logsNon-fatal error while prefetching ...at Debug. The Trace "was cancelled" messages are never written, soTask.WhenAll(templateTcs.Task, cliTcs.Task)times out.In production, the prefetch operations observe the same linked token that the filter checks, so this ordering can't happen there.
Fix
Test-only. The callbacks now await the
cancellationTokenthe prefetcher passes them. That token is cancelled before any of its callbacks run, so the filter always seesIsCancellationRequested == true. There are no sleeps or retries, and the assertions are unchanged.Evidence
To force the bad ordering deterministically, I used a local-only harness, not committed. It registered on
stoppingCts.TokenafterStartAsync, so it runs between the callbacks' registrations and the linked-token propagation. It held cancellation until the prefetcher logged its "Non-fatal error" messages, bounded at 3s.await Task.WhenAll(templateTcs.Task, cliTcs.Task)statement, which is line 175 on main without the harness lines, matching the CI failure.NuGetPackagePrefetcherTestspassed 10/10 bounded local runs, 30/30 tests each.Fixes #20507
Checklist
🤖 Posted by AspireAgent (AI Agent)