[WIP] Fix flaky task cancellation test hang on Windows Debug CI (#1701)#1702
Draft
PranavSenthilnathan wants to merge 1 commit into
Draft
[WIP] Fix flaky task cancellation test hang on Windows Debug CI (#1701)#1702PranavSenthilnathan wants to merge 1 commit into
PranavSenthilnathan wants to merge 1 commit into
Conversation
The test TaskTool_CancellationToken_GetTaskShowsWorkingBeforeCancel called CancelTaskAsync and returned immediately without waiting for the background Task.Run (the tool body) to finish. On Windows in Debug mode, thread-pool scheduling pressure can keep that runner alive past server disposal, causing McpServerImpl.DisposeAsync to race with it and—in the worst case—hang waiting for the MRTR in-flight counter to reach zero. Two fixes: 1. Test: add �wait _toolCancellationFired.Task.WaitAsync(...) after CancelTaskAsync, mirroring the pattern used by the sibling test TaskTool_CancellationToken_FiresWhenExplicitlyCancelled. 2. Infrastructure: track each background Task.Run in a ConcurrentDictionary<Task, byte> and drain it in DisposeAsync (after cancelling all CTSs). This makes server disposal deterministic regardless of whether a test explicitly waits for tool cancellation. Fixes: #1701 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Fixes #1701 —
TaskTool_CancellationToken_GetTaskShowsWorkingBeforeCancelintermittently causes the xUnit test host to hang for 7+ minutes on Windows Debug CI.Root Cause
Two issues combine to cause the hang:
1. Test doesn't wait for background tool cancellation before teardown
TaskTool_CancellationToken_GetTaskShowsWorkingBeforeCancelends with:CancelTaskAsyncsignals the CTS but doesn't wait for the tool body to finish. The sibling test (TaskTool_CancellationToken_FiresWhenExplicitlyCancelled) correctly awaits_toolCancellationFired.Task.WaitAsync(...)first — the flaky test was missing this.2. Background
Task.Runnot tracked during server disposalWhen a
TaskStoreis configured,McpServerImplfires off backgroundTask.Runlambdas to run tools but never tracked or awaited them inDisposeAsync. Under the MRTR-aware path (which is active on the 2026-07-28 protocol with a stateful transport),ObserveHandlerCompletionAsyncmust decrement_mrtrInFlightCountbeforeDisposeAsynccan complete. On Windows in Debug builds, thread-pool scheduling pressure can delay that decrement, causingDisposeAsyncto wait on_allMrtrHandlersCompleted— and if the background task itself is also delayed, this manifests as a multi-minute hang.Fixes
Fix 1 (test): Add
await _toolCancellationFired.Task.WaitAsync(TestConstants.DefaultTimeout, ct)afterCancelTaskAsyncin the flaky test, matching the pattern used by the passing sibling test.Fix 2 (infrastructure): Track each background
Task.Runin_backgroundTasks: ConcurrentDictionary<Task, byte>and drain it at the end ofDisposeAsync(after the CTSs are cancelled). This makes server disposal deterministic — even if a test skips the wait, the server won't tear down while a background runner is still alive.Testing
TaskCancellation*tests pass on .NET 8, 9, 10 (they are skipped on .NET Framework 4.7.2 by the existing#if !NETguard tracking issue Running ModelContextProtocol.Tests in net472 is causing VS test platform to hang #587)