From 7e4d28f173fd663cfbdcd84f7e064c5528b04c51 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Fri, 20 Nov 2026 16:51:35 +0800 Subject: [PATCH] fix: cancel a request that reaches a session as it ends ## 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 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. --- docs/_server/cancellation.md | 2 + lib/mcp/server.rb | 6 ++ lib/mcp/server_session.rb | 33 +++++++--- .../streamable_http_transport_test.rb | 65 +++++++++++++++++++ test/mcp/server_cancellation_test.rb | 35 ++++++++++ 5 files changed, 133 insertions(+), 8 deletions(-) diff --git a/docs/_server/cancellation.md b/docs/_server/cancellation.md index e0dff0cc..38c9c26a 100644 --- a/docs/_server/cancellation.md +++ b/docs/_server/cancellation.md @@ -113,6 +113,8 @@ the session with `DELETE`, when the session passes `session_idle_timeout`, when to resume from, and when the transport is closed. `server_context.cancelled?` turns true, `on_cancel` hooks registered on `server_context.cancellation` run with the reason `"Session terminated"`, and the response is suppressed, since no client is left to receive it. +A request that reaches the session as it ends, after the transport looked the session up but before the request started, +is not run at all. ## Nested Server-to-Client Requests Are Cancelled Automatically diff --git a/lib/mcp/server.rb b/lib/mcp/server.rb index 1da6e8fc..978634cf 100644 --- a/lib/mcp/server.rb +++ b/lib/mcp/server.rb @@ -829,6 +829,12 @@ def handle_request(request, method, session: nil, related_request_id: nil, auth_ server_context: { request: request }, exception_already_reported: ->(e) { reported_exception.equal?(e) }, ) do |instrumentation_data| + # Cancelled before its handler starts, as a request is when its session ended while it was being admitted. + if cancellation&.cancelled? + instrumentation_data.merge!(cancelled: true, cancellation_reason: cancellation.reason) + next JsonRpcHandler::NO_RESPONSE + end + refuse_non_object_meta!(params) envelope = lift_request_envelope(params, method: method, session: session) diff --git a/lib/mcp/server_session.rb b/lib/mcp/server_session.rb index a07429e8..c20b7ceb 100644 --- a/lib/mcp/server_session.rb +++ b/lib/mcp/server_session.rb @@ -27,6 +27,8 @@ def initialize(server:, transport:, session_id: nil, era: nil) @client_capabilities = nil @logging_message_notification = nil @protocol_version = nil + @incoming_cancelled = false + @incoming_cancel_reason = nil @in_flight = {} @in_flight_mutex = Mutex.new @initialized = false @@ -63,18 +65,28 @@ def lock_era!(era) # and `notifications/cancelled` back to the request that caused them, so a second live request under the same id # has no destination of its own. Rather than let the newcomer displace the registration, report the collision # and leave the first request intact; the caller turns that into an Invalid Request. + # + # Once `cancel_all_incoming` has run, the token comes back already cancelled with its reason and is not tracked: + # a transport can hand a request to a session it is ending, and that request must not outlive the session. def register_in_flight(request_id) return if request_id.nil? cancellation = Cancellation.new(request_id: request_id) - registered = @in_flight_mutex.synchronize do - next false if @in_flight.key?(request_id) - - @in_flight[request_id] = cancellation - true + cancel_reason = nil + admission = @in_flight_mutex.synchronize do + if @incoming_cancelled + cancel_reason = @incoming_cancel_reason + :cancelled + elsif @in_flight.key?(request_id) + nil + else + @in_flight[request_id] = cancellation + :registered + end end - registered ? cancellation : nil + cancellation.cancel(reason: cancel_reason) if admission == :cancelled + cancellation if admission end # Removes an in-flight registration. Passing the `Cancellation` that `register_in_flight` returned removes @@ -108,9 +120,14 @@ def cancel_incoming(request_id:, reason: nil) # Flips the `Cancellation` of every request still in flight, for a transport ending the session they belong to: # their handlers observe it as they would `notifications/cancelled`, and their responses are suppressed the same way. - # The cancellations run outside the registry lock, since each fires its `on_cancel` hooks synchronously. + # Requests registered afterwards are cancelled as they register. The cancellations run outside the registry lock, + # since each fires its `on_cancel` hooks synchronously. def cancel_all_incoming(reason: nil) - cancellations = @in_flight_mutex.synchronize { @in_flight.values } + cancellations = @in_flight_mutex.synchronize do + @incoming_cancelled = true + @incoming_cancel_reason = reason + @in_flight.values + end cancellations.each { |cancellation| cancellation.cancel(reason: reason) } end diff --git a/test/mcp/server/transports/streamable_http_transport_test.rb b/test/mcp/server/transports/streamable_http_transport_test.rb index d6df2192..e5af7615 100644 --- a/test/mcp/server/transports/streamable_http_transport_test.rb +++ b/test/mcp/server/transports/streamable_http_transport_test.rb @@ -4084,6 +4084,62 @@ def string transport&.close end + test "an SSE request whose session is terminated before its body runs does not run its tool" do + server = Server.new(name: "test", tools: [], prompts: [], resources: []) + transport = StreamableHTTPTransport.new(server) + server.transport = transport + invocations = 0 + server.define_tool(name: "effect") do |server_context:| + invocations += 1 + Tool::Response.new([{ type: "text", text: "done" }]) + end + session_id, = start_session(transport) + + status, _, body = transport.handle_request(effect_tool_call(session_id, "call-1")) + transport.handle_request(create_rack_request("DELETE", "/", { "HTTP_MCP_SESSION_ID" => session_id })) + stream = TestStream.new + body.call(stream) + + assert_equal(200, status) + assert_equal(0, invocations) + assert_empty(stream.string) + ensure + transport&.close + end + + test "a JSON request whose session is terminated before it registers does not run its tool" do + server = Server.new(name: "test", tools: [], prompts: [], resources: []) + transport = StreamableHTTPTransport.new(server, enable_json_response: true) + server.transport = transport + invocations = 0 + server.define_tool(name: "effect") do |server_context:| + invocations += 1 + Tool::Response.new([{ type: "text", text: "done" }]) + end + session_id, server_session = start_session(transport) + looked_up = Queue.new + resume = Queue.new + # Holds the request after the transport has looked its session up and before `Server` registers it, + # which is where a `DELETE` from another connection can land. + server_session.define_singleton_method(:handle_json) do |*args, **kwargs| + looked_up << true + resume.pop + super(*args, **kwargs) + end + + call = Thread.new { transport.handle_request(effect_tool_call(session_id, "call-1")) } + looked_up.pop + transport.handle_request(create_rack_request("DELETE", "/", { "HTTP_MCP_SESSION_ID" => session_id })) + resume << true + + assert_equal(202, call.value[0]) + assert_equal(0, invocations) + ensure + resume&.push(true) + call&.join + transport&.close + end + test "every other path that removes a session cancels the requests in flight in it" do expire = ->(transport, session_id) { transport.instance_variable_get(:@sessions)[session_id][:last_active_at] -= 61 } session_post = ->(session_id, body) do @@ -8875,6 +8931,15 @@ def define_held_tool(server, cancellations:, started:, release:) end end + def effect_tool_call(session_id, request_id) + create_rack_request( + "POST", + "/", + { "CONTENT_TYPE" => "application/json", "HTTP_MCP_SESSION_ID" => session_id }, + { jsonrpc: "2.0", id: request_id, method: "tools/call", params: { name: "effect", arguments: {} } }.to_json, + ) + end + def held_tool_call(session_id, request_id) create_rack_request( "POST", diff --git a/test/mcp/server_cancellation_test.rb b/test/mcp/server_cancellation_test.rb index 99fd8cab..0dab0709 100644 --- a/test/mcp/server_cancellation_test.rb +++ b/test/mcp/server_cancellation_test.rb @@ -353,6 +353,41 @@ def handle_request(request); end assert_nil @session.lookup_in_flight("req-1") end + test "register_in_flight after cancel_all_incoming returns a token already cancelled with the same reason" do + @session.cancel_all_incoming(reason: "Session terminated") + + cancellation = @session.register_in_flight("late-1") + + assert cancellation.cancelled? + assert_equal "Session terminated", cancellation.reason + refute @session.in_flight?("late-1"), "a request admitted after the session ended is not tracked" + end + + test "a request registered after cancel_all_incoming does not run its handler or get a response" do + invocations = 0 + recorded_data = nil + @server.configuration.instrumentation_callback = ->(data) { recorded_data = data } + @server.define_tool(name: "effect") do |server_context:| + invocations += 1 + Tool::Response.new([{ type: "text", text: "done" }]) + end + @session.cancel_all_incoming(reason: "Session terminated") + + response = @session.handle(jsonrpc: "2.0", id: "late-1", method: Methods::TOOLS_CALL, params: { name: "effect", arguments: {} }) + + assert_nil response + assert_equal 0, invocations + assert recorded_data[:cancelled] + assert_equal "Session terminated", recorded_data[:cancellation_reason] + end + + test "cancel_all_incoming on one session leaves registration on another session open" do + other = ServerSession.new(server: @server, transport: @mock_transport, session_id: "sess-2") + @session.cancel_all_incoming(reason: "Session terminated") + + refute other.register_in_flight("req-1").cancelled? + end + test "in_flight? reports whether an id is registered" do refute @session.in_flight?("req-1")