modelcontextprotocol / modelcontextprotocol/python-sdk

Response leak in SSE handlers

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

還沒有人認領這個 Issue。

needs repro P2 ready for work
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
31

描述

I found a resource leak in the streamable HTTP client. When SSE streaming fails with an exception, the HTTP response isn't closed.

Location:
src/mcp/client/streamable_http.py:

  • _handle_sse_response (line 336)
  • _handle_resumption_request (line 251)

The Issue:
python
async def _handle_sse_response(self, response: httpx.Response, ...):
try:
event_source = EventSource(response)
async for sse in event_source.aiter_sse():
if is_complete:
await response.aclose() # Only closed here
break
except Exception as e:
await ctx.read_stream_writer.send(e)
# response leaked!

If the SSE iteration raises an exception (malformed JSON, network error, etc.), the response is never closed.

Impact:
Connection pool gets exhausted in long-running clients, eventually causing new requests to hang or fail.

Fix:
try:
...
except Exception as e:
...
finally:
await response.aclose()

Both methods need this fix.

Env:
Python SDK version: 1.1.2 (or main branch)
Python: 3.12
Transport: StreamableHTTP

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

閱讀 src/mcp/client/streamable_http.py,重點關注 _handle_sse_response 和 _handle_resumption_request。追蹤每個方法如何處理 SSE 迭代和例外,然後確認兩條路徑在失敗後以及正常完成後都會關閉 HTTP 回應。

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

評估

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

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

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