Skip to content

Commit d6d2cbd

Browse files
Add fallback to taskkill /T before using WMI
Try taskkill /T first as it's simpler and works in many cases. Only fall back to the recursive WMI approach if taskkill /T fails. This provides better compatibility across different Windows environments. Reported-by: fweinberger
1 parent a2870a7 commit d6d2cbd

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/mcp/client/stdio/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,25 @@ async def _kill_process_tree_windows(pid: int) -> None:
257257
Recursively kill a process and all its children on Windows.
258258
Uses WMI to find child processes since taskkill /T doesn't always work.
259259
"""
260+
# First try taskkill with /T flag
261+
result = await anyio.to_thread.run_sync(
262+
subprocess.run,
263+
["taskkill", "/F", "/T", "/PID", str(pid)],
264+
capture_output=True,
265+
shell=False,
266+
check=False,
267+
)
268+
269+
# If that worked, we're done
270+
if result.returncode == 0:
271+
return
272+
273+
# Otherwise, try to find and kill children manually
260274
# First, get all child processes
261-
children_pids = set()
275+
children_pids: set[int] = set()
262276

263277
# Use wmic to get child processes
264-
result = await anyio.to_thread.run_sync(
278+
wmic_result = await anyio.to_thread.run_sync(
265279
subprocess.run,
266280
["wmic", "process", "where", f"ParentProcessId={pid}", "get", "ProcessId", "/FORMAT:VALUE"],
267281
capture_output=True,
@@ -270,9 +284,9 @@ async def _kill_process_tree_windows(pid: int) -> None:
270284
text=True,
271285
)
272286

273-
if result.returncode == 0 and result.stdout:
287+
if wmic_result.returncode == 0 and wmic_result.stdout:
274288
# Parse child PIDs
275-
for line in result.stdout.strip().split('\n'):
289+
for line in wmic_result.stdout.strip().split('\n'):
276290
if line.startswith('ProcessId='):
277291
try:
278292
child_pid = int(line.split('=')[1].strip())

0 commit comments

Comments
 (0)