fweinberger/align shutdown with spec (#1091)
Co-authored-by: davenpi <davenport.ianc@gmail.com>
This commit is contained in:
parent
9b4c2ddf93
commit
3abefeeeaf
1 changed files with 390 additions and 15 deletions
|
|
@ -186,13 +186,24 @@ async def stdio_client(server: StdioServerParameters, errlog: TextIO = sys.stder
|
|||
try:
|
||||
yield read_stream, write_stream
|
||||
finally:
|
||||
# Clean up process to prevent any dangling orphaned processes
|
||||
# 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:
|
||||
try:
|
||||
await process.stdin.aclose()
|
||||
except Exception:
|
||||
# stdin might already be closed, which is fine
|
||||
pass
|
||||
|
||||
try:
|
||||
process.terminate()
|
||||
# Give the process time to exit gracefully after stdin closes
|
||||
with anyio.fail_after(PROCESS_TERMINATION_TIMEOUT):
|
||||
await process.wait()
|
||||
except TimeoutError:
|
||||
# If process doesn't terminate in time, force kill it
|
||||
# Process didn't exit from stdin closure, use platform-specific termination
|
||||
# which handles SIGTERM -> SIGKILL escalation
|
||||
await _terminate_process_tree(process)
|
||||
except ProcessLookupError:
|
||||
# Process already exited, which is fine
|
||||
|
|
|
|||
Loading…
Reference in a new issue