fix(stdio): handle bufferless text streams in fallback - #3575
sidinsearch wants to merge 1 commit into
Conversation
_claim_fd's fallback path dereferences stream.buffer unconditionally, but _is_backed_by_fd returns False when a stream has no .buffer attribute (e.g. io.StringIO). This raises AttributeError before the transport serves a single message. Fix by checking for the buffer attribute and returning None when the stream is bufferless. The caller then wraps the text stream in place since there is no binary layer to re-encode. Closes modelcontextprotocol#1933 Co-authored-by: openhands <openhands@all-hands.dev>
|
This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #1933. If a maintainer assigns you to #1933, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take. You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way. CONTRIBUTING.md has the full reasoning, but in short:
Maintainers: reopen, remove |
Summary
Fixes a crash in
stdio_server()whensys.stdin/sys.stdoutare replaced with bufferless text streams (e.g.,io.StringIO).The
_claim_fdfunction's fallback path unconditionally dereferencesstream.buffer, but_is_backed_by_fdreturnsFalsewhen a stream has no.bufferattribute. This raisesAttributeErrorbefore the transport serves a single message.This is a distinct issue from the original #1933 (which was fixed by #3117). The original issue was about the wrapper closing the real stdio handles; this is about bufferless text streams (like
io.StringIO) crashing the fallback path.Changes
src/mcp/server/stdio.py: Check for the
bufferattribute in_claim_fd's fallback path; returnNonewhen the stream is bufferless. The caller (stdio_server) then wraps the text stream in place since there is no binary layer to re-encode.tests/server/test_stdio.py: Added
test_stdio_server_serves_bufferless_text_streams_in_placewhich monkeypatchessys.stdin/sys.stdouttoio.StringIOand verifies the transport serves messages correctly.Test results
Disclosure
Written with AI assistance, reviewed and verified by me.
Fixes #1933