fix: clean up StreamableHTTP SSE disconnects#2982
Open
tarunag10 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/server/test_streamable_http_manager.py">
<violation number="1" location="tests/server/test_streamable_http_manager.py:138">
P2: The new async test uses an unbounded `read_stream.receive()` wait, which can hang the test suite on failure paths instead of timing out deterministically.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
Comment on lines
+138
to
+139
| session_message = await read_stream.receive() | ||
| assert session_message.message.method == "tools/list" |
There was a problem hiding this comment.
P2: The new async test uses an unbounded read_stream.receive() wait, which can hang the test suite on failure paths instead of timing out deterministically.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/server/test_streamable_http_manager.py, line 138:
<comment>The new async test uses an unbounded `read_stream.receive()` wait, which can hang the test suite on failure paths instead of timing out deterministically.</comment>
<file context>
@@ -101,6 +101,48 @@ async def running_manager():
+ async with transport.connect() as (read_stream, _write_stream):
+ async with anyio.create_task_group() as tg:
+ tg.start_soon(transport.handle_request, scope, receive, send)
+ session_message = await read_stream.receive()
+ assert session_message.message.method == "tools/list"
+
</file context>
Suggested change
| session_message = await read_stream.receive() | |
| assert session_message.message.method == "tools/list" | |
| with anyio.fail_after(5): | |
| session_message = await read_stream.receive() | |
| assert session_message.message.method == "tools/list" |
e6fa554 to
0816035
Compare
|
Good fix! The cleanup in finally ensures SSE stream writers and memory streams are properly cleaned up when the response returns, preventing CLOSE_WAIT accumulation. The DisconnectingEventSourceResponse test mock is a clever way to verify cleanup behavior. |
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.
Summary
Why
Issue #2958 reports StreamableHTTP servers accumulating
CLOSE_WAITsockets behind reverse proxies. One failure mode is thatEventSourceResponsecan return after a client/proxy disconnect while the per-request writer remains blocked on the in-memory request stream. Closing the request streams in the response task'sfinallypath unblocks the writer and lets the ASGI request finish cleanly.Validation
uv run ruff format --check src/mcp/server/streamable_http.py tests/server/test_streamable_http_manager.pyuv run ruff check src/mcp/server/streamable_http.py tests/server/test_streamable_http_manager.pyuv run pytest tests/server/test_streamable_http_manager.py -qFixes #2958