Skip to content

Close a failed listen stream only from the path that removed its entry - #596

Open
koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:close_a_failed_listen_stream_from_the_path_that_removed_it
Open

koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:close_a_failed_listen_stream_from_the_path_that_removed_it

Conversation

@koic

@koic koic commented Oct 12, 2026

Copy link
Copy Markdown
Member

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.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

## 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant