Skip to content

The agent that grows with you

fix(mcp): respawn the death supervisor on any lifecycle event while groups remain

kshitijk4poor8f5749db4b22
diff --git a/tests/tools/test_mcp_death_supervisor.py b/tests/tools/test_mcp_death_supervisor.py
index c10a49db4d3..cf78ee986d9 100644
--- a/tests/tools/test_mcp_death_supervisor.py
+++ b/tests/tools/test_mcp_death_supervisor.py
@@ -532,6 +532,45 @@ def test_a_broken_pipe_never_propagates_into_a_live_mcp_session(monkeypatch, all
assert mcp_tool._death_supervisor is None
+def test_unregister_after_a_broken_pipe_rebuilds_coverage_for_survivors(monkeypatch, all_groups_alive):
+ """A lost supervisor must be replaced by the NEXT lifecycle event, whatever its verb.
+
+ Sequence from the #93517 review: two groups live, the control pipe dies
+ (write fails, supervisor dropped, set retained), then a clean teardown
+ unregisters one of them. Keying the no-spawn fast path on the verb left
+ the survivor recorded but unsupervised; it must be keyed on the set.
+ """
+ spawned = []
+
+ def _spawn():
+ fake = _FakeSupervisor()
+ spawned.append(fake)
+ return fake
+
+ monkeypatch.setattr(mcp_tool, "_spawn_death_supervisor", _spawn)
+ mcp_tool._update_death_supervisor("register", [111, 222])
+
+ class _DeadStdin:
+ def write(self, _payload):
+ raise BrokenPipeError("supervisor died")
+
+ def flush(self):
+ pass
+
+ spawned[0].stdin = _DeadStdin()
+ mcp_tool._update_death_supervisor("register", [333]) # the write fails; supervisor dropped
+ assert mcp_tool._death_supervisor is None
+ assert mcp_tool._supervised_pgids == {111, 222, 333}
+
+ mcp_tool._update_death_supervisor("unregister", [222])
+
+ assert len(spawned) == 2, "unregister after a lost supervisor did not respawn one"
+ assert sorted(spawned[1].lines()) == ["register 111", "register 333"], (
+ "the replacement did not receive the surviving groups"
+ )
+ assert mcp_tool._death_supervisor is spawned[1]
+
+
def test_a_supervisor_that_cannot_start_is_not_fatal(monkeypatch, all_groups_alive):
monkeypatch.setattr(mcp_tool, "_spawn_death_supervisor", lambda: None)
diff --git a/tools/mcp_tool.py b/tools/mcp_tool.py
index 5603558c4f1..d4832692135 100644
--- a/tools/mcp_tool.py
+++ b/tools/mcp_tool.py
@@ -1184,8 +1184,13 @@ def _update_death_supervisor(verb: str, pgids) -> None:
proc = _death_supervisor
if proc is None or proc.poll() is not None:
- if verb == "unregister" and proc is None:
- # Nothing was ever registered, so there is nothing to tell.
+ if not _supervised_pgids:
+ # Nothing left to cover, so there is nothing to tell -- and
+ # nothing to respawn a supervisor for. Keyed on the SET, not
+ # on the verb: after a broken-pipe write dropped the
+ # supervisor while groups were still registered, an
+ # unregister of one of them must still rebuild coverage for
+ # the survivors (review finding on #93517).
return
proc = _spawn_death_supervisor()
_death_supervisor = proc