Skip to content

Subprocess kill win32 #729

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

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Create a GitHub release via UI with the tag being `vX.Y.Z` where `X.Y.Z` is the
and the release title being the same. Then ask someone to review the release.

The package version will be set automatically from the tag.


1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = [
"sse-starlette>=1.6.1",
"pydantic-settings>=2.5.2",
"uvicorn>=0.23.1; sys_platform != 'emscripten'",
"psutil>=5.9.0",
"jsonschema>=4.20.0",
]

Expand Down
42 changes: 34 additions & 8 deletions src/mcp/client/stdio/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from typing import BinaryIO, TextIO, cast

import anyio
import psutil
from anyio import to_thread
from anyio.abc import Process
from anyio.streams.file import FileReadStream, FileWriteStream
Expand Down Expand Up @@ -61,6 +61,7 @@ def __init__(self, popen_obj: subprocess.Popen[bytes]):
self.stdin_raw = popen_obj.stdin # type: ignore[assignment]
self.stdout_raw = popen_obj.stdout # type: ignore[assignment]
self.stderr = popen_obj.stderr # type: ignore[assignment]
self.pid = popen_obj.pid

self.stdin = FileWriteStream(cast(BinaryIO, self.stdin_raw)) if self.stdin_raw else None
self.stdout = FileReadStream(cast(BinaryIO, self.stdout_raw)) if self.stdout_raw else None
Expand Down Expand Up @@ -162,6 +163,22 @@ async def create_windows_process(


async def terminate_windows_process(process: Process | FallbackProcess):
"""
Terminate a process and subprocesses.
"""
try:
parent = psutil.Process(process.pid)
except psutil.NoSuchProcess:
return
except Exception:
pass # Optionally log the exception
parent = psutil.Process(process.pid)
children = parent.children(recursive=True)
await terminate_psutil_process(children)
await terminate_psutil_process([parent])


async def terminate_psutil_process(processes: list[psutil.Process]):
"""
Terminate a Windows process.

Expand All @@ -173,10 +190,19 @@ async def terminate_windows_process(process: Process | FallbackProcess):
Args:
process: The process to terminate
"""
try:
process.terminate()
with anyio.fail_after(2.0):
await process.wait()
except TimeoutError:
# Force kill if it doesn't terminate
process.kill()
for process in processes:
try:
process.terminate() # Send SIGTERM (or equivalent on Windows)
except psutil.NoSuchProcess:
pass
except Exception:
pass
# Allow some time for children to terminate gracefully
_, alive = psutil.wait_procs(processes, timeout=2.0)
for child in alive:
try:
child.kill() # Force kill if still alive
except psutil.NoSuchProcess:
pass # Already gone
except Exception:
pass
15 changes: 15 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading