-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
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