Repository navigation
Conversation
## Motivation and Context Since modelcontextprotocol#692, ending a stateful Streamable HTTP session cancels the requests in flight in it. A request the transport has already handed to the session but that `Server` has not yet registered is missed: `ServerSession#cancel_all_incoming` cancels only what the registry holds, and the late request then registers with a fresh token, runs its handler with `server_context.cancelled?` false, and delivers its result to the terminated session. Over SSE the window spans from `handle_regular_request` returning the response to the Rack server calling its body, which is where the request is dispatched. A `DELETE` from another connection in that gap removes the session, yet the body still runs the tool and writes its result. In JSON response mode the window is between the transport looking the session up and `Server#handle_request` registering the request. `cancel_all_incoming` now also closes the registry under its lock: a request registered afterwards gets a token already cancelled with the same reason, which is not tracked. `Server#handle_request` does not start the handler of a request whose token is cancelled before it begins, records the cancellation in the instrumentation, and sends no response. Lock order is unchanged: only the registry lock is taken, and the cancellations still run outside it. ## How Has This Been Tested? New tests in `test/mcp/server/transports/streamable_http_transport_test.rb`: an SSE request whose session is terminated with `DELETE` between the response and its body does not run its tool and writes nothing; a JSON-mode request held, with a `Queue` barrier, after the transport looked its session up and before it registered, does not run its tool and is answered 202 when the session is terminated in that gap. New tests in `test/mcp/server_cancellation_test.rb` check that a registration after `cancel_all_incoming` returns a token already cancelled with its reason and not tracked, that such a request does not run its handler, gets no response and records the cancellation, and that another session is not affected. The request tests fail against the previous library. Dropping the registry closure, the cancellation of the late token, its reason, the untracked registration, the early return in `Server`, or its instrumentation each fails a test. ## Breaking Changes None. A request that reaches a session as the transport ends it is not run. It previously ran uncancelled.
This branch has not been deployed
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.
Follows #692: a request the transport handed to a session just before the session ended is no longer run uncancelled.
Motivation and Context
Since #692, ending a stateful Streamable HTTP session cancels the requests in flight in it. A request the transport has already handed to the session, but that
Serverhas not registered yet, is missed:ServerSession#cancel_all_incomingcancels only what the registry holds (lib/mcp/server_session.rb:112), and the late request then registers with a fresh token (lib/mcp/server.rb:807), runs its handler withserver_context.cancelled?false, and delivers its result to the terminated session.Over SSE the window spans from
handle_regular_requestreturning the response to the Rack server calling its body, which is where the request is dispatched (lib/mcp/server/transports/streamable_http_transport.rb:2537). ADELETEfrom another connection in that gap removes the session, the body finds no session to claim a stream in (:2518), and still runs the tool and writes its result. In JSON response mode the window is between the transport looking the session up (:2481) and the registration.On
main(c2ce3c1), through the Rack interface, with aDELETEbetween thetools/callresponse and its body:With this change:
cancel_all_incomingnow also closes the registry under its lock: a request registered afterwards gets a token already cancelled with the same reason, which is not tracked.Server#handle_requestdoes not start the handler of a request whose token is cancelled before it begins, records the cancellation in the instrumentation as the result path does, and sends no response, so a JSON-mode POST is answered 202 as for any cancelled request. Lock order is unchanged: only the registry lock is taken, and the cancellations still run outside it, soon_cancelhooks can call back into the transport as before.The Cancellation docs page now says this.
How Has This Been Tested?
New tests in
test/mcp/server/transports/streamable_http_transport_test.rb:DELETEbetween the response and its body does not run its tool and writes nothing.Queuebarrier after the transport looked its session up, and before it registered, does not run its tool and is answered 202 when the session is terminated in that gap.New tests in
test/mcp/server_cancellation_test.rb:cancel_all_incomingreturns a token already cancelled with its reason, and it is not tracked.cancel_all_incomingon one session leaves registration on another open.The request tests fail against
main. Dropping the registry closure, the cancellation of the late token, its reason, the untracked registration, the early return inServer, or its instrumentation each fails a test. On Ruby 3.4.4:bundle exec rake test(2719 runs, 0 failures, 3 skips),bundle exec rake rubocop(no offenses), andbundle exec rake conformance:test(baseline check passed for server and client).Breaking Changes
None. A request that reaches a session as the transport ends it is not run. It previously ran uncancelled.
Types of changes
Checklist
Additional context
This merges cleanly with #695 and #697, and the full suite passes on
mainwith all three applied. With #695 a session no longer expires while a request is in flight, but the window here is before the request is in flight, so it is not closed by it.A request that reuses an in-flight id after the session ended now gets no response instead of Invalid Request, since the session has no client left to answer. Notifications on a terminated session are not tracked in the registry and are not affected. Only Ruby 3.4.4 was run locally; CI covers the rest of the matrix.