Skip to content

Commit 9fd6d16

Browse files
Fix child process kill error with npx based servers
1 parent 05b7156 commit 9fd6d16

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/mcp/client/stdio/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import signal
23
import sys
34
from contextlib import asynccontextmanager
45
from pathlib import Path
@@ -190,6 +191,13 @@ async def stdin_writer():
190191
await terminate_windows_process(process)
191192
else:
192193
process.terminate()
194+
195+
try:
196+
with anyio.fail_after(5):
197+
await process.wait()
198+
except TimeoutError:
199+
pgid = os.getpgid(process.pid)
200+
os.killpg(pgid, signal.SIGKILL)
193201
except ProcessLookupError:
194202
# Process already exited, which is fine
195203
pass
@@ -230,7 +238,7 @@ async def _create_platform_compatible_process(
230238
process = await create_windows_process(command, args, env, errlog, cwd)
231239
else:
232240
process = await anyio.open_process(
233-
[command, *args], env=env, stderr=errlog, cwd=cwd
241+
[command, *args], env=env, stderr=errlog, cwd=cwd, start_new_session=True
234242
)
235243

236244
return process

0 commit comments

Comments
 (0)