Skip to content

Response leak in SSE handlers #1450

@certainly-param

Description

@certainly-param

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0Broken core functionality, security issues, critical missing featureneeds reproneeds additional information to be able to reproduce bugready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions