Skip to content

fix: terminate streamable http sessions on shutdown#2952

Open
fengjikui wants to merge 1 commit into
modelcontextprotocol:mainfrom
fengjikui:codex/streamable-http-shutdown
Open

fix: terminate streamable http sessions on shutdown#2952
fengjikui wants to merge 1 commit into
modelcontextprotocol:mainfrom
fengjikui:codex/streamable-http-shutdown

Conversation

@fengjikui

Copy link
Copy Markdown
Contributor

Summary

Fixes #2150.

  • terminate active StreamableHTTP transports before the session manager cancels its task group during shutdown
  • close active SSE stream writers when a transport is terminated, so EventSourceResponse instances can complete instead of being cancelled mid-stream
  • make transport termination idempotent and add regression coverage for both cleanup paths

Why

During StreamableHTTPSessionManager.run() shutdown, stateful transports were cleared after cancelling the task group without first being terminated. Separately, StreamableHTTPServerTransport.terminate() cleaned request streams but left _sse_stream_writers open. Active streaming responses could therefore be cancelled before their SSE streams were closed cleanly.

Tests

  • uv run pytest tests/server/test_streamable_http_manager.py::test_run_terminates_active_transports_before_shutdown tests/server/test_streamable_http_manager.py::test_transport_terminate_closes_active_sse_writers -q
  • uv run pytest tests/server/test_streamable_http_manager.py -q
  • uv run pytest tests/shared/test_streamable_http.py::test_session_termination tests/shared/test_streamable_http.py::test_streamable_http_client_session_termination tests/shared/test_streamable_http.py::test_streamable_http_client_session_termination_204 tests/shared/test_streamable_http.py::test_close_sse_stream_callback_not_provided_for_old_protocol_version tests/shared/test_streamable_http.py::test_close_sse_stream_callback_not_provided_for_unknown_protocol_version -q
  • uv run ruff check src/mcp/server/streamable_http_manager.py src/mcp/server/streamable_http.py tests/server/test_streamable_http_manager.py
  • uv run ruff format --check src/mcp/server/streamable_http_manager.py src/mcp/server/streamable_http.py tests/server/test_streamable_http_manager.py
  • uv run python -m py_compile src/mcp/server/streamable_http_manager.py src/mcp/server/streamable_http.py tests/server/test_streamable_http_manager.py
  • git diff --check

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.

Active Streamable HTTP sessions are not terminated during shutdown

1 participant