Skip to content

Keep a session with a request in flight from idle expiry - #695

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

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

Conversation

@koic

@koic koic commented Nov 20, 2026

Copy link
Copy Markdown
Member

Motivation and Context

StreamableHTTPTransport counted a session's idle time from the start of its latest request, so a request that ran longer than session_idle_timeout lost its session while the client was still waiting for the response. Removing the session closes its streams, which dropped the response over SSE, and since #692 it also cancels the requests in flight, so in JSON response mode the POST was answered 202 with no body instead of the result.

A session is now idle only while none of its requests is running, and its idle time counts from the later of the last activity that touched it and the end of its last request. ServerSession records when a request leaves the in-flight registry, and the transport reads that together with whether any request is left.

With that, a session that expires has no request to cancel: the cancellation #692 added applies when the client ends the session with DELETE, when its GET stream breaks without an event store, and when the transport is closed. The tests of #692 that expired a session with a request in flight now assert that it stays, and the docs say so.

How Has This Been Tested?

test/mcp/server/transports/streamable_http_transport_test.rb; the test that lets the reaper, a request, a notification, a broadcast and an initialize check a session whose request outlived the timeout fails on the previous library, where the request is cancelled.

Breaking Changes

None. A session whose request outlives session_idle_timeout is kept until the request ends.

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` counted a session's idle time from the start of its latest request, so a request that ran
longer than `session_idle_timeout` lost its session while the client was still waiting for the response.
Removing the session closes its streams, which dropped the response over SSE, and since modelcontextprotocol#692 it also cancels
the requests in flight, so in JSON response mode the POST was answered 202 with no body instead of the result.

A session is now idle only while none of its requests is running, and its idle time counts from the later of
the last activity that touched it and the end of its last request. `ServerSession` records when a request leaves
the in-flight registry, and the transport reads that together with whether any request is left.

With that, a session that expires has no request to cancel: the cancellation modelcontextprotocol#692 added applies when the client ends
the session with `DELETE`, when its GET stream breaks without an event store, and when the transport is closed.
The tests of modelcontextprotocol#692 that expired a session with a request in flight now assert that it stays, and the docs say so.

## How Has This Been Tested?

`test/mcp/server/transports/streamable_http_transport_test.rb`; the test that lets the reaper, a request,
a notification, a broadcast and an `initialize` check a session whose request outlived the timeout fails
on the previous library, where the request is cancelled.

## Breaking Changes

None. A session whose request outlives `session_idle_timeout` is kept until the request ends.

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