Fix stdio_client BrokenResourceError race condition during shutdown#2268
Open
weiguangli-io wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Fix stdio_client BrokenResourceError race condition during shutdown#2268weiguangli-io wants to merge 1 commit intomodelcontextprotocol:mainfrom
weiguangli-io wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
During shutdown, the finally block closes the receiving end of the read_stream memory channel while stdout_reader may still be blocked on send(). This causes BrokenResourceError (not ClosedResourceError) because the *other* end of the stream was closed. The existing except clause only caught ClosedResourceError, letting BrokenResourceError propagate into an ExceptionGroup. Catch BrokenResourceError alongside ClosedResourceError in both stdout_reader and stdin_writer to handle this race gracefully. Closes modelcontextprotocol#1960 Github-Issue:modelcontextprotocol#1960 Reported-by:maxisbey
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
Fixes #1960. Supersedes #2219 (which was closed).
During
stdio_clientshutdown, thefinallyblock closesread_stream(the receiving end of a zero-buffer memory channel) whilestdout_readermay still be blocked onsend(). When the receiving end closes underneath a pendingsend(), anyio raisesBrokenResourceError— notClosedResourceError. The existingexceptclause only caughtClosedResourceError, soBrokenResourceErrorescaped into anExceptionGroup.The fix catches
BrokenResourceErroralongsideClosedResourceErrorin bothstdout_readerandstdin_writer. This is the minimal, correct change:ClosedResourceErrormeans "you called send on a handle you already closed", whileBrokenResourceErrormeans "the other end was closed" — both are expected during shutdown and should be handled identically.Test plan
test_stdio_client_no_broken_resource_error_on_shutdownthat reproduces the exact scenario: server sends a JSON-RPC message, client exits without consuming the read streamtests/client/test_stdio.pytests pass