[v1.x] fix: handle non-UTF-8 bytes in stdio server stdin (#2303)

This commit is contained in:
Max Isbey 2026-03-17 18:40:43 +00:00 • committed by GitHub
commit 2e9897e2b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -44,7 +44,7 @@ async def stdio_server(
# python is platform-dependent (Windows is particularly problematic), so we
# re-wrap the underlying binary stream to ensure UTF-8.
if not stdin:
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8"))
stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace"))
if not stdout:
stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding="utf-8"))
@ -63,7 +63,7 @@ async def stdio_server(
async for line in stdin:
try:
message = types.JSONRPCMessage.model_validate_json(line)
except Exception as exc: # pragma: no cover
except Exception as exc:
await read_stream_writer.send(exc)
continue