Skip to content

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

Closed

Description

@yorzi

Describe the bug

A stateful Streamable HTTP request checks its session (validate_and_touch_session, lib/mcp/server/transports/streamable_http_transport.rb:2609) and then looks up its ServerSession in a second @mutex section (:2481). When a DELETE from another connection (or close) removes the session between the two, the lookup returns nil and dispatch_handle_json (:2601) falls back to the shared @server.handle_json. The tool runs with no session, so the session termination that #692 and #698 use to cancel the session's requests never reaches it, and the request is answered 200 with its result. Notifications have the same split between session_active? (:1948) and the lookup in dispatch_notification (:2338): a custom notification handler runs against the shared Server and the POST is answered 202.

To Reproduce

On main (00868f7), hold the request with a Queue barrier just after validate_and_touch_session (or session_active? for a notification) returns, send DELETE for the session, then release it:

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

Expected behavior

A request that names a session and finds none when it looks the session up is answered 404 Session not found, as it is when the session is already gone at the check, and is not dispatched.

Logs

See above.

Additional context

The barrier only widens a window that exists between the two @mutex sections; a multi-threaded server can hit it without one. The effect is a handler running for a session that has ended, outside its cancellation; it does not skip the ownership check, which runs before both.

I have a fix with tests and will open a PR for it.

Activity

  1. added
    bug
    on Nov 22, 2026
  2. added a commit that references this issue on Nov 25, 2026
    2aff76c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions