modelcontextprotocol / modelcontextprotocol/python-sdk
Response leak in SSE handlers
還沒有人認領這個 Issue。
- 主要語言
- 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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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