Repository navigation
[release/17.0] Make MAUI build-queue cancellation surface a consistent OperationCanceledException - #22046
Closed
Mitch Denny (mitchdenny) wants to merge 1 commit into
Conversation
…eledException (#21571) ## Description Fixes #20284. The test `MauiBuildQueueTests.CancelQueuedResource_CompletesGracefullyAndDoesNotAcquireSemaphore` was flaky: it intermittently got `TaskCanceledException` where it asserts an exact `OperationCanceledException`. ### Root cause `MauiBuildQueueEventSubscriber.OnBeforeResourceStartedAsync` handles a busy build lock in this order: 1. It registers the per-resource `CancellationTokenSource` (the stop command cancels it). 2. It publishes the `Queued` state. 3. It calls `await semaphore.WaitAsync(resourceCts.Token)`. When the user cancels the resource (which is what the test does once it sees `Queued`), the exception type depends on when the cancellation lands: | When the cancel lands | Exception from `SemaphoreSlim.WaitAsync` | |---|---| | During the wait (after step 3) | `OperationCanceledException` | | Between steps 2 and 3 (token already cancelled) | `Task.FromCanceled` fast path, so `TaskCanceledException` | The user-cancel `catch` used `throw;` and so passed whichever type it caught straight to the caller. The same applies to cancellation observed inside the build itself. The result was the CI failure: ``` Assert.IsType() Failure: Value is not the exact type Expected: typeof(System.OperationCanceledException) Actual: typeof(System.Threading.Tasks.TaskCanceledException) at ...MauiBuildQueueTests.CancelQueuedResource_CompletesGracefullyAndDoesNotAcquireSemaphore() in MauiBuildQueueTests.cs:line 543 ``` ### Fix On the user-cancel path (`resourceCts` cancelled, caller token not cancelled), the handler now throws one `OperationCanceledException("Build cancelled for resource '<name>'.", inner, resourceCts.Token)`. The original exception is kept as `InnerException`. Callers get the same exception shape no matter where cancellation was observed. Callers are otherwise unaffected. `DcpExecutor` catches `Exception` around resource start (`DcpExecutor.cs` lines ~1052, ~1220 and ~1297) and handles every failure the same way. The `FailedToStart` → `Exited` override in the stop command does not look at the exception. There are no assertion changes, sleeps or retries. The existing `Assert.IsType<OperationCanceledException>` is kept. ### Regression test (deterministic) `CancelQueuedResource_BeforeSemaphoreWait_ThrowsOperationCanceledException` adds a command annotation to the queued resource. Its `updateState` callback cancels the resource when it sees the `Queued` snapshot. `ResourceNotificationService.PublishUpdateAsync` runs command state callbacks synchronously while publishing, so the cancellation always lands between steps 2 and 3. That is exactly the race window, with no timing dependence. - **Before the fix (main 6a8f531):** fails on every run with the CI signature: `Expected: typeof(System.OperationCanceledException) / Actual: typeof(System.Threading.Tasks.TaskCanceledException)` at `MauiBuildQueueTests.cs:597`. - **After the fix:** passes. ### Stress evidence (Linux, 2 cores, whole `MauiBuildQueueTests` class per iteration, as in CI) - **Before the fix:** 3 of 150 iterations failed, all on `CancelQueuedResource_CompletesGracefullyAndDoesNotAcquireSemaphore` with `TaskCanceledException`. - **After the fix:** **0 of 300** iterations failed (same loop, same machine). ### Validation - `Aspire.Hosting.Maui.Tests`, `MauiBuildQueueTests` class: 42/42 passed, including the new test. - Full `Aspire.Hosting.Maui.Tests` (excluding quarantined and outerloop): 205/205 passed (Linux). - E2E: a real MAUI AppHost run needs MAUI workloads plus an emulator or device, which this environment doesn't have. The change only affects the exception object thrown on the user-cancel path. Its single consumer, `DcpExecutor`, handles all exceptions the same way (see above), so orchestration behavior does not change. ### Scope - The separate `SingleResource_ShowsBuildingState` hang (#21494) is already handled by #21492. This PR does not touch those tests or the watcher helpers. ## Checklist - Is this feature complete? - [x] Yes. Ready to ship. - [ ] No. Follow-up changes expected. - Are you including unit tests for the changes and scenario tests if relevant? - [x] Yes - [ ] No - Did you add public API? - [ ] Yes - [x] No - Does the change make any security assumptions or guarantees? - [ ] Yes - [x] No **🤖 Posted by AspireAgent (AI Agent)** Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> (cherry picked from commit b09d94e)
This comment has been minimized.
This comment has been minimized.
Mitch Denny (mitchdenny)
marked this pull request as ready for review
November 10, 2026 05:15
Member
Author
|
Approved for inclusion in release/17.0. Do not merge yet. |
Mitch Denny (mitchdenny)
requested review from
James Newton-King (JamesNK),
Adam Ratzman (adamint),
David Negstad (danegsta),
David Fowler (davidfowl),
Eric Erhardt (eerhardt),
Ella Hathaway (ellahathaway),
Karol Zadora-Przylecki (karolz-ms),
Ankit Jain (radical) and
Sébastien Ros (sebastienros)
as code owners
November 10, 2026 06:17
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-48-pr21571
branch
from
November 10, 2026 08:03
ec70976 to
e731eef
Compare
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 22046Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 22046" |
This comment has been minimized.
This comment has been minimized.
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-48-pr21571
branch
from
November 10, 2026 08:29
e731eef to
ec70976
Compare
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.
Description
Backport of #21571 to
release/17.0(cherry-picked from b09d94e with no modifications).Layer 48 of 55 in the release/17.0 backport stack; stacked on #22045.
Backport recommendation: No — Small change driven by a test flake.
Validation: Stable patch IDs match (
4c66360ad489bced4ba7b624c7e402ad833b53a6). Builds and tests not run for this unchanged cherry-pick.Backport of #21571
Checklist
<remarks />and<code />elements on your triple slash comments?