Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

`stdio_server` uses unbuffered memory streams which can cause server to block and become unresponsive

未關閉
#1,333 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
52/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
python
領域
backend

研究方向

從 src/mcp/server/stdio.py 中引用的 memory-stream 設定開始,檢查 max_buffer_size 如何傳遞給兩個串流。依照描述讓緩衝區大小可設定,同時保留目前的預設值,並使用提供的非同步範例驗證正值能在處理速度較慢時對輸入進行緩衝。

由索引模型根據 Issue 內容生成。

描述

bug P2 ready for work
Initial Checks
Description

The MCP Python SDK's stdio_server becomes unresponsive during slow message processing operations, causing ping and/or new requests to timeout.

Observed Behaviour
  • Server appears to 'freeze' and becomes unresponsive after running for extended periods
  • Ping requests timeout during slow operations
  • New requests cannot be processed while the server is handling long-running operations
  • After 67 minutes of operation with requests every 10 seconds, the server became completely unresponsive
Expected Behaviour
  • Server should remain responsive to new requests (like pings) even while processing slow operations
  • Ping requests should not timeout due to message processing delays
  • The server should handle concurrent requests without blocking
Suspected Root Cause

The stdio_server uses max_buffer_size=0 (supplied to anyio.create_memory_object_stream) by default, which creates synchronous handoff between the stdin reader and message processor.

See: https://github.com/modelcontextprotocol/python-sdk/blob/543961968c0634e93d919d509cce23a1d6a56c21/src/mcp/server/stdio.py#L57-L58

When the message processor is slow or blocked, the stdin reader cannot read new messages from stdin, making the server appear unresponsive.

Example Code
  import anyio
  import pytest


  @pytest.mark.anyio
  async def test_server_becomes_unresponsive_with_slow_processor():
      """Demonstrates how server becomes unresponsive during slow processing."""

      # Simulates stdio_server with default max_buffer_size=0 (synchronous handoff)
      send_stream, receive_stream = anyio.create_memory_object_stream(0)

      async def stdin_reader():
          # First message gets through
          await send_stream.send("request_1")

          # Second message (like a ping) blocks until first is fully processed
          await send_stream.send("ping")  # This will block for entire processing time!

      async def message_processor():
          # Process first message
          msg = await receive_stream.receive()

          # Simulate slow processing (database query, API call, etc.)
          await anyio.sleep(0.1)  # 100ms processing time

          # During this time, stdin_reader is completely blocked
          # No new messages (including pings) can be read!

          ping = await receive_stream.receive()  # Finally unblocks stdin_reader

      async with anyio.create_task_group() as tg:
          tg.start_soon(message_processor)
          await anyio.sleep(0.01)  # Let processor start waiting
          tg.start_soon(stdin_reader)

      send_stream.close()
      receive_stream.close()
Proposed Solution

Allow users to configure max_buffer_size > 0 to enable buffering, with a default value (0) that preserves the current behaviour.

async def stdio_server(
    stdin: anyio.AsyncFile[str] | None = None,
    stdout: anyio.AsyncFile[str] | None = None,
    max_buffer_size: int = 0, 
):
    # ...
    read_stream_writer, read_stream = anyio.create_memory_object_stream(max_buffer_size)
    write_stream, write_stream_reader = anyio.create_memory_object_stream(max_buffer_size)
Python & MCP Python SDK
  • Python: 3.12
  • MCP Python SDK: 1.12.4
  • OS: macOS
Additional Context

The issue manifests in long-running servers where message processing can occasionally be slow (database queries, file operations, API calls).

The fix should make max_buffer_size configurable so users can add buffering to prevent the stdin reader from blocking during slow operations.

主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 11 小時
30 天內合併 PR
30

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

modelcontextprotocol/python-sdk 的其他 Issue

查看 modelcontextprotocol/python-sdk 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。