Skip to content

Commit 6d40d4c

Browse files
committed
Close a failed listen stream only from the path that removed its entry
## Motivation and Context When a `subscriptions/listen` stream's write fails, in notification delivery or in the acknowledgement write, `StreamableHTTPTransport` removes the stream's registry entry and closes the stream. The close ran whether or not the removal was this path's own: when the keepalive loop, a token expiry, or the transport teardown had removed the entry a moment earlier, that path closes the stream too, and the failed write closed it a second time. Every other removal path closes the stream only when it was the one to remove the entry. The two write failure paths now do the same, so a stream is closed exactly once, by the path that removed it. An acknowledgement skipped because the teardown marked the entry closed first went the same way, closing a stream the teardown closes itself; that close is gone too. ## How Has This Been Tested? A new test in `test/mcp/server/transports/streamable_http_transport_test.rb` fails a delivery write on a stream whose entry another path removed just before, and counts the stream's closes: the delivery closes it once against the previous library, and leaves it to the remover now. A second test has the teardown mark an entry closed while its acknowledgement waits on the write mutex, and counts one close instead of the two the previous library made. ## Breaking Changes None.
1 parent a391d32 commit 6d40d4c

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

‎lib/mcp/server/transports/streamable_http_transport.rb‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,14 +1204,11 @@ def listen_sse_body(request_id, honored, acknowledgement, auth_info = nil)
12041204
true
12051205
end
12061206

1207-
if acknowledged
1208-
start_listen_keepalive_thread(subscription_key, request_id)
1209-
else
1210-
close_removed_listen_stream(subscription)
1211-
end
1207+
# An acknowledgement skipped because the entry was marked closed leaves the stream to the path that marked
1208+
# it, the teardown, which closes the stream itself once it holds the write mutex.
1209+
start_listen_keepalive_thread(subscription_key, request_id) if acknowledged
12121210
rescue *STREAM_WRITE_ERRORS
1213-
remove_listen_subscription(subscription_key)
1214-
close_removed_listen_stream(subscription)
1211+
close_removed_listen_stream(subscription) if remove_listen_subscription(subscription_key)
12151212
end
12161213
end
12171214
end
@@ -1422,8 +1419,9 @@ def deliver_to_listen_subscriptions(method, params)
14221419
e,
14231420
{ subscription_id: subscription[:request_id], error: "Failed to send notification" },
14241421
)
1425-
remove_listen_subscription(subscription_key)
1426-
close_removed_listen_stream(subscription)
1422+
# Closed only when the removal is this path's own, as every other removal path does: the keepalive loop,
1423+
# a token expiry, or the teardown that removed the entry first closes the stream itself.
1424+
close_removed_listen_stream(subscription) if remove_listen_subscription(subscription_key)
14271425
end
14281426
end
14291427
end

‎test/mcp/server/transports/streamable_http_transport_test.rb‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6887,6 +6887,56 @@ def string
68876887
assert_predicate io, :closed?
68886888
end
68896889

6890+
test "transport close that marks an entry closed before its acknowledgement closes the stream once" do
6891+
# The acknowledgement waits on the stream's write mutex while the teardown marks the entry closed and closes the stream;
6892+
# the skipped acknowledgement then leaves the close to the teardown. The write mutex the body creates is swapped at
6893+
# registration for one the test can hold the acknowledgement on while letting the teardown through.
6894+
reached = Queue.new
6895+
release = Queue.new
6896+
first_call = true
6897+
write_mutex = Object.new
6898+
write_mutex.define_singleton_method(:synchronize) do |&block|
6899+
if first_call
6900+
first_call = false
6901+
reached.push(true)
6902+
release.pop
6903+
end
6904+
block.call
6905+
end
6906+
6907+
registry = Class.new(Hash) do
6908+
define_method(:[]=) do |key, subscription|
6909+
subscription[:write_mutex] = write_mutex
6910+
super(key, subscription)
6911+
end
6912+
end.new
6913+
@transport.instance_variable_set(:@listen_subscriptions, registry)
6914+
6915+
io = StringIO.new
6916+
closes = 0
6917+
io.define_singleton_method(:close) do
6918+
closes += 1
6919+
super()
6920+
end
6921+
6922+
response = @transport.handle_request(modern_rack_request(
6923+
modern_listen_body(id: "listen-1", params: { notifications: { toolsListChanged: true } }),
6924+
))
6925+
body_thread = Thread.new { response[2].call(io) }
6926+
6927+
begin
6928+
wait_until { !reached.empty? }
6929+
@transport.close
6930+
ensure
6931+
release.push(true)
6932+
end
6933+
6934+
assert(body_thread.join(5), "the listen body did not finish")
6935+
assert_empty sse_events(io)
6936+
assert_predicate io, :closed?
6937+
assert_equal 1, closes
6938+
end
6939+
68906940
# Waits until `thread` is either blocked (asleep on a lock or queue) or finished, so the step after it
68916941
# runs against a thread that has made its move.
68926942
def wait_until_blocked_or_done(thread)
@@ -7061,6 +7111,36 @@ def wait_until_blocked_or_done(thread)
70617111
assert_equal 2, sse_events(second).count { |event| event["method"] == "notifications/tools/list_changed" }
70627112
end
70637113

7114+
test "a delivery whose write fails leaves closing the stream to the path that removed its entry" do
7115+
io = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })
7116+
registry = @transport.instance_variable_get(:@listen_subscriptions)
7117+
subscription_key, entry = registry.first
7118+
transport = @transport
7119+
closes = 0
7120+
7121+
io.define_singleton_method(:close) do
7122+
closes += 1
7123+
super()
7124+
end
7125+
7126+
# The peer went away: the keepalive loop removed the entry a moment before this write failed on the same stream.
7127+
io.define_singleton_method(:write) do |_data|
7128+
transport.send(:remove_listen_subscription, subscription_key)
7129+
raise Errno::EPIPE
7130+
end
7131+
7132+
@server.notify_tools_list_changed
7133+
7134+
assert_equal 0, closes, "the delivery closed a stream whose removal belonged to another path"
7135+
assert_empty registry
7136+
7137+
# The path that removed the entry closes the stream, once.
7138+
@transport.send(:close_removed_listen_stream, entry)
7139+
7140+
assert_equal 1, closes
7141+
assert_predicate io, :closed?
7142+
end
7143+
70647144
test "a delivery that took an entry before its removal does not write to the removed stream" do
70657145
io = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })
70667146
registry = @transport.instance_variable_get(:@listen_subscriptions)

0 commit comments

Comments
 (0)