fix(streamable-http): wait for post_writer before yielding#2666
Open
Epochex wants to merge 3 commits into
Open
fix(streamable-http): wait for post_writer before yielding#2666Epochex wants to merge 3 commits into
Epochex wants to merge 3 commits into
Conversation
Author
|
Hi @maxisbey — this fixes #1675 (StreamableHTTP startup race right after initialize). It waits for post_writer readiness before yielding streams and adds regression tests. I also tightened stdio teardown by closing stdout to avoid an unclosed pipe transport warning on Windows. Local tests are listed in the PR body. Would appreciate a review when you have a moment. |
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 #1675.
What happened
streamable_http_client()startedStreamableHTTPTransport.post_writer()withtg.start_soon(), then yielded(read_stream, write_stream)immediately.Because the write side is a zero-buffer in-memory stream, there is a narrow startup window where the first requests right after
initialize()can race with the writer task entering its stream contexts. That shows up as flaky/incorrect behavior on the first few calls (e.g.list_tools()).What changed
post_writer()withawait tg.start(...)and havepost_writer()signal readiness viaTaskStatus.started(None).initialize().Extra hardening
While iterating on the Windows lowest-direct matrix, one job surfaced an unclosed pipe transport warning during teardown. I added an explicit
process.stdout.aclose()instdio_client()teardown to make subprocess cleanup more robust.Tests
uv run --frozen pytest tests/shared/test_streamable_http.py -k 'no_race_on_consecutive_requests or rapid_request_sequence'uv run --frozen ruff check src/mcp/client/streamable_http.py tests/shared/test_streamable_http.pyuv run --frozen pyright src/mcp/client/streamable_http.pyuv run --frozen pytest tests/issues/test_1027_win_unreachable_cleanup.py tests/issues/test_129_resource_templates.py