Repository navigation
fix(client): default encoding_error_handler to "replace" - #3896
ssgupta905 wants to merge 1 commit into
Conversation
StdioServerParameters.encoding_error_handler defaulted to "strict", which TextReceiveStream uses when decoding the server's stdout. A single invalid UTF-8 byte raised UnicodeDecodeError inside stdout_reader's receive loop; that error matches none of the handler's except clauses, so it escaped the task group and brought the whole transport down as an ExceptionGroup, losing any valid message that followed. The shutdown drain already consumes raw bytes, so only the mid-session path was affected. Defaulting to "replace" makes the malformed bytes U+FFFD, which then fails JSON-RPC validation, and _parse_line returns that error as a value on the read stream -- the established behaviour for an unparseable line. The session stays up and later messages still arrive. This matches the server side, hardened the same way in modelcontextprotocol#2302. Fixes modelcontextprotocol#2454 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
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 #2454. If a maintainer assigns you to #2454, 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
StdioServerParameters.encoding_error_handlerdefaults to"strict", and that handler is whatTextReceiveStreamuses to decode the server's stdout (src/mcp/client/stdio.py:145). A single invalid UTF-8 byte therefore raisesUnicodeDecodeErrorinsidestdout_reader's receive loop. That exception matches none of the handler'sexceptclauses, so it escapes the task group and takes the whole transport down as anExceptionGroup— a valid message on the next line is never delivered.Worth noting the asymmetry: the shutdown path is already tolerant, because
_drain_stdoutconsumes raw bytes (pinned bytest_invalid_utf8_flushed_by_a_dying_server_does_not_break_shutdown). Only the mid-session path was affected.This defaults the handler to
"replace". The malformed bytes become U+FFFD, that line then fails JSON-RPC validation, and_parse_linereturns the error as a value on the read stream — the established behaviour for an unparseable line, already pinned bytest_invalid_json_from_the_server_surfaces_as_an_in_stream_exception. The session stays up and later messages still arrive.This matches the server side, which was hardened the same way in #2302.
Changes
src/mcp/client/stdio.py— defaultencoding_error_handlerto"replace", with a docstring note on the resulting behaviour.tests/client/test_stdio.py— one regression test, mirroring the invalid-JSON sibling test and reusing the existingFakeProcess/install_fake_process/_line/_next_messagehelpers. It asserts on exception type and on the follow-up message, never on OS- or locale-dependent message text.Verification
./scripts/test: 5923 passed, 100.00% coverage,strict-no-coverclean.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0inside anExceptionGroup, so it genuinely pins the regression rather than passing trivially.ruff checkandruff format --checkclean.Out of scope
#3714 covers the adjacent case where a caller passes
encoding_error_handler="strict"explicitly — the decode error still escapesstdout_readerthere. Left alone deliberately to keep this change to the one issue; happy to follow up separately if that fix is wanted.Fixes #2454
Prepared with AI assistance (Claude Code).