The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): a dead MCP server's grandchildren are reaped again (main e2e red since #131910)
teknium1d526f14714ce
diff --git a/tests/tools/test_pgid_starttime_drift.py b/tests/tools/test_pgid_starttime_drift.pyindex 0bbaae36f3f..a15d5161f3c 100644--- a/tests/tools/test_pgid_starttime_drift.py+++ b/tests/tools/test_pgid_starttime_drift.py@@ -79,7 +79,8 @@ def test_local_guard_still_refuses_recycled_pgid(monkeypatch, killpg_calls):def test_local_guard_unreadable_start_time_live_leader_is_best_effort(monkeypatch, killpg_calls):"""An unreadable current start time for a still-live leader keeps the legacy- best-effort kill; a gone leader still refuses the possibly-recycled number."""+ best-effort kill, and so does a gone leader: POSIX never reuses a PGID while a+ group member lives, so its reparented grandchildren must still be reached."""from tools.environments.local import _kill_process_group_posix, _leader_is_oursmonkeypatch.setattr("gateway.status.get_process_start_time", lambda pid: None)@@ -88,7 +89,7 @@ def test_local_guard_unreadable_start_time_live_leader_is_best_effort(monkeypatcassert (67890, signal.SIGTERM) in killpg_callsmonkeypatch.setattr("gateway.status._pid_exists", lambda pid: False)- assert _leader_is_ours(67890, RECORDED) is False+ assert _leader_is_ours(67890, RECORDED) is True# --- stdio MCP orphan reaper ------------------------------------------------diff --git a/tools/environments/local.py b/tools/environments/local.pyindex e2f2bf3fa2a..58745d47add 100644--- a/tools/environments/local.py+++ b/tools/environments/local.py@@ -847,21 +847,23 @@ def _leader_is_ours(pgid, expected_start) -> bool:come from the same host at different times, so they go through the drift-tolerantfingerprint comparator — same-host readings drift ~1 s on macOS (#117505), andexact equality made the guard refuse to kill live, legitimately-owned groups.- When no baseline was captured, or the current reading is unreadable while the- leader is still alive, keep the legacy best-effort behaviour rather than- refusing to kill; an unreadable reading for a gone leader means the PID/PGID may- have been recycled, so refuse."""+ When no baseline was captured, or the current reading is unreadable, keep the+ legacy best-effort behaviour rather than refusing to kill: only a LIVE leader with+ a different start time proves recycling."""if pgid is None:return Falseif expected_start is None:return True- from gateway.status import _pid_exists, get_process_start_time, start_time_fingerprints_match+ from gateway.status import get_process_start_time, start_time_fingerprints_matchtry:current = get_process_start_time(pgid)except Exception: # noqa: BLE001 — the guard must never break signallingreturn Trueif current is None:- return _pid_exists(pgid)+ # Unreadable while alive: best effort. Gone: POSIX never reuses a PGID while any+ # member of the group lives, so the group (if it still exists) is ours and its+ # reparented grandchildren still need the signal; an empty group is just ESRCH.+ return Truetry:return start_time_fingerprints_match(expected_start, current)except (TypeError, ValueError): # junk fingerprints: best-effort, never break signallingdiff --git a/tools/mcp_tool_lifecycle.py b/tools/mcp_tool_lifecycle.pyindex 4c0c801aa61..73a3332c26a 100644--- a/tools/mcp_tool_lifecycle.py+++ b/tools/mcp_tool_lifecycle.py@@ -274,19 +274,12 @@ def _signal_mcp_process(pid: int, sig: int, server_name: str, pgid: Optional[intat spawn and no longer matches — compared drift-tolerantly, because same-host readingsdrift ~1 s on macOS (#117505) and exact equality skipped live, legitimately-owned servers— skip entirely. Without a baseline (the capture raced the child's exit), or when the- current reading is unreadable while the PID is still alive, fall through to the legacy+ current reading is unreadable (leader reaped: POSIX never reuses a PGID while a member+ lives, so its reparented grandchildren are still ours), fall through to the legacybest-effort path."""if expected_start is not None:current = _leader_start_time(pid)- if current is None:- from gateway.status import _pid_exists- if not _pid_exists(pid):- logger.debug(- "Skip signalling MCP pid %d (%s): leader start time unreadable and the "- "PID is gone — it was likely recycled; refusing to kill an unrelated "- "process group.", pid, server_name)- return- else:+ if current is not None:try:from gateway.status import start_time_fingerprints_matchif not start_time_fingerprints_match(expected_start, current):