fix: stop newline translation corrupting stdio framing on Windows#3132
Open
bmdhodl wants to merge 1 commit into
Open
fix: stop newline translation corrupting stdio framing on Windows#3132bmdhodl wants to merge 1 commit into
bmdhodl wants to merge 1 commit into
Conversation
stdio_server() wrapped sys.stdout.buffer in a TextIOWrapper with default newline handling, which translates "\n" to os.linesep on write. On Windows every outgoing frame was terminated with "\r\n", breaking the newline-delimited JSON framing required by the spec. Fix: newline="" on the stdout wrapper only. Outgoing frames must be spec-exact; stdin keeps universal-newline reading so CRLF from clients stays tolerated. The regression test asserts on the raw bytes written to stdout, so the platform's newline translation itself is under test: it fails on main on Windows (b'...}\r\n') and passes with the fix. Runs on the windows-latest CI cell. Fixes modelcontextprotocol#2433 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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 #2433. Supersedes stale #2552 (credit to its author for the direction).
stdio_server()wrapssys.stdout.bufferin aTextIOWrapperwith default newline handling, which translates\ntoos.linesepon write. On Windows every outgoing frame is terminated with\r\n, breaking the newline-delimited JSON framing required by the spec.Change
newline=""on the stdout wrapper only. Write-side frames must be spec-exact (\n-delimited); stdin keeps universal-newline reading so CRLF from clients stays tolerated (liberal in what we accept).How Has This Been Tested?
Reproduced the bug natively on Windows 11 (Python 3.13, current
main) with a raw-bytes client against a real stdio server subprocess: frames arrived asb'...}\r\n'before the fix,b'...}\n'after.The new regression test asserts on the raw bytes written to stdout, so the platform's newline translation itself is under test. Verified red/green on native Windows: fails on
mainwithb'{"jsonrpc":"2.0","id":1,"result":{}}\r\n', passes with the fix. It runs on thewindows-latestunit-test CI cell, which currently has no byte-level framing coverage (the test in #2552 assertedendswith(b"\n"), which cannot detect CRLF).Local gate on Windows: full suite 5438 passed / 15 skipped / 1 xfailed,
ruff check+ruff formatclean,pyright(pinned 1.1.405) 0 errors.Breaking Changes
None. Output on POSIX platforms is byte-identical; Windows output changes from out-of-spec
\r\nframing to the spec's\n.Types of changes
Checklist