|
1 |
| -import asyncio |
| 1 | +"""Test for issue #552: stdio_client hangs on Windows.""" |
| 2 | + |
2 | 3 | import sys
|
3 | 4 |
|
| 5 | +import anyio |
4 | 6 | import pytest
|
5 | 7 |
|
6 |
| -from mcp import ClientSession, StdioServerParameters |
| 8 | +from mcp import StdioServerParameters |
7 | 9 | from mcp.client.stdio import stdio_client
|
8 | 10 |
|
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") |
19 | 45 | @pytest.mark.anyio
|
20 |
| -async def test_windows_process_creation(args): |
| 46 | +async def test_windows_stdio_client_immediate_exit(): |
21 | 47 | """
|
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. |
24 | 52 | """
|
25 |
| - # Use a simple command that should complete quickly on Windows |
| 53 | + # Subprocess exits immediately |
26 | 54 | 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)"], |
30 | 57 | )
|
31 | 58 |
|
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: |
37 | 62 | 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