Skip to content

The agent that grows with you

fix(mcp): an owned spawn racing MCP discovery no longer hands MCP the owner-death supervisor

kshitijk4poor41d0a947f4bc
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index fa2d059cb2b..a6473434cb3 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -823,6 +823,14 @@ def _spawn_supervisor():
return None
+def is_owner_death_supervisor(pid: int, argv: Sequence[str]) -> bool:
+ """True when ``pid`` is this process's supervisor. Another family's child discovery (MCP's
+ before/after child-pid delta) can catch it starting and must never adopt, and later kill, it."""
+ proc = _supervisor
+ return (proc is not None and proc.pid == pid
+ and any(arg == str(_SUPERVISOR_SCRIPT) for arg in argv[1:]))
+
+
def _prune_dead_supervised_pgids() -> set:
"""Forget supervised groups with no members left; return what went. Caller holds
``_supervisor_lock``. Signal 0 is a pure existence probe (cannot terminate anything). It
diff --git a/tests/tools/test_mcp_child_filter_supervisor.py b/tests/tools/test_mcp_child_filter_supervisor.py
new file mode 100644
index 00000000000..d8783cee480
--- /dev/null
+++ b/tests/tools/test_mcp_child_filter_supervisor.py
@@ -0,0 +1,40 @@
+"""MCP's spawn-delta filter never adopts the owner-death supervisor.
+
+The supervisor is a direct child of this process, started lazily by the first owned spawn of any
+family. When that happens between MCP's before/after child snapshots, the delta contains it; adopting
+it would let MCP teardown group-kill the one process that reaps every other owned child.
+"""
+
+import subprocess
+import sys
+
+import pytest
+
+from hermes_cli import process_identity
+from tools import mcp_tool_lifecycle
+
+pytestmark = pytest.mark.platforms("posix")
+
+
+def test_an_owned_spawn_racing_mcp_discovery_does_not_hand_mcp_the_supervisor(monkeypatch):
+ monkeypatch.setattr(process_identity, "_supervisor", None)
+ monkeypatch.setattr(process_identity, "_supervised_pgids", set())
+ monkeypatch.setattr(process_identity, "register_child", lambda *a, **k: True)
+ before = mcp_tool_lifecycle._snapshot_child_pids()
+ # Another family's first owned spawn lands inside MCP's before/after window: it starts the supervisor.
+ worker = process_identity.spawn_owned([sys.executable, "-c", "import sys; sys.stdin.read()"],
+ purpose="test", stdin=subprocess.PIPE)
+ mcp_server = subprocess.Popen([sys.executable, "-c", "import sys; sys.stdin.read()"], stdin=subprocess.PIPE)
+ try:
+ supervisor = process_identity._supervisor
+ assert supervisor is not None
+ delta = mcp_tool_lifecycle._snapshot_child_pids() - before
+ assert supervisor.pid in delta, "precondition: the supervisor is in MCP's spawn delta"
+ assert supervisor.pid not in mcp_tool_lifecycle._filter_mcp_children(delta)
+ assert mcp_server.pid in mcp_tool_lifecycle._filter_mcp_children(delta)
+ finally:
+ for proc in (worker, mcp_server):
+ proc.stdin.close()
+ proc.wait(timeout=10)
+ process_identity.release_process_group_once_empty(worker.pid, proc=worker, timeout=5)
+ assert process_identity._supervisor is None, "releasing the last group stops the supervisor"
diff --git a/tools/mcp_tool_lifecycle.py b/tools/mcp_tool_lifecycle.py
index 0d406110cfa..78fec7c06f2 100644
--- a/tools/mcp_tool_lifecycle.py
+++ b/tools/mcp_tool_lifecycle.py
@@ -6,7 +6,7 @@ import asyncio
import os
import time
from typing import Dict, Optional
-from hermes_cli.process_identity import release_process_groups
+from hermes_cli.process_identity import is_owner_death_supervisor, release_process_groups
from tools.mcp_tool_common import _core
from tools import mcp_tool_loop as _loop
@@ -102,6 +102,8 @@ def _filter_mcp_children(pids: set) -> set:
argv = psutil.Process(pid).cmdline()
except (psutil.NoSuchProcess, psutil.AccessDenied, OSError):
continue # raced away or zombie — cannot be our fresh server, unsafe to track
+ if is_owner_death_supervisor(pid, argv):
+ continue # the shared reaper of every owned child; MCP teardown must never kill it
if not any(marker in arg for arg in argv[1:] for marker in _NON_MCP_CHILD_CMDLINE_MARKERS):
kept.add(pid)
return kept