From e06cc0c4ec18086dec86489ab107227ce8b979fc Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 3 Dec 2026 15:27:37 +0900 Subject: [PATCH] Keep a delivery going when the exception reporter raises for one stream ## Motivation and Context A notification is written to one stream after another, and a write that fails on a stream is reported through the exception reporter before that stream is removed. A reporter that raised there ended the whole delivery: the failed stream stayed registered, the streams the delivery had not reached yet never received the notification, and the exception went to the code that emitted the change. The report of a failed stream write now ignores a reporter that raises, on both paths that write to several streams: the `subscriptions/listen` streams and the streams of the sessions. The failed stream is removed as before, and the delivery continues with the next one. The second path is `store_and_write`, which also writes a message for a single session and the response of a request, so a failed write is handled the same way there: the stream is dropped and the caller is told that nothing was written. The other reports of the transport are unchanged. ## How Has This Been Tested? With two new tests in `streamable_http_transport_test.rb`, one per path: of two streams the first is broken, and the reporter raises. Both fail against the previous tree with the reporter's exception. ## Breaking Changes None. --- .../transports/streamable_http_transport.rb | 16 ++++++--- .../streamable_http_transport_test.rb | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/mcp/server/transports/streamable_http_transport.rb b/lib/mcp/server/transports/streamable_http_transport.rb index 19c33187..fdfb7bcb 100644 --- a/lib/mcp/server/transports/streamable_http_transport.rb +++ b/lib/mcp/server/transports/streamable_http_transport.rb @@ -1903,10 +1903,8 @@ def deliver_to_listen_subscriptions(method, params) push_to_stream(subscription[:stream], notification, subscription: subscription) end rescue *STREAM_WRITE_ERRORS => e - MCP.configuration.exception_reporter.call( - e, - { subscription_id: subscription[:request_id], error: "Failed to send notification" }, - ) + report_stream_write_failure(e, { subscription_id: subscription[:request_id], error: "Failed to send notification" }) + # Closed only when the removal is this path's own, as every other removal path does: the keepalive loop, # a token expiry, or the teardown that removed the entry first closes the stream itself. close_removed_listen_stream(subscription) if remove_listen_subscription(subscription_key) @@ -1914,6 +1912,14 @@ def deliver_to_listen_subscriptions(method, params) end end + # Reports a write that failed on one stream of a delivery. A reporter that raises is ignored here, unlike elsewhere in the transport: + # the exception would leave the failed stream registered and cost the streams the delivery has not reached yet the message. + def report_stream_write_failure(exception, context) + MCP.configuration.exception_reporter.call(exception, context) + rescue StandardError + nil + end + def remove_listen_subscription(subscription_key) @mutex.synchronize do subscription = @listen_subscriptions.delete(subscription_key) @@ -3361,7 +3367,7 @@ def store_and_write(session_id, related_request_id, json, pushed: true) written = true rescue *STREAM_WRITE_ERRORS => e - MCP.configuration.exception_reporter.call(e, { session_id: session_id, error: "Failed to send message" }) + report_stream_write_failure(e, { session_id: session_id, error: "Failed to send message" }) drop_broken_stream(session_id, stream, related_request_id) end diff --git a/test/mcp/server/transports/streamable_http_transport_test.rb b/test/mcp/server/transports/streamable_http_transport_test.rb index 2ea899d4..d05d441b 100644 --- a/test/mcp/server/transports/streamable_http_transport_test.rb +++ b/test/mcp/server/transports/streamable_http_transport_test.rb @@ -8704,6 +8704,42 @@ def wait_until_blocked_or_done(thread) assert_equal "listen-2", second_events[1].dig("params", "_meta", "io.modelcontextprotocol/subscriptionId") end + test "an exception reporter that raises for a failed listen stream does not cost the other streams the notification" do + broken = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true }) + healthy = open_listen_stream(id: "listen-2", notifications: { toolsListChanged: true }) + broken.close + + original_reporter = MCP.configuration.exception_reporter + MCP.configuration.exception_reporter = ->(_error, _context) { raise "reporter boom" } + begin + assert_nothing_raised { @transport.send_notification("notifications/tools/list_changed") } + ensure + MCP.configuration.exception_reporter = original_reporter + end + + assert_equal "notifications/tools/list_changed", sse_events(healthy).last["method"] + assert_equal ["listen-2"], @transport.instance_variable_get(:@listen_subscriptions).values.map { |entry| entry[:request_id] } + end + + test "an exception reporter that raises for a failed GET stream does not cost the other sessions the notification" do + broken = StringIO.new + healthy = StringIO.new + broken_session_id = open_sse_session(@transport, io: broken) + open_sse_session(@transport, io: healthy) + broken.close + + original_reporter = MCP.configuration.exception_reporter + MCP.configuration.exception_reporter = ->(_error, _context) { raise "reporter boom" } + begin + assert_nothing_raised { @transport.send_notification("notifications/tools/list_changed") } + ensure + MCP.configuration.exception_reporter = original_reporter + end + + assert_equal "notifications/tools/list_changed", sse_events(healthy).last["method"] + refute @transport.instance_variable_get(:@sessions).key?(broken_session_id) + end + test "subscriptions/listen closes gracefully with a SubscriptionsListenResult on transport close" do io = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })