Skip to content

Align stdio shutdown sequence with spec #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/mcp/client/stdio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,26 @@ async def stdin_writer():
try:
yield read_stream, write_stream
finally:
# Clean up process to prevent any dangling orphaned processes
if sys.platform == "win32":
await terminate_windows_process(process)
else:
process.terminate()
# MCP spec: stdio shutdown sequence
# 1. Close input stream to server
# 2. Wait for server to exit, or send SIGTERM if it doesn't exit in time
# 3. Send SIGKILL if still not exited
if process.stdin:
await process.stdin.aclose()
try:
with anyio.fail_after(2.0):
await process.wait()
except TimeoutError:
if sys.platform == "win32":
await terminate_windows_process(process)
else:
process.terminate()
try:
with anyio.fail_after(2.0):
await process.wait()
except TimeoutError:
process.kill()
await process.wait()
await read_stream.aclose()
await write_stream.aclose()

Expand Down
Loading