Skip to content

Fix child process kill error with npx based servers #850

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
10 changes: 9 additions & 1 deletion src/mcp/client/stdio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import signal
import sys
from contextlib import asynccontextmanager
from pathlib import Path
Expand Down Expand Up @@ -190,6 +191,13 @@ async def stdin_writer():
await terminate_windows_process(process)
else:
process.terminate()

try:
with anyio.fail_after(5):
await process.wait()
except TimeoutError:
pgid = os.getpgid(process.pid)
os.killpg(pgid, signal.SIGKILL)
except ProcessLookupError:
# Process already exited, which is fine
pass
Expand Down Expand Up @@ -230,7 +238,7 @@ async def _create_platform_compatible_process(
process = await create_windows_process(command, args, env, errlog, cwd)
else:
process = await anyio.open_process(
[command, *args], env=env, stderr=errlog, cwd=cwd
[command, *args], env=env, stderr=errlog, cwd=cwd, start_new_session=True
)

return process
Loading