Skip to content

fix(stdio): suppress BrokenResourceError in stdout_reader (#1960)#2723

Open
Epochex wants to merge 1 commit into
modelcontextprotocol:mainfrom
Epochex:fix/1960-stdio-cleanup-brokenresourceerror
Open

fix(stdio): suppress BrokenResourceError in stdout_reader (#1960)#2723
Epochex wants to merge 1 commit into
modelcontextprotocol:mainfrom
Epochex:fix/1960-stdio-cleanup-brokenresourceerror

Conversation

@Epochex
Copy link
Copy Markdown

@Epochex Epochex commented May 29, 2026

Fixes #1960.

stdio_client() can exit while the server is still writing to stdout. With the read stream unconsumed (0-sized memory stream), stdout_reader can be parked inside read_stream_writer.send(...). When the context manager closes the receive side during teardown, anyio raises BrokenResourceError, which currently escapes the task group as an ExceptionGroup.

Changes:

  • Treat (ClosedResourceError, BrokenResourceError) as a normal shutdown signal in stdout_reader.
  • Add a deterministic regression test that waits until stdout_reader is blocked in send() (read_stream.statistics().tasks_waiting_send > 0) before exiting the context.

Tests:

  • uv run --frozen pytest tests/client/test_stdio.py::test_stdio_client_exits_cleanly_while_stdout_reader_is_blocked_in_send
  • uv run --frozen ruff format src/mcp/client/stdio.py tests/client/test_stdio.py
  • uv run --frozen ruff check src/mcp/client/stdio.py tests/client/test_stdio.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BrokenResourceError race condition in stdio_client cleanup when context exits quickly

1 participant