Skip to content

Fix flaky NuGetPackagePrefetcher cancellation logging test - #21599

Merged
David Fowler (davidfowl) merged 1 commit into
microsoft:mainfrom
davidfowl:fix-20507-prefetcher-cancel-token
Oct 31, 2026
Merged

David Fowler (davidfowl) merged 1 commit into
microsoft:mainfrom
davidfowl:fix-20507-prefetcher-cancel-token

Conversation

@davidfowl

Copy link
Copy Markdown
Collaborator

Description

Fixes a flaky test: NuGetPackagePrefetcherTests.PrefetchingCancellationDueToShutdownLogsCleanMessage times out after 30s waiting for the "prefetching was cancelled" log messages. It has 10 recorded occurrences, 9 of them on Cli (ubuntu-latest).

Root cause

This is a race in the test, not a product bug.

  • NuGetPackagePrefetcher is a BackgroundService. StartAsync(stoppingCts.Token) creates a linked stopping token, which ExecuteAsync passes to GetChannelsAsync and CheckForCliUpdatesAsync. It classifies cancellation with catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested).
  • The test's callbacks ignored the token they were given and awaited AsyncTestHelpers.WaitForCancellationAsync(stoppingCts.Token), the outer token.
  • CancellationTokenSource.Cancel() runs registrations newest-first. The callbacks' registrations, made after StartAsync, run before the one that cancels the linked token. Their RunContinuationsAsynchronously continuations can resume the prefetcher on the thread pool while the linked token isn't cancelled yet.
  • When that happens, the when filter is false and the cancellation falls into catch (Exception), which logs Non-fatal error while prefetching ... at Debug. The Trace "was cancelled" messages are never written, so Task.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 cancellationToken the prefetcher passes them. That token is cancelled before any of its callbacks run, so the filter always sees IsCancellationRequested == 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.Token after StartAsync, 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.

  • Before (original test plus harness): fails every time with the CI signature. The harness was released by the two "Non-fatal" logs, which confirms the misclassification.
    failed ...NuGetPackagePrefetcherTests.PrefetchingCancellationDueToShutdownLogsCleanMessage (5s 027ms)
      System.TimeoutException : The operation at .../NuGetPackagePrefetcherTests.cs:186 timed out ...
    
    Line 186 was the await Task.WhenAll(templateTcs.Task, cliTcs.Task) statement, which is line 175 on main without the harness lines, matching the CI failure.
  • After (fixed test plus the same harness): passes. No "Non-fatal" logs are written; the harness waits out its bound, then the linked token cancels and both "was cancelled" messages appear.
  • After (committed change): NuGetPackagePrefetcherTests passed 10/10 bounded local runs, 30/30 tests each.

Fixes #20507

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
    • No

🤖 Posted by AspireAgent (AI Agent)

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>
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21599

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21599"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CI Failure] Flaky: NuGetPackagePrefetcherTests.PrefetchingCancellationDueToShutdownLogsCleanMessage times out after 30s

3 participants