@@ -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