Skip to content

Commit 2d1ec8b

Browse files
Fix ResourceWarning by properly cleaning up subprocess objects
After killing processes with taskkill on Windows, we need to call wait() on the Popen object to let it know the process is dead and clean up its resources. This prevents the ResourceWarning. This is the proper fix instead of suppressing the warning.
1 parent cd0fea2 commit 2d1ec8b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tests/client/test_stdio.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ def sigterm_handler(signum, frame):
365365

366366

367367
@pytest.mark.anyio
368-
@pytest.mark.filterwarnings("ignore::ResourceWarning")
369368
async def test_stdio_client_child_process_cleanup():
370369
"""
371370
Test that child processes are properly terminated when the parent is killed.
@@ -444,6 +443,11 @@ async def test_stdio_client_child_process_cleanup():
444443
from mcp.client.stdio import _terminate_process_with_children
445444

446445
await _terminate_process_with_children(proc)
446+
447+
# Ensure the process object is properly cleaned up
448+
if sys.platform == "win32":
449+
# Wait for the process to be reaped to avoid ResourceWarning
450+
proc._popen.wait()
447451

448452
# Verify processes stopped
449453
await anyio.sleep(0.5)
@@ -469,7 +473,6 @@ async def test_stdio_client_child_process_cleanup():
469473

470474

471475
@pytest.mark.anyio
472-
@pytest.mark.filterwarnings("ignore::ResourceWarning")
473476
async def test_stdio_client_nested_process_tree():
474477
"""
475478
Test that a nested process tree (parent → child → grandchild) is properly cleaned up.
@@ -554,6 +557,11 @@ async def test_stdio_client_nested_process_tree():
554557
from mcp.client.stdio import _terminate_process_with_children
555558

556559
await _terminate_process_with_children(proc)
560+
561+
# Ensure the process object is properly cleaned up
562+
if sys.platform == "win32":
563+
# Wait for the process to be reaped to avoid ResourceWarning
564+
proc._popen.wait()
557565

558566
# Verify all stopped
559567
await anyio.sleep(0.5)
@@ -576,7 +584,6 @@ async def test_stdio_client_nested_process_tree():
576584

577585

578586
@pytest.mark.anyio
579-
@pytest.mark.filterwarnings("ignore::ResourceWarning")
580587
async def test_stdio_client_early_parent_exit():
581588
"""
582589
Test that child processes are cleaned up when parent exits during cleanup.
@@ -644,6 +651,11 @@ def handle_term(sig, frame):
644651
from mcp.client.stdio import _terminate_process_with_children
645652

646653
await _terminate_process_with_children(proc)
654+
655+
# Ensure the process object is properly cleaned up
656+
if sys.platform == "win32":
657+
# Wait for the process to be reaped to avoid ResourceWarning
658+
proc._popen.wait()
647659

648660
# Verify child stopped
649661
await anyio.sleep(0.5)

0 commit comments

Comments
 (0)