|
1 | 1 | import shutil
|
| 2 | +import sys |
| 3 | +import textwrap |
| 4 | +import time |
2 | 5 |
|
| 6 | +import anyio |
3 | 7 | import pytest
|
4 | 8 |
|
5 | 9 | from mcp.client.session import ClientSession
|
@@ -90,3 +94,121 @@ async def test_stdio_client_nonexistent_command():
|
90 | 94 | or "not found" in error_message.lower()
|
91 | 95 | or "cannot find the file" in error_message.lower() # Windows error message
|
92 | 96 | )
|
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.anyio |
| 100 | +async def test_stdio_client_universal_cleanup(): |
| 101 | + """ |
| 102 | + Test that stdio_client completes cleanup within reasonable time |
| 103 | + even when connected to processes that exit slowly. |
| 104 | + """ |
| 105 | + |
| 106 | + # Use a Python script that simulates a long-running process |
| 107 | + # This ensures consistent behavior across platforms |
| 108 | + long_running_script = textwrap.dedent( |
| 109 | + """ |
| 110 | + import time |
| 111 | + import sys |
| 112 | + |
| 113 | + # Simulate a long-running process |
| 114 | + for i in range(100): |
| 115 | + time.sleep(0.1) |
| 116 | + # Flush to ensure output is visible |
| 117 | + sys.stdout.flush() |
| 118 | + sys.stderr.flush() |
| 119 | + """ |
| 120 | + ) |
| 121 | + |
| 122 | + server_params = StdioServerParameters( |
| 123 | + command=sys.executable, |
| 124 | + args=["-c", long_running_script], |
| 125 | + ) |
| 126 | + |
| 127 | + start_time = time.time() |
| 128 | + |
| 129 | + with anyio.move_on_after(8.0) as cancel_scope: |
| 130 | + async with stdio_client(server_params) as (read_stream, write_stream): |
| 131 | + # Immediately exit - this triggers cleanup while process is still running |
| 132 | + pass |
| 133 | + |
| 134 | + end_time = time.time() |
| 135 | + elapsed = end_time - start_time |
| 136 | + |
| 137 | + # On Windows: 2s (stdin wait) + 2s (terminate wait) + overhead = ~5s expected |
| 138 | + assert elapsed < 6.0, ( |
| 139 | + f"stdio_client cleanup took {elapsed:.1f} seconds, expected < 6.0 seconds. " |
| 140 | + f"This suggests the timeout mechanism may not be working properly." |
| 141 | + ) |
| 142 | + |
| 143 | + # Check if we timed out |
| 144 | + if cancel_scope.cancelled_caught: |
| 145 | + pytest.fail( |
| 146 | + "stdio_client cleanup timed out after 8.0 seconds. " |
| 147 | + "This indicates the cleanup mechanism is hanging and needs fixing." |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.anyio |
| 152 | +@pytest.mark.skipif(sys.platform == "win32", reason="Windows signal handling is different") |
| 153 | +async def test_stdio_client_sigint_only_process(): |
| 154 | + """ |
| 155 | + Test cleanup with a process that ignores SIGTERM but responds to SIGINT. |
| 156 | + """ |
| 157 | + # Create a Python script that ignores SIGTERM but handles SIGINT |
| 158 | + script_content = textwrap.dedent( |
| 159 | + """ |
| 160 | + import signal |
| 161 | + import sys |
| 162 | + import time |
| 163 | +
|
| 164 | + # Ignore SIGTERM (what process.terminate() sends) |
| 165 | + signal.signal(signal.SIGTERM, signal.SIG_IGN) |
| 166 | +
|
| 167 | + # Handle SIGINT (Ctrl+C signal) by exiting cleanly |
| 168 | + def sigint_handler(signum, frame): |
| 169 | + sys.exit(0) |
| 170 | +
|
| 171 | + signal.signal(signal.SIGINT, sigint_handler) |
| 172 | +
|
| 173 | + # Keep running until SIGINT received |
| 174 | + while True: |
| 175 | + time.sleep(0.1) |
| 176 | + """ |
| 177 | + ) |
| 178 | + |
| 179 | + server_params = StdioServerParameters( |
| 180 | + command=sys.executable, |
| 181 | + args=["-c", script_content], |
| 182 | + ) |
| 183 | + |
| 184 | + start_time = time.time() |
| 185 | + |
| 186 | + try: |
| 187 | + # Use anyio timeout to prevent test from hanging forever |
| 188 | + with anyio.move_on_after(5.0) as cancel_scope: |
| 189 | + async with stdio_client(server_params) as (read_stream, write_stream): |
| 190 | + # Let the process start and begin ignoring SIGTERM |
| 191 | + await anyio.sleep(0.5) |
| 192 | + # Exit context triggers cleanup - this should not hang |
| 193 | + pass |
| 194 | + |
| 195 | + if cancel_scope.cancelled_caught: |
| 196 | + raise TimeoutError("Test timed out") |
| 197 | + |
| 198 | + end_time = time.time() |
| 199 | + elapsed = end_time - start_time |
| 200 | + |
| 201 | + # Should complete quickly even with SIGTERM-ignoring process |
| 202 | + # This will fail if cleanup only uses process.terminate() without fallback |
| 203 | + assert elapsed < 5.0, ( |
| 204 | + f"stdio_client cleanup took {elapsed:.1f} seconds with SIGTERM-ignoring process. " |
| 205 | + f"Expected < 5.0 seconds. This suggests the cleanup needs SIGINT/SIGKILL fallback." |
| 206 | + ) |
| 207 | + except (TimeoutError, Exception) as e: |
| 208 | + if isinstance(e, TimeoutError) or "timed out" in str(e): |
| 209 | + pytest.fail( |
| 210 | + "stdio_client cleanup timed out after 5.0 seconds with SIGTERM-ignoring process. " |
| 211 | + "This confirms the cleanup needs SIGINT/SIGKILL fallback for processes that ignore SIGTERM." |
| 212 | + ) |
| 213 | + else: |
| 214 | + raise |
0 commit comments