Repository navigation
Use a task that runs until cancelled in the task store cancel test - #1955
Merged
tarekgh merged 1 commit intoOct 23, 2026
Merged
Conversation
CancelTaskAsync_ViaStore_TransitionsToCancelled cancels a task running slow-tool, which finishes by itself after 200 ms. If the tool finishes before the tasks/cancel handler marks the task cancelled, the task stays completed, so tasks/get returns CompletedTaskResult and the test fails. This happened on windows-latest Debug (net9.0): https://github.com/modelcontextprotocol/csharp-sdk/actions/runs/39976544742/job/120528606965 Add a long-running-tool that waits until it is cancelled, like the one in TaskCancellationIntegrationTests, and use it in this test. slow-tool is unchanged for the other tests that use it.
Contributor
Author
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.
Problem
McpTaskStoreTests.CancelTaskAsync_ViaStore_TransitionsToCancelledrunsslow-toolas a task and then cancels it, butslow-toolfinishes by itself after 200 ms. If the tool finishes before thetasks/cancelhandler marks the task cancelled, the task stays completed, becauseInMemoryMcpTaskStore.SetCancelledAsyncdoes not change a finished task.tasks/getthen returnsCompletedTaskResultand the test fails:This happened in build (windows-latest, Debug) on .NET 9 for #1856, which does not touch Tasks; attempt 3 of that job passed. In the test log the server starts the
tasks/cancelhandler, then logs"slow-tool" completed. IsError = False., then finishes the handler after 250 ms.Change
long-running-tooltoTaskStoreTestTools. It waits onTask.Delay(Timeout.Infinite, cancellationToken), like the tool inTaskCancellationIntegrationTests.CancelTaskAsync_ViaStore_TransitionsToCancelleduses it, so the task can only end throughtasks/cancel. The cancel handler marks the task cancelled before it cancels the tool's token, so the tool stops and the task stays cancelled. Iftasks/cancelarrives before the tool starts, the tool gets an already-cancelled token and the task still ends cancelled.slow-toolis unchanged. Two other tests still use it, andCallToolAsync_WithTaskStore_PollsToCompletionneeds it to finish.Validation
Built
tests/ModelContextProtocol.Testsfor net10.0, net9.0, net8.0 (Debug) and net472 (Release) with 0 warnings, then ran the tests with vstest, for example:await Task.Delay(1000, ct);beforeCancelTaskAsync(not in this PR), the test failed 10 of 10 times onmainwith the CI message, and passed 10 of 10 times with this change (.NET 10).FullyQualifiedName~McpTaskStoreTests|FullyQualifiedName~TaskCancellationIntegrationTests(58 tests, includingInMemoryMcpTaskStoreTests): all passed on .NET 10, 9 and 8. On .NET Framework on Windows,McpTaskStoreTestsandTaskCancellationIntegrationTestsare skipped (Running ModelContextProtocol.Tests in net472 is causing VS test platform to hang #587).ModelContextProtocol.TestswithExecution!=Manual(the CI filter): 0 failed on .NET 10 and on .NET Framework 4.7.2.