Title: [v1.x] fix(stdio): handle BrokenResourceError in stdout_reader race (#1960) by ddullah · Pull Request #2449 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: [v1.x] fix(stdio): handle BrokenResourceError in stdout_reader race (#1960) by ddullah · Pull Request #2449 · modelcontextprotocol/python-sdk
X Title: [v1.x] fix(stdio): handle BrokenResourceError in stdout_reader race (#1960) by ddullah · Pull Request #2449 · modelcontextprotocol/python-sdk
Description: Summary Fixes #1960. The stdio_client in mcp/client/stdio/__init__.py has a race condition: read_stream_writer.aclose() in the finally block can close the stream while the background stdout_reader task is mid-send, which raises anyio.BrokenResourceError and propagates through the task group as an ExceptionGroup, failing the caller. Root cause The two read_stream_writer.send(...) sites inside stdout_reader were not guarded against BrokenResourceError. The outer except only caught ClosedResourceError, which is the sibling class raised on already-closed streams; BrokenResourceError is raised when the receiver is closed during an in-flight send, which is the exact shape of this shutdown race. Fix Wrap each read_stream_writer.send(...) in try/except catching both ClosedResourceError and BrokenResourceError, then return cleanly. Widen the outer except to the same union for defense in depth. No API changes. Alternative considered Issue #1960 suggests tg.cancel_scope.cancel() before the stream closes in finally. That works but changes shutdown ordering (cancels user-defined notification handlers, etc.). The chosen fix is narrower: it treats the shutdown race as a graceful exit from stdout_reader specifically, without altering the task group lifecycle. Reproduction Any stdio MCP server that emits log-channel notifications alongside tool responses will exhibit this when driven by a downstream wrapper (e.g., mcp2cli) that opens a fresh stdio_client per invocation. The task group exits quickly, the subprocess is still flushing output, and the race is triggered. Verification Drove jules-mcp-server (an MCP server that emits notifications/message log frames during init) through mcp2cli with a list-schedules tool call. Before this patch: ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- | Traceback (most recent call last): | File ".../mcp/client/stdio/__init__.py", line 162, in stdout_reader | await read_stream_writer.send(session_message) | File ".../anyio/streams/memory.py", line 220, in send_nowait | raise BrokenResourceError | anyio.BrokenResourceError After this patch: {"count": 0, "schedules": []} EXIT:0 Closes #1960.
Open Graph Description: Summary Fixes #1960. The stdio_client in mcp/client/stdio/__init__.py has a race condition: read_stream_writer.aclose() in the finally block can close the stream while the background stdout_reader ...
X Description: Summary Fixes #1960. The stdio_client in mcp/client/stdio/__init__.py has a race condition: read_stream_writer.aclose() in the finally block can close the stream while the background stdout_reader ...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2449
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:3931d623-1540-e96c-bb06-11837e20f849 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9CBC:188D03:28F957:37E66C:6A5A6DBB |
| html-safe-nonce | c9bd2564e1709dd85757233157b22529ff910e6aae6cd862e65fd46ef81aa68a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5Q0JDOjE4OEQwMzoyOEY5NTc6MzdFNjZDOjZBNUE2REJCIiwidmlzaXRvcl9pZCI6IjQ2NDIxNzQ2OTU5NjU4Nzk3MzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 68b26ab314d92fdd1d08d99db0171d6bc1dfdccd2c09f3b5a3e65caf3aebea0c |
| hovercard-subject-tag | pull_request:3534337881 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2449/files |
| twitter:image | https://avatars.githubusercontent.com/u/134097561?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/134097561?s=400&v=4 |
| og:image:alt | Summary Fixes #1960. The stdio_client in mcp/client/stdio/__init__.py has a race condition: read_stream_writer.aclose() in the finally block can close the stream while the background stdout_reader ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 922bdba685ea48400234d557977f0e32d9adbae6f5125725b4f48cacc6801106 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 03575fc589c0e2692de839d7ca2b3aa5b5179f52 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width