Skip to content

Skip the reaper join when close runs on the reaper thread - #637

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

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

Conversation

@koic

@koic koic commented Oct 28, 2026

Copy link
Copy Markdown
Member

Motivation and Context

StreamableHTTPTransport#close joins the session reaper and kills it if it is still alive after the wait. When close itself runs on the reaper thread, reached from a stream's close or from the exception reporter while a reap is in progress, that join raises ThreadError, the rescue around it treats the reaper as gone, and the kill that follows ends the reaper, which is the thread running close: the listen streams are never torn down and the sessions never cleaned up, with nothing reporting it.

close now joins and kills the reaper only when running on another thread. On the reaper itself it goes straight to the teardown, and the reaper leaves its loop on @closing once the reap that called close returns.

How Has This Been Tested?

A new test in test/mcp/server/transports/streamable_http_transport_test.rb reaps a session whose stream's close calls transport.close, and checks that the listen stream is torn down, the sessions are gone, and the reaper ends without an exception. Against the previous library it fails: the listen registry still holds its entry.

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

`StreamableHTTPTransport#close` joins the session reaper and kills it if it is still alive after the wait.
When `close` itself runs on the reaper thread, reached from a stream's `close` or from the exception reporter
while a reap is in progress, that join raises `ThreadError`, the rescue around it treats the reaper as gone,
and the kill that follows ends the reaper, which is the thread running `close`: the listen streams are never
torn down and the sessions never cleaned up, with nothing reporting it.

`close` now joins and kills the reaper only when running on another thread. On the reaper itself it goes
straight to the teardown, and the reaper leaves its loop on `@closing` once the reap that called `close` returns.

## How Has This Been Tested?

A new test in `test/mcp/server/transports/streamable_http_transport_test.rb` reaps a session whose stream's
`close` calls `transport.close`, and checks that the listen stream is torn down, the sessions are gone,
and the reaper ends without an exception. Against the previous library it fails: the listen registry still
holds its entry.

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