Skip to content

Commit 35eaaaf

Browse files
committed
fix: align stdio shutdown sequence with spec.
1 parent 2ca2de7 commit 35eaaaf

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/mcp/client/stdio/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,26 @@ async def stdin_writer():
176176
try:
177177
yield read_stream, write_stream
178178
finally:
179-
# Clean up process to prevent any dangling orphaned processes
180-
if sys.platform == "win32":
181-
await terminate_windows_process(process)
182-
else:
183-
process.terminate()
179+
# MCP spec: stdio shutdown sequence
180+
# 1. Close input stream to server
181+
# 2. Wait for server to exit, or send SIGTERM if it doesn't exit in time
182+
# 3. Send SIGKILL if still not exited
183+
if process.stdin:
184+
await process.stdin.aclose()
185+
try:
186+
with anyio.fail_after(2.0):
187+
await process.wait()
188+
except TimeoutError:
189+
if sys.platform == "win32":
190+
await terminate_windows_process(process)
191+
else:
192+
process.terminate()
193+
try:
194+
with anyio.fail_after(2.0):
195+
await process.wait()
196+
except TimeoutError:
197+
process.kill()
198+
await process.wait()
184199
await read_stream.aclose()
185200
await write_stream.aclose()
186201

0 commit comments

Comments
 (0)