You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A terminated session's requests run or fail uncancelled while its streams close #708
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 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
fix: cancel a terminated session's requests before closing its streams