Repository navigation
Conversation
## Motivation and Context When the Streamable HTTP transport removes a session, `cleanup_session_unsafe` wakes the threads waiting on the session's server-to-client requests with `:session_closed`, and `release_session` then closes the session's GET and POST streams before it cancels the session's requests. A close can block for up to `stream_write_timeout:` (10 seconds by default) on a stalled peer, and the cancellation waits behind it. Two documented guarantees of the Cancellation page fail meanwhile: - A request that reaches the session as it ends (modelcontextprotocol#698) is admitted and run: its tool runs with `server_context.cancelled?` false, and over SSE its result is written to its POST stream after the `DELETE`. - A request waiting on a nested server-to-client request is woken with `:session_closed` before its own token is cancelled: the nested call raises "SSE session closed while waiting", the handler sees `cancelled?` false, and the request is recorded as an internal error instead of a cancellation. The wake-up runs first even without a stalled stream, so this outcome depends on thread scheduling. `release_session` now cancels the session's requests first, then wakes the remaining waits with `:session_closed`, then closes the streams. `cleanup_session_unsafe` collects the waits under the lock instead of waking them; no wait is added for a session once it is removed. A nested request is now woken through its parent's cancellation hook, so it raises `MCP::CancelledError` as it does for `notifications/cancelled`. Cancellations still run outside `@mutex`. ## How Has This Been Tested? New tests in `test/mcp/server/transports/streamable_http_transport_test.rb`, with a GET stream whose close blocks until released: an SSE request whose body runs while the `DELETE` is held up closing that stream does not run its tool and writes nothing; a request waiting on an elicitation when its session is terminated sees `MCP::CancelledError` with `cancelled?` true and is recorded as cancelled with the reason "Session terminated". Both fail against the previous library. Closing the streams before cancelling fails both, waking the waits before cancelling fails the second, and dropping the wake-up hangs the existing "send_request unblocks when session is cleaned up" test. ## Breaking Changes None. `on_cancel` hooks of a terminated session's requests now run before its streams are closed rather than after. Fixes modelcontextprotocol#708.
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 and #698: ending a session now cancels its requests before anything that can block, so a stalled stream no longer leaves them running, and a request waiting on a nested request ends cancelled instead of failing.
Fixes #708
Motivation and Context
When
StreamableHTTPTransportremoves a stateful session,cleanup_session_unsafewakes the threads waiting on the session's server-to-client requests with:session_closed(lib/mcp/server/transports/streamable_http_transport.rb:2053), andrelease_session(:2071) then closes the GET and POST streams before it callscancel_all_incoming. A close can take up tostream_write_timeout:(10 seconds by default,:2092) on a stalled peer, and the cancellation waits behind it. Two guarantees of the Cancellation page fail meanwhile:server_context.cancelled?false, and over SSE its result is written to its POST stream after theDELETE.:session_closedbefore its own token is cancelled: the nested call raises "SSE session closed while waiting" (:848), the handler seescancelled?false, and the request is reported as an exception and recorded as an internal error rather than a cancellation. The wake-up runs before the cancellation even without a stalled stream, so this outcome depends on thread scheduling.On
main(00868f7), with a GET stream whosecloseblocks until released:With this change:
release_sessionnow cancels the session's requests first, then wakes the remaining waits with:session_closed, then closes the streams.cleanup_session_unsafecollects those waits under the lock instead of waking them there;send_requestregisters a wait only for a session it finds under the lock, so none is added once the session is removed. A nested request is now woken through its parent's cancellation hook, so it raisesMCP::CancelledErroras it does afternotifications/cancelled, and:session_closedstill ends the waits that belong to no request (asend_requestmade outside a handler). Cancellations still run outside@mutex, as the existing "cancellation hooks of a terminated session's requests run outside the transport lock" test checks.How Has This Been Tested?
New tests in
test/mcp/server/transports/streamable_http_transport_test.rb, with a GET stream whosecloseblocks until released:DELETEis held up closing that stream does not run its tool and writes nothing.MCP::CancelledErrorwithcancelled?true, and is recorded as cancelled with the reason "Session terminated".Both fail against the previous library. Closing the streams before cancelling fails both; waking the waits before cancelling fails the second; dropping the wake-up hangs the existing "send_request unblocks when session is cleaned up" test.
bundle exec rake test,bundle exec rake rubocopandbundle exec rake conformance:test(baseline) pass locally on Ruby 4.0.9.Breaking Changes
None. The
on_cancelhooks of a terminated session's requests now run before its streams are closed rather than after, so a hook that blocks now delays the closes instead of the other way round.Types of changes
Checklist
Additional context
The ordering trade-off is the one in Breaking Changes: a blocking
on_cancelhook can now hold up the stream closes. I preferred that to the opposite, since a stalled close used to leave requests of an ended session running. The two parts can be split if you prefer: cancelling before the closes fixes (a), and waking the waits after the cancellation fixes (b).