Fix stdio transport closing sys.stdin.buffer/sys.stdout.buffer on exit#2722
Open
TheChyeahhh wants to merge 1 commit into
Open
Fix stdio transport closing sys.stdin.buffer/sys.stdout.buffer on exit#2722TheChyeahhh wants to merge 1 commit into
TheChyeahhh wants to merge 1 commit into
Conversation
When using transport='stdio', anyio.wrap_file() wraps a TextIOWrapper around sys.stdin.buffer/sys.stdout.buffer. When the server exits and the async file is closed, TextIOWrapper.close() propagates to close the underlying buffer, making subsequent stdio operations fail with: ValueError: underlying buffer has been closed This adds a _DetachingTextIOWrapper that detaches from the buffer on close, preventing the propagation. The original comment in the code stated 'Purposely not using context managers for these, as we don't want to close standard process handles' — this fix aligns the implementation with that intent. Fixes modelcontextprotocol#1933
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.
Description
Fixes #1933
When using
transport="stdio",anyio.wrap_file()wraps aTextIOWrapperaroundsys.stdin.buffer/sys.stdout.buffer. When the server exits, the async file close propagates throughTextIOWrapper.close()and closes the underlying buffer, making subsequent stdio operations fail withValueError: underlying buffer has been closed.Root Cause
The code comment at line 37-38 says:
But
anyio.wrap_file()creates a context manager that callsclose()on theTextIOWrapper, which in turn closes the underlyingsys.stdin.buffer— exactly what the comment says should not happen.Fix
Added
_DetachingTextIOWrapper— a thin subclass ofTextIOWrapperthat overridesclose()to detach the underlying buffer first, preventing close propagation. The buffer survives, and subsequent stdio operations work as expected after the server exits.Changes
src/mcp/server/stdio.py— +13/-2 lines