diff --git a/tests/tools/test_mcp_stability.py b/tests/tools/test_mcp_stability.py index 494ebbbe024..796dbed913d 100644 --- a/tests/tools/test_mcp_stability.py +++ b/tests/tools/test_mcp_stability.py @@ -560,7 +560,7 @@ class TestMCPInitialConnectionRetry: asyncio.get_event_loop().run_until_complete(_run()) def test_initial_connect_gives_up_after_max_retries(self): - """Server gives up after _MAX_INITIAL_CONNECT_RETRIES failures.""" + """Server parks (does not exit) after _MAX_INITIAL_CONNECT_RETRIES failures.""" from tools.mcp_tool import MCPServerTask, _MAX_INITIAL_CONNECT_RETRIES call_count = 0 @@ -583,8 +583,13 @@ class TestMCPInitialConnectionRetry: assert "DNS resolution failed" in str(server._error) # 1 initial + N retries = _MAX_INITIAL_CONNECT_RETRIES + 1 total attempts assert call_count == _MAX_INITIAL_CONNECT_RETRIES + 1 + # The task parks for later revival instead of exiting. + await asyncio.sleep(0) + assert not task.done(), "run task should park, not exit" - await task + server._shutdown_event.set() + server._reconnect_event.set() + await asyncio.wait_for(task, timeout=5) asyncio.get_event_loop().run_until_complete(_run()) diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.py index 6264b507a86..eeeeef7e48c 100644 --- a/tests/tools/test_mcp_tool.py +++ b/tests/tools/test_mcp_tool.py @@ -1876,12 +1876,19 @@ class TestReconnection: with patch.object(MCPServerTask, "_run_stdio", patched_run_stdio), \ patch("asyncio.sleep", new_callable=AsyncMock): - await server.run({"command": "test"}) + task = asyncio.ensure_future(server.run({"command": "test"})) + await server._ready.wait() - # Now retries up to _MAX_INITIAL_CONNECT_RETRIES before giving up - assert run_count == _MAX_INITIAL_CONNECT_RETRIES + 1 - assert server._error is not None - assert "cannot connect" in str(server._error) + # Now retries up to _MAX_INITIAL_CONNECT_RETRIES, then PARKS + # (keeps the task alive for later revival) instead of exiting. + assert run_count == _MAX_INITIAL_CONNECT_RETRIES + 1 + assert server._error is not None + assert "cannot connect" in str(server._error) + assert not task.done(), "run task should park, not exit" + + server._shutdown_event.set() + server._reconnect_event.set() + await asyncio.wait_for(task, timeout=5) asyncio.run(_test())