Skip to content

Commit 1c07e37

Browse files
Apply review feedback
- Replace asyncio.timeout with anyio.fail_after for consistency - Simplify exception handling - Uncomment Windows-specific test skip condition - Rewrite tests to focus on verifying no hang occurs (issue #552) - Use simple Python subprocesses that exit quickly - Remove complex assertions - test passes if it completes without timeout
1 parent e64a939 commit 1c07e37

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

tests/issues/test_552_windows_hang.py

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
1-
import asyncio
1+
"""Test for issue #552: stdio_client hangs on Windows."""
2+
23
import sys
34

5+
import anyio
46
import pytest
57

6-
from mcp import ClientSession, StdioServerParameters
8+
from mcp import StdioServerParameters
79
from mcp.client.stdio import stdio_client
810

9-
# @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
10-
@pytest.mark.skipif(sys.version_info < (3, 11), reason="asyncio.timeout in 3.11+")
11-
@pytest.mark.parametrize(
12-
"args",
13-
[
14-
["/C", "echo", '{"jsonrpc": "2.0", "id": 1, "result": null}'],
15-
["dfghfgh"],
16-
["/C", "echo"],
17-
],
18-
)
11+
12+
@pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
13+
@pytest.mark.anyio
14+
async def test_windows_stdio_client_no_hang_on_exit():
15+
"""
16+
Test for issue #552: stdio_client hangs on Windows 11.
17+
18+
This test verifies that the stdio_client properly handles subprocess
19+
termination on Windows without hanging. The key is that the test
20+
completes within the timeout period.
21+
"""
22+
# Create a subprocess that exits after a short delay
23+
# This gives stdio_client time to set up pipes before the process exits
24+
exit_script = "import time; time.sleep(0.1); import sys; sys.exit(0)"
25+
26+
params = StdioServerParameters(
27+
command=sys.executable,
28+
args=["-c", exit_script],
29+
)
30+
31+
# The test passes if this completes without timing out
32+
# We don't care about exceptions - just that it doesn't hang
33+
with anyio.fail_after(10): # Generous timeout - should complete in < 1s
34+
try:
35+
async with stdio_client(params) as (read, write):
36+
# If we get here, the connection was established
37+
# The subprocess will exit soon, causing cleanup
38+
await anyio.sleep(0.5) # Wait for subprocess to exit
39+
except Exception:
40+
# Any exception is fine - we're just testing for hangs
41+
pass
42+
43+
44+
@pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
1945
@pytest.mark.anyio
20-
async def test_windows_process_creation(args):
46+
async def test_windows_stdio_client_immediate_exit():
2147
"""
22-
Test that directly tests the process creation function that was fixed in issue #552.
23-
This simpler test verifies that Windows process creation works without hanging.
48+
Test that stdio_client handles immediate subprocess exit on Windows.
49+
50+
This tests the edge case where the subprocess exits before
51+
stdio_client can fully initialize.
2452
"""
25-
# Use a simple command that should complete quickly on Windows
53+
# Subprocess exits immediately
2654
params = StdioServerParameters(
27-
command="cmd",
28-
# Echo a valid JSON-RPC response message that will be parsed correctly
29-
args=args,
55+
command=sys.executable,
56+
args=["-c", "import sys; sys.exit(1)"],
3057
)
3158

32-
# Directly test the fixed function that was causing the hanging issue
33-
try:
34-
# Set a timeout to prevent hanging
35-
async with asyncio.timeout(5):
36-
# Test the actual process creation function that was fixed
59+
# The test passes if this completes without timing out
60+
with anyio.fail_after(10): # Generous timeout
61+
try:
3762
async with stdio_client(params) as (read, write):
38-
print("inside client")
39-
async with ClientSession(read, write) as c:
40-
print("inside ClientSession")
41-
await c.initialize()
42-
43-
except asyncio.TimeoutError:
44-
pytest.xfail("Process creation timed out, indicating a hang issue")
45-
except ProcessLookupError:
46-
pytest.xfail("Process creation failed with ProcessLookupError")
47-
except Exception as e:
48-
assert "ExceptionGroup" in repr(e), f"Unexpected error: {e}"
49-
assert "ProcessLookupError" in repr(e), f"Unexpected error: {e}"
50-
pytest.xfail(f"Expected error: {e}")
63+
# Subprocess should have exited already
64+
pass
65+
except Exception:
66+
# Expected - subprocess exited
67+
pass

0 commit comments

Comments
 (0)