Skip to content

The agent that grows with you

fix(mcp): recognise the supervisor by its argv, not by _supervisor.pid

kshitijk4poor945c323c0d90
diff --git a/hermes_cli/local_runtime/supervisor.py b/hermes_cli/local_runtime/supervisor.py
index 9a12d8e28b4..442624c65b1 100644
--- a/hermes_cli/local_runtime/supervisor.py
+++ b/hermes_cli/local_runtime/supervisor.py
@@ -297,7 +297,7 @@ class LlamaServerSupervisor:
if self._job is not None:
self._job.close()
self._job = None
- if self.proc is not None and sys.platform != "win32":
+ if self.proc is not None:
# A router that crashed (the watcher's restart backoff) can leave its model children
# running in its group: stop them, then release the group only once it is empty, so
# a tree we failed to kill stays covered by the owner-death supervisor.
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index 65913add905..c5182de6775 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -843,12 +843,12 @@ 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 is_owner_death_supervisor(argv: Sequence[str]) -> bool:
+ """True when a direct child's ``argv`` is this install's supervisor (``sh <script> <pgid>``).
+ Another family's child discovery (MCP's before/after child-pid delta) can catch it starting and
+ must never adopt, and later kill, it. Matched on the script path, not ``_supervisor.pid``: that
+ is assigned only after the spawn returns, so a scan racing the spawn would miss it."""
+ return list(argv[1:2]) == [str(_SUPERVISOR_SCRIPT)]
def _prune_dead_supervised_pgids() -> set:
diff --git a/tests/tools/test_mcp_child_filter_supervisor.py b/tests/tools/test_mcp_child_filter_supervisor.py
index d8783cee480..90d26f5e683 100644
--- a/tests/tools/test_mcp_child_filter_supervisor.py
+++ b/tests/tools/test_mcp_child_filter_supervisor.py
@@ -38,3 +38,15 @@ def test_an_owned_spawn_racing_mcp_discovery_does_not_hand_mcp_the_supervisor(mo
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"
+
+
+def test_a_supervisor_whose_spawn_has_not_returned_yet_is_still_not_an_mcp_child(monkeypatch):
+ """``_supervisor`` is assigned only after the spawn returns; a scan in that gap must still skip it."""
+ monkeypatch.setattr(process_identity, "_supervisor", None)
+ supervisor = process_identity._spawn_supervisor() # running, but not yet published as _supervisor
+ assert supervisor is not None
+ try:
+ assert mcp_tool_lifecycle._filter_mcp_children({supervisor.pid}) == set()
+ finally:
+ supervisor.stdin.close()
+ supervisor.wait(timeout=10)
diff --git a/tools/mcp_tool_lifecycle.py b/tools/mcp_tool_lifecycle.py
index 78fec7c06f2..42887568697 100644
--- a/tools/mcp_tool_lifecycle.py
+++ b/tools/mcp_tool_lifecycle.py
@@ -102,7 +102,7 @@ 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):
+ if is_owner_death_supervisor(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)