Commit 20209d6
BashNetCorp
[v1.x] fix(stdio): handle BrokenResourceError in stdout_reader (#1960)
Fix the race condition where `read_stream_writer.aclose()` in the `finally`
block of `stdio_client` can close the stream while `stdout_reader` is
mid-`send`, producing an unhandled `anyio.BrokenResourceError` that
propagates through the task group and surfaces as an `ExceptionGroup` to
the caller.
Root cause: the two `send` sites in `stdout_reader` did not catch
`BrokenResourceError`. The outer `except` only caught `ClosedResourceError`,
which is a distinct anyio exception class raised on already-closed streams.
`BrokenResourceError` is raised when the receiver is closed while a `send`
is in flight, which is the exact shape of this shutdown race.
Fix: wrap each `read_stream_writer.send(...)` call in a `try`/`except` that
catches both `ClosedResourceError` and `BrokenResourceError`, and return
cleanly. Also widen the outer `except` to the same union for defense in
depth.
Verified by driving a stdio MCP server (jules-mcp-server) through mcp2cli,
which previously always surfaced the ExceptionGroup traceback; after the
patch, tool calls return cleanly.
Github-Issue:#19601 parent 73d458b commit 20209d6
1 file changed
+13
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
159 | 163 | | |
160 | 164 | | |
161 | 165 | | |
162 | | - | |
163 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
164 | 174 | | |
165 | 175 | | |
166 | 176 | | |
| |||
0 commit comments