@@ -169,7 +169,8 @@ def initialize(
169169 @allowed_origins = Array ( allowed_origins ) . map ( &:downcase ) . freeze
170170 @pending_responses = { }
171171
172- # Maps a `subscriptions/listen` request id to `{ stream: stream_object, filter: honored_subscription_filter, active: boolean }` (SEP-2575).
172+ # Maps a `subscriptions/listen` request id to
173+ # `{ stream: stream_object, filter: honored_subscription_filter, active: boolean, write_mutex: Mutex }` (SEP-2575).
173174 # In-process only; a multi-worker deployment needs an external event bus to fan notifications out across processes,
174175 # which is a follow-up.
175176 @listen_subscriptions = { }
@@ -936,7 +937,7 @@ def listen_sse_body(request_id, honored)
936937 ( @max_listen_subscriptions && @listen_subscriptions . size >= @max_listen_subscriptions )
937938 rejected = true
938939 else
939- @listen_subscriptions [ request_id ] = { stream : stream , filter : honored , active : false }
940+ @listen_subscriptions [ request_id ] = { stream : stream , filter : honored , active : false , write_mutex : Mutex . new }
940941 end
941942 end
942943
@@ -1066,24 +1067,32 @@ def deliver_to_listen_subscriptions(method, params)
10661067 uris . is_a? ( Array ) && uris . include? ( uri )
10671068 end
10681069
1069- [ request_id , subscription [ :stream ] ] if hit
1070+ [ request_id , subscription ] if hit
10701071 end
10711072 end
10721073
1073- matched . each do |request_id , stream |
1074+ matched . each do |request_id , subscription |
10741075 meta = { RequestEnvelope ::SUBSCRIPTION_ID_META_KEY . to_sym => request_id }
10751076 notification_params = ( params || { } ) . merge ( _meta : meta )
10761077 notification = { jsonrpc : "2.0" , method : method , params : notification_params }
10771078
10781079 begin
1079- send_to_stream ( stream , notification )
1080+ # The per-stream write mutex orders this write against a concurrent graceful teardown:
1081+ # once teardown has marked the entry closed and written its `SubscriptionsListenResult`,
1082+ # a delivery that snapshotted the entry before the registry was cleared skips it instead
1083+ # of writing after the final message.
1084+ subscription [ :write_mutex ] . synchronize do
1085+ next if subscription [ :closed ]
1086+
1087+ send_to_stream ( subscription [ :stream ] , notification )
1088+ end
10801089 rescue *STREAM_WRITE_ERRORS => e
10811090 MCP . configuration . exception_reporter . call (
10821091 e ,
10831092 { subscription_id : request_id , error : "Failed to send notification" } ,
10841093 )
10851094 remove_listen_subscription ( request_id )
1086- close_stream_safely ( stream )
1095+ close_stream_safely ( subscription [ : stream] )
10871096 end
10881097 end
10891098 end
@@ -1102,20 +1111,27 @@ def teardown_listen_subscriptions
11021111 end
11031112
11041113 removed . each do |request_id , subscription |
1105- begin
1106- send_to_stream ( subscription [ :stream ] , {
1107- jsonrpc : "2.0" ,
1108- id : request_id ,
1109- result : {
1110- # `SubscriptionsListenResult` is served at the transport layer and never
1111- # passes through the dispatch path, so the REQUIRED 2026-07-28 `resultType` is
1112- # stamped at its construction site.
1113- resultType : ResultType ::COMPLETE ,
1114- _meta : { RequestEnvelope ::SUBSCRIPTION_ID_META_KEY . to_sym => request_id } ,
1115- } ,
1116- } )
1117- rescue *STREAM_WRITE_ERRORS
1118- nil
1114+ # Marking the entry closed and writing the result under the stream's write mutex orders
1115+ # this against in-flight deliveries: each one either lands before the result or observes
1116+ # `closed` and skips, keeping the graceful result the stream's final message.
1117+ subscription [ :write_mutex ] . synchronize do
1118+ subscription [ :closed ] = true
1119+
1120+ begin
1121+ send_to_stream ( subscription [ :stream ] , {
1122+ jsonrpc : "2.0" ,
1123+ id : request_id ,
1124+ result : {
1125+ # `SubscriptionsListenResult` is served at the transport layer and never
1126+ # passes through the dispatch path, so the REQUIRED 2026-07-28 `resultType` is
1127+ # stamped at its construction site.
1128+ resultType : ResultType ::COMPLETE ,
1129+ _meta : { RequestEnvelope ::SUBSCRIPTION_ID_META_KEY . to_sym => request_id } ,
1130+ } ,
1131+ } )
1132+ rescue *STREAM_WRITE_ERRORS
1133+ nil
1134+ end
11191135 end
11201136 close_stream_safely ( subscription [ :stream ] )
11211137 end
0 commit comments