The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): reconnect dead stdio child past proof deadline instead of spinning
Yagna Vudathu55b6116b454f
diff --git a/tests/tools/test_mcp_stdio_dead_reconnect_115483.py b/tests/tools/test_mcp_stdio_dead_reconnect_115483.pynew file mode 100644index 00000000000..cf46599115e--- /dev/null+++ b/tests/tools/test_mcp_stdio_dead_reconnect_115483.py@@ -0,0 +1,86 @@+"""Regression test for #115483: dead stdio child past the proof deadline.++Premise: in ``_wait_for_lifecycle_event`` the stdio-no-keepalive branch+computes ``timeout = max(0.0, proof_at - now)``, which sticks at 0 once the+proof deadline passes, and the expired-proof check no-ops on a dead child+(``... and not self._stdio_children_dead()``) then ``continue``s. With the+child dead and no events ever firing, the supervisor spins on zero-timeout+``asyncio.wait`` wakes forever instead of reconnecting.++Fix contract (narrow): at expired ``proof_at`` with dead stdio children,+return ``"reconnect"`` (failing stale in-flight calls) instead of+``continue``.+"""+import asyncio++import pytest++from tools.mcp_tool import MCPServerTask+import tools.mcp_tool_server_run as run_mod+++async def test_expired_proof_with_dead_stdio_children_reconnects(monkeypatch):+ task = MCPServerTask("test-stdio-dead")+ # Stdio config: no URL, no keepalive_interval.+ task._config = {"command": "true"}+ task._session_proven = False+ task.session = object()+ task._stdio_children_dead = lambda: True+ failed = []+ task._fail_inflight_calls = lambda reason: failed.append(reason)+ # Expire the proof deadline immediately.+ monkeypatch.setattr("tools.mcp_tool._DEFAULT_KEEPALIVE_INTERVAL", 0.0)++ real_wait = asyncio.wait+ calls = {"n": 0}++ async def fake_wait(waiters, timeout=None, return_when=None):+ calls["n"] += 1+ if calls["n"] == 1:+ # Past the deadline the supervisor must wake immediately...+ assert timeout is not None and timeout <= 1.0, timeout+ # ...and reconnect instead of spinning: nothing completes here.+ return set(), set(waiters)+ # Pre-fix fallback: the loop spun, so shut it down to terminate.+ task._shutdown_event.set()+ return await real_wait(+ waiters, timeout=1.0,+ return_when=return_when or asyncio.FIRST_COMPLETED,+ )++ monkeypatch.setattr(run_mod.asyncio, "wait", fake_wait)+ reason = await asyncio.wait_for(task._wait_for_lifecycle_event(), timeout=10)+ assert reason == "reconnect", reason+ assert failed == ["reconnect"], failed+ assert calls["n"] == 1, "must reconnect on the first expired-proof wake, not spin"+++async def test_expired_proof_with_live_stdio_children_still_proves(monkeypatch):+ """Guard: the fix must not break the healthy path — a live child at the+ expired proof deadline marks the session proven and keeps serving."""+ task = MCPServerTask("test-stdio-live")+ task._config = {"command": "true"}+ task._session_proven = False+ task.session = object()+ task._stdio_children_dead = lambda: False+ monkeypatch.setattr("tools.mcp_tool._DEFAULT_KEEPALIVE_INTERVAL", 0.0)++ real_wait = asyncio.wait+ calls = {"n": 0}++ async def fake_wait(waiters, timeout=None, return_when=None):+ calls["n"] += 1+ if calls["n"] == 1:+ return set(), set(waiters)+ task._shutdown_event.set()+ return await real_wait(+ waiters, timeout=1.0,+ return_when=return_when or asyncio.FIRST_COMPLETED,+ )++ monkeypatch.setattr(run_mod.asyncio, "wait", fake_wait)+ reason = await asyncio.wait_for(task._wait_for_lifecycle_event(), timeout=10)+ assert reason == "shutdown", reason+ assert task._session_proven is True, "live child past proof deadline proves the session"diff --git a/tools/mcp_tool_server_run.py b/tools/mcp_tool_server_run.pyindex 3f8b1137159..08fa5158d37 100644--- a/tools/mcp_tool_server_run.py+++ b/tools/mcp_tool_server_run.py@@ -113,7 +113,13 @@ class MCPServerRunMixin:# transports — clear the rapid-drop budget without pinging (#62212). An# earlier wake (a hidden-then-missed recycle deadline) is not that proof.if (not self._session_proven and proof_at is not None- and time.monotonic() >= proof_at and not self._stdio_children_dead()):+ and time.monotonic() >= proof_at):+ if self._stdio_children_dead():+ # The child died before the session ever proved itself: no+ # lifecycle event will ever fire and the zero-timeout wait+ # would spin forever — reconnect now, failing stale+ # in-flight calls at the tail below (#115483).+ breakself._mark_session_proven()continue# Timeout: probe for a stale session — NEVER while an RPC is in flight (a