Skip to content

fix: cancel a request that reaches a session as it ends - #698

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

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

Conversation

@yorzi

@yorzi yorzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

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 Server has not registered yet, is missed: ServerSession#cancel_all_incoming cancels 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 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 (lib/mcp/server/transports/streamable_http_transport.rb:2537). A DELETE from 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 a DELETE between the tools/call response and its body:

tools/call answered 200, body not yet run
DELETE answered 200
tool ran, cancelled? = false
SSE body wrote: "data: {\"jsonrpc\":\"2.0\",\"id\":2,\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"done\"}],\"isError\":false}}\n\n"

With this change:

tools/call answered 200, body not yet run
DELETE answered 200
SSE body wrote: ""

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 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, so on_cancel hooks 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:

  • 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:

  • A registration after cancel_all_incoming returns a token already cancelled with its reason, and it is not tracked.
  • Such a request does not run its handler, gets no response, and records the cancellation in the instrumentation.
  • cancel_all_incoming on 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 in Server, 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), and bundle 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

  • 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

This merges cleanly with #695 and #697, and the full suite passes on main with 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.

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

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