Skip to content

[release/17.0] Make MAUI build-queue cancellation surface a consistent OperationCanceledException - #22046

Closed
Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-47-pr21651from
mitchdenny-backport-17-0-48-pr21571
Closed

Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-47-pr21651from
mitchdenny-backport-17-0-48-pr21571

Conversation

@mitchdenny

Copy link
Copy Markdown
Member

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

  • 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
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

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

This comment has been minimized.

@mitchdenny
Mitch Denny (mitchdenny) marked this pull request as ready for review November 10, 2026 05:15
@mitchdenny

Copy link
Copy Markdown
Member Author

Approved for inclusion in release/17.0. Do not merge yet.

@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 -- 22046

Or

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

@github-actions

This comment has been minimized.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants