Skip to content

fix: refuse a request whose session ends while it is admitted - #705

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

yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/stateful-session-admission-race

Conversation

@yorzi

@yorzi yorzi commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Follows #698: a request whose session is removed between the transport's session check and its session lookup is answered 404 instead of running against the shared Server.

Fixes #704

Motivation and Context

A stateful Streamable HTTP request checks its session in one @mutex section (validate_and_touch_session) and looks the session's ServerSession up in another (lib/mcp/server/transports/streamable_http_transport.rb:2481). A DELETE from another connection, or close, can remove the session between the two. The lookup then finds nothing, and dispatch_handle_json falls back to the shared Server (:2601): the tool runs with no session, so the termination that cancels the session's requests (#692, #698) never reaches it, server_context.cancelled? stays false, and the request is answered 200 with its result in both response modes. Notifications have the same split between session_active? (:1948) and the lookup in dispatch_notification, so a custom notification handler runs against the shared Server and the POST is answered 202.

On main (00868f7), holding the request with a Queue barrier just after its session check and sending DELETE before releasing it:

JSON: DELETE=200 POST=200 tool_calls(cancelled?)=[false] body="{\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{...\"text\":\"ran\"...}}"
SSE: DELETE=200 POST=200 tool_calls(cancelled?)=[false] body="data: {\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{...\"text\":\"ran\"...}}\n\n"
NOTIFICATION: DELETE=200 POST=202 handler_calls=1

A request or notification that names a session and finds none at the lookup is now answered 404 Session not found, the answer it gets when the session is already gone at the check, and is not dispatched. Stateless requests and requests sent without a session id (such as a sessionless server/discover) still go to the shared Server as before. The ownership check, which runs before both steps, is unchanged.

How Has This Been Tested?

New tests in test/mcp/server/transports/streamable_http_transport_test.rb hold a request with a Queue barrier right after its session check passes, terminate the session with DELETE, then release it:

  • A tools/call in SSE mode is answered 404 and does not run its tool.
  • The same in JSON response mode.
  • A custom notification is answered 404 and does not run its handler.

All three fail against the previous library (200, 200, 202). Dropping the request guard, the notification guard, or the caller's use of the notification result each fails a test. bundle exec rake test, bundle exec rake rubocop and bundle exec rake conformance:test (baseline) pass locally on Ruby 4.0.9.

Breaking Changes

None. A request that loses its session while it is admitted is answered 404 instead of running without the session.

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 (none needed: the docs already describe 404 for an unknown session, and this makes the late case match)

Additional context

dispatch_notification now returns whether it dispatched, so its caller can answer 404; an exception inside the dispatch is still reported and answered 202 as before. I kept the check and the lookup as two steps rather than merging validate_and_touch_session with the lookup, because the GET path shares that helper and only needs the check.

## Motivation and Context

A stateful Streamable HTTP request checks its session in one `@mutex` section (`validate_and_touch_session`) and looks
the session's `ServerSession` up in another. A `DELETE` from another connection, or `close`, can remove the session
between the two. The lookup then finds nothing, and `dispatch_handle_json` falls back to the shared `Server`: the tool
runs with no session, so the termination that cancels the session's requests (modelcontextprotocol#692, modelcontextprotocol#698) never reaches it,
`server_context.cancelled?` stays false, and the request is answered 200 with its result in both response modes.
Notifications have the same split between `session_active?` and the lookup in `dispatch_notification`, so a custom
notification handler runs against the shared `Server` and the POST is answered 202.

A request or notification that names a session and finds none at the lookup is now answered 404 Session not found, the
answer it gets when the session is already gone at the check, and is not dispatched. Stateless requests and requests
sent without a session id are unchanged.

## How Has This Been Tested?

New tests in `test/mcp/server/transports/streamable_http_transport_test.rb` hold a request with a `Queue` barrier right
after its session check passes, terminate the session with `DELETE`, then release it: a `tools/call` in SSE mode and in
JSON response mode is answered 404 and does not run its tool, and a custom notification is answered 404 and does not
run its handler. All three fail against the previous library. Dropping the request guard, the notification guard, or
the caller's use of the notification result each fails a test.

## Breaking Changes

None. A request that loses its session while being admitted is answered 404 instead of running without the session.

Fixes modelcontextprotocol#704.

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.

A request whose session is terminated while it is admitted runs against the shared Server

1 participant