Repository navigation
Fix unobserved task exception in WithCancellationAsync and unignore flaky tests - #9427
Conversation
…ests The "observe the exception" continuation in both WithCancellationAsync overloads was scheduled via ContinueWith(observeAction, cancellationToken). Passing the cancellation token cancels the *continuation itself* once the token fires, so when the awaited task later faults, its exception is never observed. This leaks an unobserved TaskException that surfaces during GC and flakily crashes unrelated tests (issue #6907), which is why CancellationAsync_ObserveException_Succeeds and CancellationAsyncWithReturnValue_ObserveException_Succeeds were ignored. Schedule the observe continuation on TaskScheduler.Default instead (matching TimeoutAfterAsync in the same file) so the exception is always observed. A focused repro that drives the actual product method showed 200/200 iterations leaking before the fix and 0/200 after, while cancellation is still observed in every iteration. Re-enabled both tests; they pass reliably (verified across many consecutive runs on net8.0 and net462). Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR fixes a flakiness source in Microsoft.Testing.Platform’s internal TaskExtensions.WithCancellationAsync helpers by ensuring faulted task exceptions are always observed, and then re-enables the two unit tests that were previously ignored due to the resulting UnobservedTaskException crashes.
Changes:
- Update both
WithCancellationAsyncoverloads to schedule the “observe exception” continuation without using the cancellation token (usingTaskScheduler.Defaultinstead). - Re-enable the two previously ignored
TaskExtensionsTestscovering exception observation under cancellation.
Show a summary per file
| File | Description |
|---|---|
src/Platform/Microsoft.Testing.Platform/Helpers/TaskExtensions.cs |
Fixes the unobserved-exception bug by preventing the observe-continuation from being canceled. |
test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/TaskExtensionsTests.cs |
Removes [Ignore] on the two flaky tests now that the underlying product issue is addressed. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
This comment has been minimized.
This comment has been minimized.
|
Note 🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging Code-Review Findings (not tied to diff lines)🔴 Finding 1 —
|
Amaury Levé (Evangelink)
left a comment
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
Review Summary — PR #9427
The root-cause diagnosis is correct and the fix for WithCancellationAsync is sound. ContinueWith(action, cancellationToken) treats the token as a scheduling guard — the continuation is silently dropped when the token fires — while ContinueWith(action, TaskScheduler.Default) always runs it. The cancellation semantics visible to callers are unchanged. TaskScheduler.Default is available on every supported TFM (net462, netstandard2.0, net8.0, net9.0) with identical behaviour.
2 findings — see inline comment above.
| # | Dimension | Result | Severity |
|---|---|---|---|
| 1 | Algorithmic Correctness (WithCancellationAsync fix) | ✅ LGTM | — |
| 2 | Threading & Concurrency | ✅ LGTM | — |
| 3 | Cross-TFM Compatibility | ✅ LGTM | — |
| 4 | Resource & IDisposable Management | ✅ LGTM | — |
| 5 | Public API & Binary Compatibility | ✅ LGTM | — |
| 6 | Performance & Allocations | ✅ LGTM | — |
| 7 | Test Isolation | ✅ LGTM | — |
| 8 | Test Assertion Quality | ✅ LGTM | — |
| 9 | Test Completeness & Coverage | ✅ LGTM | — |
| 10 | Scope & PR Discipline | ✅ LGTM | — |
| 11 | Documentation / Code Comments | ✅ LGTM | — |
| 12 | Code Consistency — TimeoutAfterAsync with token |
❌ ISSUE | MAJOR |
| 13 | Flakiness — WaitOne() without timeout |
MODERATE |
11/13 dimensions clean.
- Fix the same unobserved-exception bug in the TimeoutAfterAsync(Task, TimeSpan, CancellationToken) overload, which still passed the token to ContinueWith. Schedule its observe continuation on TaskScheduler.Default too (MAJOR review finding). - Remove the observeException parameter from all four helpers: it was never passed as false, so the observe continuation now always runs. Updated the single explicit caller in MessageHandlerFactory. - Add explanatory comments at every observe continuation describing why it must be scheduled on TaskScheduler.Default rather than via the token. - Bound both waitException.WaitOne() calls in the tests with a 30s timeout and assert on it, so a cancelled inner task fails cleanly instead of hanging (MODERATE review finding). - Drop the now-stale "#if !NETFRAMEWORK // Polyfill bug" guard around the CancellationToken assertion: the WaitAsync polyfill preserves the token, and the assertion passes on net462 (verified 6/6 runs). Co-authored-by: Copilot <[email protected]>
|
Addressed both Expert Code Review findings in da3a831: Finding 1 (MAJOR) — Finding 2 (MODERATE) — unbounded Verified: all 9 |
This comment has been minimized.
This comment has been minimized.
Removing the 'observeException' parameter changed the binary signature of the internal TaskExtensions helpers, which shipped extensions (HangDump, TrxReport) bind to across assemblies. Older extension packages exercised by the forward-compatibility acceptance test (NewerPlatform_WithPreviousExtensions_ShouldExecuteTests) threw MissingMethodException and crashed the test host (exit code 134). Restore the parameter to keep the signature binary-compatible while preserving the real fix: scheduling the fire-and-forget exception observer on TaskScheduler.Default instead of passing the cancellation token (#6907). Co-authored-by: Copilot <[email protected]>
🧪 Test quality grade — PR #9427
This advisory comment was generated automatically. Grades are heuristic
|
Summary
Re-enables the two ignored
TaskExtensionsTests(issue #6907) by fixing the underlying product bug that made them flaky.Root cause
In both
WithCancellationAsyncoverloads, the "observe the exception" continuation was scheduled with:Passing
cancellationTokentoContinueWithcancels the continuation itself once the token fires. So when the token cancels before the awaited task faults, the observe continuation never runs, the task's exception is never observed, and it leaks as anUnobservedTaskExceptionthat surfaces during GC — flakily crashing unrelated tests. The siblingTimeoutAfterAsyncin the same file already schedules its observe continuation without the token.Fix
Schedule the observe continuation on
TaskScheduler.Defaultinstead of passing the cancellation token, so the exception is always observed. The returned task's cancellation semantics are unchanged.Verification
A focused repro driving the actual product
WithCancellationAsync<T>(cancel at 50ms, inner task faults at 250ms, 200 iterations):Both unignored tests pass reliably — 15× consecutive on net8.0 and 8× on net462, all green.
No public API or user-facing behavior change (internal helper). No changelog entry added since entries here reference merged PR numbers; happy to add one if maintainers prefer.
Closes #6907.
Co-authored-by: Copilot [email protected]