Skip to content

fix: cancel in-flight requests when their session ends - #692

Open
yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/mcp-cancellation-session-race
Open

yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/mcp-cancellation-session-race

Conversation

@yorzi

@yorzi yorzi commented Nov 18, 2026

Copy link
Copy Markdown

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 for notifications/cancelled.

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.

On main (4451525), a tool call held in flight while its session is deleted:

DELETE answered 200
handler finished, cancelled? = false
tools/call answered 200 {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"done"}],"isError":false}}

With this change:

DELETE answered 200
on_cancel fired: "Session terminated"
handler finished, cancelled? = true
tools/call answered 202 (empty body)

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.

The Cancellation docs page now describes this.

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 rather than sleeps:

  • A DELETE cancels the request with the reason "Session terminated", and its POST is answered 202 with no body.
  • Terminating one session leaves the requests of another running, and they return their result.
  • 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.
  • The cancellation hooks run without the transport lock held.

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) and bundle exec rake rubocop pass 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 outlives session_idle_timeout (30 minutes by default), since a session's idle time is counted from the start of its latest request.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

  • The idle clock of a session runs from the start of its latest request, so a tool call longer than session_idle_timeout loses 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.
  • A request that fetched its session just before a concurrent DELETE, and registers itself only after the cancellation, still runs to completion. Closing that window would mean marking the ServerSession terminated so that later registrations start cancelled. I kept it out of this change.

## 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant