Skip to content

Commit 157ab51

Browse files
authored
Merge pull request #596 from koic/close_a_failed_listen_stream_from_the_path_that_removed_it
Close a failed listen stream only from the path that removed its entry
2 parents 0019b4d + 6d40d4c commit 157ab51

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
@@ -1207,14 +1207,11 @@ def listen_sse_body(request_id, honored, acknowledgement, auth_info = nil)
12071207
true
12081208
end
12091209

1210-
if acknowledged
1211-
start_listen_keepalive_thread(subscription_key, request_id)
1212-
else
1213-
close_removed_listen_stream(subscription)
1214-
end
1210+
# An acknowledgement skipped because the entry was marked closed leaves the stream to the path that marked
1211+
# it, the teardown, which closes the stream itself once it holds the write mutex.
1212+
start_listen_keepalive_thread(subscription_key, request_id) if acknowledged
12151213
rescue *STREAM_WRITE_ERRORS
1216-
remove_listen_subscription(subscription_key)
1217-
close_removed_listen_stream(subscription)
1214+
close_removed_listen_stream(subscription) if remove_listen_subscription(subscription_key)
12181215
end
12191216
end
12201217
end
@@ -1425,8 +1422,9 @@ def deliver_to_listen_subscriptions(method, params)
14251422
e,
14261423
{ subscription_id: subscription[:request_id], error: "Failed to send notification" },
14271424
)
1428-
remove_listen_subscription(subscription_key)
1429-
close_removed_listen_stream(subscription)
1425+
# Closed only when the removal is this path's own, as every other removal path does: the keepalive loop,
1426+
# a token expiry, or the teardown that removed the entry first closes the stream itself.
1427+
close_removed_listen_stream(subscription) if remove_listen_subscription(subscription_key)
14301428
end
14311429
end
14321430
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)
@@ -7184,6 +7234,36 @@ def wait_until_blocked_or_done(thread)
71847234
assert_equal 2, sse_events(second).count { |event| event["method"] == "notifications/tools/list_changed" }
71857235
end
71867236

7237+
test "a delivery whose write fails leaves closing the stream to the path that removed its entry" do
7238+
io = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })
7239+
registry = @transport.instance_variable_get(:@listen_subscriptions)
7240+
subscription_key, entry = registry.first
7241+
transport = @transport
7242+
closes = 0
7243+
7244+
io.define_singleton_method(:close) do
7245+
closes += 1
7246+
super()
7247+
end
7248+
7249+
# The peer went away: the keepalive loop removed the entry a moment before this write failed on the same stream.
7250+
io.define_singleton_method(:write) do |_data|
7251+
transport.send(:remove_listen_subscription, subscription_key)
7252+
raise Errno::EPIPE
7253+
end
7254+
7255+
@server.notify_tools_list_changed
7256+
7257+
assert_equal 0, closes, "the delivery closed a stream whose removal belonged to another path"
7258+
assert_empty registry
7259+
7260+
# The path that removed the entry closes the stream, once.
7261+
@transport.send(:close_removed_listen_stream, entry)
7262+
7263+
assert_equal 1, closes
7264+
assert_predicate io, :closed?
7265+
end
7266+
71877267
test "a delivery that took an entry before its removal does not write to the removed stream" do
71887268
io = open_listen_stream(id: "listen-1", notifications: { toolsListChanged: true })
71897269
registry = @transport.instance_variable_get(:@listen_subscriptions)

0 commit comments

Comments
 (0)