Add tests for resource leak in streamable_http SSE handlers#1490
Closed
certainly-param wants to merge 1 commit intomodelcontextprotocol:mainfrom
Closed
Add tests for resource leak in streamable_http SSE handlers#1490certainly-param wants to merge 1 commit intomodelcontextprotocol:mainfrom
certainly-param wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Fixes modelcontextprotocol#1450 - HTTP responses don't get closed properly when SSE streaming fails with exceptions in _handle_sse_response and _handle_resumption_request. The issue: when the async for loop throws an exception, response.aclose() never gets called because it's only in the success path. Added reproduction script and pytest tests to demonstrate the problem. The fix requires adding finally blocks to ensure response.aclose() always gets called.
1b4bf1e to
d2f7de8
Compare
Contributor
|
Marking as a draft for now since this is only a reproduction and not a completed fix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1450
I found that HTTP responses don't get closed properly when SSE streaming fails with exceptions in the streamable HTTP client.
The Problem:
When the async for loop in
_handle_sse_responseand_handle_resumption_requestthrows an exception (malformed JSON, network error, etc.), the response is never closed becauseresponse.aclose()is only called in the success path.Impact:
This can cause connection pool exhaustion in long-running clients, eventually causing new requests to hang or fail.
What I Added:
test_resource_leak_reproduction.py- Standalone script that demonstrates the issuetests/client/test_streamable_http_resource_leak.py- Pytest tests that verify the resource leak and show how the fix should workBoth test files show that when SSE parsing fails, the response doesn't get closed, confirming the resource leak.
The Fix:
The methods need
finallyblocks to ensureresponse.aclose()is always called:This ensures resources are properly cleaned up regardless of whether exceptions occur.