StreamableHTTPServerTransport: TOCTOU race between GET 409 conflict check and standalone SSE slot registration allows concurrent GET streams

Đang mở
#3,891 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

  • #3912 của @EnesKartal — đã đóng, không merge
  • #3920 của @EnesKartal — đã đóng, không merge
  • #3936 của @Tthanh2k6 — đã đóng, không merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
64/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Start in src/mcp/server/streamable_http.py: compare the synchronous registration in the POST paths with _handle_get_request (~line 768) and standalone_sse_writer (~line 783), where the GET slot is added after the response starts. Reproduce with the concurrent anyio.create_task_group() GET test in tests/shared/test_streamable_http.py, expecting [200, 409]. Done means the GET slot is registered synchronously after the conflict check, cleanup runs in finally, and the streamable HTTP tests plus ruff/pyright pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug v1 v2
Describe the bug

In src/mcp/server/streamable_http.py (StreamableHTTPServerTransport._handle_get_request), the check rejecting a second standalone GET SSE stream (if GET_STREAM_KEY in self._request_streams: -> 409 Conflict) and the registration of self._request_streams[GET_STREAM_KEY] are not atomic:

  1. The conflict check runs synchronously in _handle_get_request() (~line 768).
  2. The slot is only inserted inside the async standalone_sse_writer() task (~line 783), which sse-starlette's EventSourceResponse schedules via task_group.start_soon(...) after the response begins.

By contrast, both POST paths register self._request_streams[request_id] synchronously before yielding control.

When two GET /mcp (Accept: text/event-stream) requests arrive concurrently for the same session:

  • Both pass if GET_STREAM_KEY in self._request_streams: before either standalone_sse_writer() runs.
  • Both return HTTP 200 instead of [200, 409].
  • The second writer overwrites self._request_streams[GET_STREAM_KEY], leaving the first stream orphaned (receiving no server-initiated messages), and when the first connection later closes its finally cleanup closes the second connection's active stream.
Minimal Reproduction

In tests/shared/test_streamable_http.py, dispatch two concurrent GET /mcp streams for the same initialized session via anyio.create_task_group():

statuses: dict[str, int] = {}

async def open_stream(key: str) -> None:
    async with client.stream("GET", "/mcp", headers=get_headers) as response:
        statuses[key] = response.status_code

async with anyio.create_task_group() as tg:
    tg.start_soon(open_stream, "first")
    tg.start_soon(open_stream, "second")

assert sorted(statuses.values()) == [200, 409]

Actual result on main@77001c96:

AssertionError: assert [200, 200] == [200, 409]
Suggested Fix

Register self._request_streams[GET_STREAM_KEY] synchronously in _handle_get_request() immediately after the GET_STREAM_KEY in self._request_streams conflict check (matching the POST paths), reuse standalone_stream_reader inside standalone_sse_writer(), and ensure await self._clean_up_memory_streams(GET_STREAM_KEY) runs in the finally: block to cover cancellation/errors before standalone_sse_writer() starts.

Environment
  • modelcontextprotocol/python-sdk: main@77001c96 (v2)
  • OS: macOS (Darwin arm64)
  • Python: 3.12+

Verified locally: concurrent GET test fails [200, 200] before the fix and passes [200, 409] after, with full test_streamable_http.py + ruff/pyright clean. Happy to submit a PR if helpful.


AI disclosure: AI assistance was used to help investigate and draft this issue; the reproduction and observed output were verified locally.

Ngôn ngữ chính
Python
Star
25k
Fork
4.2k
Merge trung bình
10 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/python-sdk

Tất cả issue của modelcontextprotocol/python-sdk

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.