Skip to content

Add idle session timeout to StreamableHTTPSessionManager#2022

Draft
felixweinberger wants to merge 3 commits intomainfrom
fweinberger/idle-timeout-termination-main
Draft

Add idle session timeout to StreamableHTTPSessionManager#2022
felixweinberger wants to merge 3 commits intomainfrom
fweinberger/idle-timeout-termination-main

Conversation

@felixweinberger
Copy link
Contributor

@felixweinberger felixweinberger commented Feb 9, 2026

Port of #1994 (v1.x) to main.

Motivation and Context

Sessions created via StreamableHTTPSessionManager persist indefinitely in _server_instances even after the client disconnects, leaking memory over time (issue #1283).

The session_idle_timeout parameter automatically terminates and removes sessions that receive no HTTP requests for the configured duration. Each session manages its own lifetime via an anyio CancelScope deadline — no background reaper task needed. Incoming requests push the deadline forward to keep active sessions alive.

How Has This Been Tested?

Tests added to tests/server/test_streamable_http_manager.py: idle session reaping (deterministic, no sleeps), terminate idempotency, and parameterized input validation. All existing tests pass unchanged.

Breaking Changes

None — new optional parameter with default None.

Types of changes

  • New feature (non-breaking change which adds functionality)

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

@claude
Copy link

claude bot commented Feb 9, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

1 similar comment
@claude
Copy link

claude bot commented Feb 9, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@felixweinberger felixweinberger marked this pull request as draft February 16, 2026 12:23
Sessions created via StreamableHTTPSessionManager persist indefinitely
in _server_instances even after the client disconnects, leaking memory
over time.

Add a session_idle_timeout parameter that automatically terminates and
removes sessions that receive no HTTP requests for the configured
duration. Each session manages its own lifetime via an anyio CancelScope
deadline — no background reaper task needed. Incoming requests push the
deadline forward to keep active sessions alive.

Github-Issue: #1283
@felixweinberger felixweinberger force-pushed the fweinberger/idle-timeout-termination-main branch 7 times, most recently from fd6e745 to 482ff78 Compare February 16, 2026 12:47
@felixweinberger felixweinberger marked this pull request as ready for review February 16, 2026 12:51
@felixweinberger felixweinberger force-pushed the fweinberger/idle-timeout-termination-main branch from 482ff78 to 1b92691 Compare February 16, 2026 12:57
- Reformat session_idle_timeout docstring to use 4-space continuation
  indent and fill to 120 chars
- Move idle timeout tests from tests/issues/ into
  tests/server/test_streamable_http_manager.py alongside existing
  session manager tests
- Remove redundant/unnecessary tests (6 dropped, 5 kept)
- Replace sleep-based test assertions with event-based polling
  using anyio.fail_after for deterministic behavior

Github-Issue: #1283
@felixweinberger felixweinberger force-pushed the fweinberger/idle-timeout-termination-main branch from 1b92691 to 8aef039 Compare February 16, 2026 13:00
@felixweinberger felixweinberger marked this pull request as draft February 16, 2026 13:03
@felixweinberger felixweinberger force-pushed the fweinberger/idle-timeout-termination-main branch 3 times, most recently from 215a2d4 to 7f4e2ae Compare February 16, 2026 15:46
@felixweinberger felixweinberger force-pushed the fweinberger/idle-timeout-termination-main branch 2 times, most recently from 9155f4f to 2e8d1ab Compare February 16, 2026 15:51
- Fix docstring indentation: use 4-space continuation indent filled to
  120 cols consistently across all Args parameters
- Change stateless+idle_timeout error from ValueError to RuntimeError
- Remove unnecessary None guard on session_id in idle timeout cleanup
- Replace while+sleep(0) polling with anyio.Event in test

Github-Issue: #1283
@felixweinberger felixweinberger force-pushed the fweinberger/idle-timeout-termination-main branch from 2e8d1ab to 2721c9b Compare February 16, 2026 15:59
@felixweinberger felixweinberger marked this pull request as ready for review February 16, 2026 16:11
@felixweinberger felixweinberger marked this pull request as draft February 16, 2026 16:11
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.

2 participants