Skip to content

Avoid _request_streams key collisions in StreamableHTTPServerTransport - #3982

Closed
aadityabuilds wants to merge 1 commit into
modelcontextprotocol:mainfrom
aadityabuilds:fix/streamable-http-stream-key-collision
Closed

aadityabuilds wants to merge 1 commit into
modelcontextprotocol:mainfrom
aadityabuilds:fix/streamable-http-stream-key-collision

Conversation

@aadityabuilds

Copy link
Copy Markdown

Summary

Fixes key collisions in StreamableHTTPServerTransport._request_streams, _sse_stream_writers, and EventStore stream IDs:

  1. Standalone GET stream takeover: A client POST request with JSON-RPC "id": "_GET_stream" previously collided with GET_STREAM_KEY = "_GET_stream", hijacking in-flight unsolicited notifications into the POST response and tearing down the active standalone GET SSE stream on completion.
  2. Integer vs. string request ID collision: Concurrent POST requests with JSON-RPC "id": 1 and "id": "1" both mapped to str(message.id) == "1", overwriting and leaking streams and hanging the displaced request.

Changes

  • Added _request_stream_id(request_id) mapping int IDs to "int:<id>" and str IDs to "str:<id>", disjoint from GET_STREAM_KEY ("_GET_stream").
  • Updated _request_streams, _sse_stream_writers, _run_sse_writer, _clean_up_memory_streams, POST handling, and response routing to key by StreamId via _request_stream_id().
  • Extracted _close_stream(stream_id) so close_sse_stream(request_id) and close_standalone_sse_stream() route to their respective namespaces cleanly.
  • Added regression tests in tests/server/test_streamable_http_router.py covering both GET-stream displacement and concurrent int/str ID collisions.

Verification

  • uv run --frozen pytest tests/server/test_streamable_http_router.py tests/server/test_streamable_http_manager.py tests/shared/test_streamable_http.py tests/interaction/transports/test_hosting_resume.py -q — 139 passed (both new regression tests failed on unmodified main).
  • ruff, pyright, and strict-no-cover clean.

@aadityabuilds
aadityabuilds deleted the fix/streamable-http-stream-key-collision branch December 5, 2026 23: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