fix: prevent stdio transport from closing real process stdin/stdout (#1933)#2820
Open
friendlygeorge wants to merge 1 commit into
Open
Conversation
The stdio server transport wraps sys.stdin.buffer and sys.stdout.buffer with TextIOWrapper for UTF-8 encoding. TextIOWrapper calls close() in __del__, which closes the underlying buffer. When the server exits and the AsyncFile wrappers are garbage collected, this closes the real process stdio file descriptors (fd 0 and fd 1). This causes ValueError on subsequent print() or input() calls in the parent process after the MCP server exits (e.g., Ctrl+D exit). Fix: use _NoCloseTextIOWrapper that overrides close() and __del__() to prevent closing the underlying buffer. The standard process handles should outlive the server. 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.
Problem
The stdio server transport wraps
sys.stdin.bufferandsys.stdout.bufferwithTextIOWrapperfor UTF-8 encoding.TextIOWrappercallsclose()in__del__, which closes the underlying buffer. When the server exits and theAsyncFilewrappers are garbage collected, this closes the real process stdio file descriptors (fd 0 and fd 1).This causes
ValueErroron subsequentprint()orinput()calls in the parent process after the MCP server exits (e.g., Ctrl+D exit).Reproduction
Fix
Introduce
_NoCloseTextIOWrapperthat overridesclose()and__del__()to prevent closing the underlying buffer. The standard process handles should outlive the server.The comment in the existing code already states the intent: "Purposely not using context managers for these, as we don't want to close standard process handles." However,
TextIOWrapper.__del__callsclose()regardless, which silently closes the buffer on garbage collection.Testing
Verified that:
_NoCloseTextIOWrapperdoes not close the underlying buffer when garbage collectedsys.stdinremains readable andsys.stdoutremains writable after the wrapper is deletedTextIOWrapperdoes close the buffer (confirming the bug)Fixes #1933