Skip to content

fix(client): default encoding_error_handler to "replace" - #3896

Closed
ssgupta905 wants to merge 1 commit into
modelcontextprotocol:mainfrom
ssgupta905:fix/stdio-client-invalid-utf8
Closed

ssgupta905 wants to merge 1 commit into
modelcontextprotocol:mainfrom
ssgupta905:fix/stdio-client-invalid-utf8

Conversation

@ssgupta905

Copy link
Copy Markdown

Summary

StdioServerParameters.encoding_error_handler defaults to "strict", and that handler is what TextReceiveStream uses to decode the server's stdout (src/mcp/client/stdio.py:145). A single invalid UTF-8 byte therefore raises UnicodeDecodeError inside stdout_reader's receive loop. That exception matches none of the handler's except clauses, so it escapes the task group and takes the whole transport down as an ExceptionGroup — a valid message on the next line is never delivered.

Worth noting the asymmetry: the shutdown path is already tolerant, because _drain_stdout consumes raw bytes (pinned by test_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_line returns the error as a value on the read stream — the established behaviour for an unparseable line, already pinned by test_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 — default encoding_error_handler to "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 existing FakeProcess / install_fake_process / _line / _next_message helpers. 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-cover clean.
  • Reverting only the source change makes the new test fail with UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0 inside an ExceptionGroup, so it genuinely pins the regression rather than passing trivially.
  • ruff check and ruff format --check clean.

Out of scope

#3714 covers the adjacent case where a caller passes encoding_error_handler="strict" explicitly — the decode error still escapes stdout_reader there. 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).

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>
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot closed this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdio_client crashes on malformed UTF-8 from child stdout instead of surfacing parse error

1 participant