Skip to content

A terminated session's requests run or fail uncancelled while its streams close #708

Closed

Description

@yorzi

Describe the bug

When StreamableHTTPTransport removes a stateful session, cleanup_session_unsafe wakes the threads waiting on the session's server-to-client requests with :session_closed (lib/mcp/server/transports/streamable_http_transport.rb:2053), and release_session (:2071) then closes the GET and POST streams before it calls cancel_all_incoming. A close can take up to stream_write_timeout: (10 seconds by default, :2092) on a stalled peer, and the cancellation waits behind it. Meanwhile:

  • a request that reaches the session as it ends, which fix: cancel a request that reaches a session as it ends #698 made not run, is admitted and run, and over SSE its result is written to its POST stream after the DELETE;
  • a request waiting on a nested request is woken with :session_closed before its own token is cancelled, so the nested call raises "SSE session closed while waiting" (:848), the handler sees server_context.cancelled? false, and the request is reported as an exception and recorded as an internal error rather than a cancellation. This one happens even without a stalled stream, depending on thread scheduling.

To Reproduce

On main (00868f7), with a GET stream whose close blocks until released, standing in for a stalled peer:

(a) tools/call answered before DELETE, its SSE body run while DELETE is blocked closing the GET stream:
    DELETE=200 tool_calls(cancelled?)=[false] written="data: {\"jsonrpc\":\"2.0\",\"id\":9,\"result\":{...\"text\":\"ran\"...}}\n\n"
(b) tools/call waiting on elicitation/create when the session is deleted:
    handler saw=[["RuntimeError", false]]
    instrumentation(tools/call)=[{method: "tools/call", error: :internal_error}]
    reported=["RuntimeError: SSE session closed while waiting for elicitation/create response."]

Expected behavior

What the Cancellation docs page states: a request that reaches the session as it ends is not run, and a nested call of a cancelled request raises MCP::CancelledError, whatever the time the streams take to close.

Logs

See above.

Additional context

Cancelling first and closing the streams afterwards removes both windows; the cancellations still run outside the transport lock.

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