Skip to content

Cancel every session that ends before closing the streams of any - #712

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

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

Conversation

@koic

@koic koic commented Nov 23, 2026

Copy link
Copy Markdown
Member

Motivation and Context

#709 made release_session cancel a terminated session's requests and wake its waits before it closes the session's streams, since a close can block for up to stream_write_timeout: on a stalled peer. release_sessions, which close and the reaper use for sessions that end together, still released them one after the other, so each session waited for the closes of those ahead of it: its requests kept running uncancelled meanwhile, and its waits, which were woken under the lock before #709, now waited as well.

release_sessions now cancels the requests and wakes the waits of every session first, and closes the streams in a second pass. release_session does the same two steps for its one session. The second pass runs whatever the first one does, so an error while ending the requests leaves no stream open.

How Has This Been Tested?

test/mcp/server/transports/streamable_http_transport_test.rb; the test in which a session is cancelled while the stream of the one ahead of it is still closing times out on the previous library.

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

modelcontextprotocol#709 made `release_session` cancel a terminated session's requests and wake its waits before it closes
the session's streams, since a close can block for up to `stream_write_timeout:` on a stalled peer.
`release_sessions`, which `close` and the reaper use for sessions that end together, still released them one after the other,
so each session waited for the closes of those ahead of it: its requests kept running uncancelled meanwhile, and its waits,
which were woken under the lock before modelcontextprotocol#709, now waited as well.

`release_sessions` now cancels the requests and wakes the waits of every session first, and closes the streams in a second pass.
`release_session` does the same two steps for its one session. The second pass runs whatever the first one does,
so an error while ending the requests leaves no stream open.

## How Has This Been Tested?

`test/mcp/server/transports/streamable_http_transport_test.rb`; the test in which a session is cancelled while
the stream of the one ahead of it is still closing times out on the previous library.

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