fix: use os.dup to avoid closing real stdin/stdout in stdio_server#2737
Open
xlyoung wants to merge 1 commit into
Open
fix: use os.dup to avoid closing real stdin/stdout in stdio_server#2737xlyoung wants to merge 1 commit into
xlyoung wants to merge 1 commit into
Conversation
Fixes modelcontextprotocol#1933 When stdio_server() wraps sys.stdin.buffer/sys.stdout.buffer directly, closing the context manager also closes the real file descriptors, causing ValueError on subsequent I/O operations. Fix: Use os.dup() to duplicate file descriptors before wrapping them. This ensures closing the wrappers doesn't affect the real stdin/stdout. Added fallback for environments where fileno() is not available (e.g., BytesIO-backed streams in tests).
StantonMatt
reviewed
Jun 3, 2026
StantonMatt
left a comment
There was a problem hiding this comment.
I checked 193c4f5 locally on Windows/Python 3.13.13:
uv run --frozen pytest tests/server/test_stdio.py -q
# 3 passed
git diff --check origin/main...HEADThe one visible red job is failing in tests/shared/test_sse.py::test_sse_session_cleanup_on_disconnect on Python 3.14 locked Ubuntu, not in the stdio tests.
Two branch-specific things stood out:
uv.lockis rewritten even though this PR does not change dependencies.- The new
test_stdio_server_uses_os_duponly inspects source text foros.dup/os.fdopen, so it could pass without proving the original process stdio wrappers remain usable afterstdio_server()exits.
This also overlaps with #2734, which targets the same #1933 path with behavior-level stdio assertions and currently has a green matrix. I would either drop the lockfile churn and switch this test to behavior, or close this in favor of #2734 if that branch is the intended fix.
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 #1933
Problem
When
stdio_server()wrapssys.stdin.buffer/sys.stdout.bufferdirectly, closing the context manager also closes the real file descriptors, causingValueError: I/O operation on closed fileon subsequent I/O operations.Fix
Use
os.dup()to duplicate file descriptors before wrapping them. This ensures closing the wrappers does not affect the real stdin/stdout.Added fallback for environments where
fileno()is not available (e.g., BytesIO-backed streams in tests).Testing
test_stdio_server_uses_os_dupto verify the fix-raceflag