Repository navigation
Conversation
## Motivation and Context `StreamableHTTPTransport` removes a stateful session when the client terminates it with `DELETE`, when it passes `session_idle_timeout`, when its GET stream breaks without an event store, and when the transport is closed. Requests still running in that session were left running: their `MCP::Cancellation` was never flipped, so `server_context.cancelled?` stayed false, `on_cancel` hooks registered on `server_context.cancellation` never ran, and a long tool call kept working for a client that had gone. In JSON response mode its result was still written to the POST of a session that no longer existed. Only nested server-to-client waits were released, through the `:session_closed` that `cleanup_session_unsafe` pushes to them. Each removal path now releases the session through one `release_session`, which closes its streams as before and then cancels its in-flight requests with `ServerSession#cancel_all_incoming` and the reason `"Session terminated"`. Handlers observe it as they would `notifications/cancelled`, and their responses are suppressed the same way. The cancellations run once `@mutex` is released, as the stream closes already do: `Cancellation#cancel` fires its hooks synchronously, and a hook may call back into the transport, whose lock is not reentrant. The TypeScript SDK does the same in `Protocol#_onclose`, which aborts the handlers of every in-flight request when the transport closes. ## How Has This Been Tested? New tests in `test/mcp/server/transports/streamable_http_transport_test.rb` hold a tool call in flight with `Queue` barriers. They check that a `DELETE` cancels the request with the reason `"Session terminated"` and its POST is answered 202 with no body; that terminating one session leaves the requests of another running to their result; that `close`, the reaper, a request, a notification, a targeted notification, a broadcast, an `initialize` reclaiming an expired slot, and a failed GET stream write each cancel the requests of the session they remove; and that the hooks run without the transport lock held. All of them fail against the previous library, and skipping the cancellation on any one removal path fails the matching case. ## Breaking Changes A request still running when its session is removed no longer delivers its result. In JSON response mode its POST is answered 202 with no body, as for a request cancelled with `notifications/cancelled`. Over SSE the termination already closed the POST stream, so the result was lost before as well. This includes a request that outlives `session_idle_timeout` (30 minutes by default), since a session's idle time is counted from the start of its latest request.
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.
Cancels the requests still running in a Streamable HTTP session when the transport removes that session, so their handlers see
server_context.cancelled?and their responses are suppressed, as fornotifications/cancelled.Motivation and Context
StreamableHTTPTransportremoves a stateful session when the client terminates it withDELETE, when it passessession_idle_timeout, when its GET stream breaks without an event store, and when the transport is closed. Requests still running in that session were left running: theirMCP::Cancellationwas never flipped, soserver_context.cancelled?stayed false,on_cancelhooks registered onserver_context.cancellationnever ran, and a long tool call kept working for a client that had gone. In JSON response mode its result was still written to the POST of a session that no longer existed. Only nested server-to-client waits were released, through the:session_closedthatcleanup_session_unsafepushes to them.On
main(4451525), a tool call held in flight while its session is deleted:With this change:
Each removal path now releases the session through one
release_session, which closes its streams as before and then cancels its in-flight requests withServerSession#cancel_all_incomingand the reason"Session terminated". Handlers observe it as they wouldnotifications/cancelled, and their responses are suppressed the same way. The cancellations run once@mutexis released, as the stream closes already do:Cancellation#cancelfires its hooks synchronously, and a hook may call back into the transport, whose lock is not reentrant. The TypeScript SDK does the same inProtocol#_onclose, which aborts the handlers of every in-flight request when the transport closes.The Cancellation docs page now describes this.
How Has This Been Tested?
New tests in
test/mcp/server/transports/streamable_http_transport_test.rbhold a tool call in flight withQueuebarriers rather than sleeps:DELETEcancels the request with the reason"Session terminated", and its POST is answered 202 with no body.close, the reaper, a request, a notification, a targeted notification, a broadcast, aninitializereclaiming an expired slot, and a failed GET stream write each cancel the requests of the session they remove.All of them fail against
main, and skipping the cancellation on any one removal path fails the matching case.bundle exec rake test(2704 runs, 0 failures) andbundle exec rake rubocoppass on Ruby 3.4.4.Breaking Changes
A request still running when its session is removed no longer delivers its result. In JSON response mode its POST is answered 202 with no body, as for a request cancelled with
notifications/cancelled. Over SSE the termination already closed the POST stream, so the result was lost before as well. This includes a request that outlivessession_idle_timeout(30 minutes by default), since a session's idle time is counted from the start of its latest request.Types of changes
Checklist
Additional context
session_idle_timeoutloses its session mid-call. With this change that call ends as cancelled instead of finishing for a session that no longer exists. Should a session with requests in flight be exempt from expiry instead? I left the expiry rule as it is.DELETE, and registers itself only after the cancellation, still runs to completion. Closing that window would mean marking theServerSessionterminated so that later registrations start cancelled. I kept it out of this change.