Skip to content

Release a session in full when cancelling one of its requests raises - #721

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
koic:release_a_session_in_full_when_cancelling_one_of_its_requests_raises
Nov 27, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
koic:release_a_session_in_full_when_cancelling_one_of_its_requests_raises

Conversation

@koic

@koic koic commented Nov 27, 2026

Copy link
Copy Markdown
Member

Motivation and Context

Ending a session cancels its requests in flight, wakes the threads waiting on its server-to-client requests, and closes its streams. A cancellation runs the on_cancel hooks of the request, and a hook that raises is handed to the exception reporter; when the reporter raises as well, the error leaves the cancellation.

For a session ended on its own (DELETE, or a request that finds it expired), that error skipped everything after it: the requests not yet cancelled kept running, the waits were left to their timeout, and the streams stayed open, although the session was already gone from the registry and nothing would come back for them. Sessions that end together already had their streams closed whatever the cancellations did, but the sessions after the one that raised were not cancelled.

Now such an error (a StandardError) no longer stops the rest: every request of every session is still cancelled, the waits are woken and the streams are closed, and the first error is raised once that is done.

How Has This Been Tested?

test/mcp/server/transports/streamable_http_transport_test.rb. Against the previous library the second request of the session is not cancelled.

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

Ending a session cancels its requests in flight, wakes the threads waiting on its server-to-client requests, and
closes its streams. A cancellation runs the `on_cancel` hooks of the request, and a hook that raises is handed to
the exception reporter; when the reporter raises as well, the error leaves the cancellation.

For a session ended on its own (`DELETE`, or a request that finds it expired), that error skipped everything after
it: the requests not yet cancelled kept running, the waits were left to their timeout, and the streams stayed open,
although the session was already gone from the registry and nothing would come back for them. Sessions that end
together already had their streams closed whatever the cancellations did, but the sessions after the one that
raised were not cancelled.

Now such an error (a `StandardError`) no longer stops the rest: every request of every session is still cancelled,
the waits are woken and the streams are closed, and the first error is raised once that is done.

## How Has This Been Tested?

`test/mcp/server/transports/streamable_http_transport_test.rb`. Against the previous library the second request of
the session is not cancelled.

## Breaking Changes

None.
@koic
koic merged commit 34cadfe into modelcontextprotocol:main Nov 27, 2026
13 checks passed
@koic
koic deleted the release_a_session_in_full_when_cancelling_one_of_its_requests_raises branch November 27, 2026 19:50
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