Repository navigation
fix: honor cancellation in the input-required loop - #730
Merged
koic merged 1 commit intoDec 1, 2026
Merged
Conversation
## Motivation and Context The client looks at the `cancellation:` token only when it sends a request. Between requests, the remaining `on_elicitation`, `on_sampling` and `on_roots` handlers of the round run after a cancel, and the backoff of a `requestState`-only result is slept to its end. The token is now checked before each round and before each handler. With a token, the backoff waits on a condition variable that the cancel signals, against a monotonic deadline, and its callback is removed when the wait ends. Re-issuing the request then raises `CancelledError` before anything is sent. Without a token, `sleep` is used as before. No `notifications/cancelled` is sent in these cases, because no request is in flight. ## How Has This Been Tested? New tests in `test/mcp/client_test.rb`: - A cancel in the first of two handlers does not run the second. - A cancel after a response takes precedence over the round limit. - A cancel wakes a backoff wait. The test cancels once the wait has been entered, so it does not depend on timing. - A backoff that ends normally removes its callback from the token. All four fail on `main`, and reverting any one part of the change fails one of them. ## Breaking Changes None. Fixes modelcontextprotocol#727.
7 of 13 tasks
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.
Cancelling a request during an
input_requiredround now stops further input handlers and wakes the backoff wait.Fixes #727
Motivation and Context
The client looks at the
cancellation:token only when it sends a request. Between requests, the remainingon_elicitation,on_samplingandon_rootshandlers of the round run after a cancel, and the backoff of arequestState-only result is slept to its end.The token is now checked before each round and before each handler. With a token, the backoff waits on a condition variable that the cancel signals, against a monotonic deadline, and its callback is removed when the wait ends. Re-issuing the request then raises
CancelledErrorbefore anything is sent. Without a token,sleepis used as before. Nonotifications/cancelledis sent in these cases, because no request is in flight.How Has This Been Tested?
New tests in
test/mcp/client_test.rb:All four fail on
main, and reverting any one part of the change fails one of them.bundle exec rake test(2,812 tests, 10,194 assertions on Ruby 4.0.9; also run on 2.7),bundle exec rake rubocop,bundle exec yard --no-outputandbundle exec rake conformance:test(baseline) pass locally.Breaking Changes
None.
Types of changes
Checklist
Additional context
#707 covers the server's legacy
input_requiredshim and does not overlap.