Skip to content

Commit 00920b5

Browse files
committed
fix: handle broken stdio streams during cleanup
1 parent 5593617 commit 00920b5

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/mcp/client/stdio.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def stdout_reader():
158158

159159
session_message = SessionMessage(message)
160160
await read_stream_writer.send(session_message)
161-
except anyio.ClosedResourceError: # pragma: lax no cover
161+
except (anyio.BrokenResourceError, anyio.ClosedResourceError): # pragma: lax no cover
162162
await anyio.lowlevel.checkpoint()
163163

164164
async def stdin_writer():
@@ -174,7 +174,7 @@ async def stdin_writer():
174174
errors=server.encoding_error_handler,
175175
)
176176
)
177-
except anyio.ClosedResourceError: # pragma: no cover
177+
except (anyio.BrokenResourceError, anyio.ClosedResourceError): # pragma: no cover
178178
await anyio.lowlevel.checkpoint()
179179

180180
async with anyio.create_task_group() as tg, process:
@@ -205,13 +205,10 @@ async def stdin_writer():
205205
except ProcessLookupError: # pragma: no cover
206206
# Process already exited, which is fine
207207
pass
208-
# Stop background stream tasks before closing the memory streams they use.
209-
tg.cancel_scope.cancel()
210-
211-
await read_stream.aclose()
212-
await write_stream.aclose()
213-
await read_stream_writer.aclose()
214-
await write_stream_reader.aclose()
208+
await read_stream.aclose()
209+
await write_stream.aclose()
210+
await read_stream_writer.aclose()
211+
await write_stream_reader.aclose()
215212

216213

217214
def _get_executable_command(command: str) -> str:

0 commit comments

Comments
 (0)