Skip to content

Commit 12f2f50

Browse files
committed
Fix stdio_client kill process after timeout
1 parent babb477 commit 12f2f50

File tree

2 files changed

+7
-27
lines changed

2 files changed

+7
-27
lines changed

src/mcp/client/stdio/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from .win32 import (
1616
create_windows_process,
1717
get_windows_executable_command,
18-
terminate_windows_process,
1918
)
2019

2120
# Environment variables to inherit by default
@@ -173,10 +172,13 @@ async def stdin_writer():
173172
yield read_stream, write_stream
174173
finally:
175174
# Clean up process to prevent any dangling orphaned processes
176-
if sys.platform == "win32":
177-
await terminate_windows_process(process)
178-
else:
175+
try:
179176
process.terminate()
177+
with anyio.fail_after(2.0):
178+
await process.wait()
179+
except TimeoutError:
180+
# Force kill if it doesn't terminate
181+
process.kill()
180182

181183

182184
def _get_executable_command(command: str) -> str:

src/mcp/client/stdio/win32.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from typing import TextIO
1010

1111
import anyio
12-
from anyio.abc import Process
1312

1413

1514
def get_windows_executable_command(command: str) -> str:
@@ -85,25 +84,4 @@ async def create_windows_process(
8584
process = await anyio.open_process(
8685
[command, *args], env=env, stderr=errlog, cwd=cwd
8786
)
88-
return process
89-
90-
91-
async def terminate_windows_process(process: Process):
92-
"""
93-
Terminate a Windows process.
94-
95-
Note: On Windows, terminating a process with process.terminate() doesn't
96-
always guarantee immediate process termination.
97-
So we give it 2s to exit, or we call process.kill()
98-
which sends a SIGKILL equivalent signal.
99-
100-
Args:
101-
process: The process to terminate
102-
"""
103-
try:
104-
process.terminate()
105-
with anyio.fail_after(2.0):
106-
await process.wait()
107-
except TimeoutError:
108-
# Force kill if it doesn't terminate
109-
process.kill()
87+
return process

0 commit comments

Comments
 (0)