Skip to content

The agent that grows with you

fix(process): a failed yield-to-background handoff gives the foreground wait its group back

kshitijk4poorb197ffca1eb0
diff --git a/tests/tools/test_terminal_owned_jobs.py b/tests/tools/test_terminal_owned_jobs.py
index 5f77cafd9c2..28c749825d3 100644
--- a/tests/tools/test_terminal_owned_jobs.py
+++ b/tests/tools/test_terminal_owned_jobs.py
@@ -197,6 +197,33 @@ def test_a_command_yielded_to_background_keeps_its_group_until_the_job_ends(monk
assert calls.count(("release", [session.pid])) == 1, calls
[email protected]("jobs_outlive_host", [False, True], ids=["owned-jobs", "gateway"])
+def test_a_failed_handoff_leaves_the_foreground_owner_its_group(monkeypatch, tmp_path, jobs_outlive_host):
+ """adopt_local moved the group to the job before its reader started. If the reader cannot start,
+ the foreground wait resumes: it must still hold the group, covered, so its completion releases
+ it (a gateway had already released it; elsewhere the dead session kept it)."""
+ import types
+
+ from tools.process_registry import ProcessRegistry
+
+ calls = _record_calls(monkeypatch)
+ registry = ProcessRegistry()
+ registry._jobs_outlive_host = jobs_outlive_host
+
+ def no_reader(*_a, **_kw):
+ raise RuntimeError("can't start new thread")
+
+ monkeypatch.setattr(registry, "_track_started", no_reader)
+ proc = types.SimpleNamespace(pid=4242, _hermes_owned_pgid=4242, poll=lambda: None)
+ with pytest.raises(RuntimeError):
+ registry.adopt_local(proc, command="sleep 60", cwd=str(tmp_path))
+
+ assert proc._hermes_owned_pgid == 4242, "the foreground wait lost its group"
+ covered = not calls or calls[-1] == ("own", [4242])
+ assert covered, f"the group is left uncovered: {calls}"
+ assert not registry._running, "a session with no reader stayed registered"
+
+
def test_unowned_jobs_are_never_handed_to_the_supervisor(monkeypatch, tmp_path):
from tools.process_registry import ProcessRegistry
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 52defc75c7a..1e14aa0e1f4 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -1256,10 +1256,16 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
session.pid = proc.pid
session.host_start_time = self._safe_host_start_time(session.pid)
session.notify_on_complete = notify_on_complete
+ handed_pgid = getattr(proc, "_hermes_owned_pgid", None)
self._take_handoff_group(session, proc)
if output_so_far:
session.append_output(output_so_far)
- self._track_started(session, self._reader_loop, f"proc-reader-{session.id}")
+ try:
+ self._track_started(session, self._reader_loop, f"proc-reader-{session.id}")
+ except Exception:
+ # The caller resumes its foreground wait: give it back its group, still covered.
+ self._return_handoff_group(session, proc, handed_pgid)
+ raise
return session
def spawn_via_env(
diff --git a/tools/process_registry_ownership.py b/tools/process_registry_ownership.py
index 40bc8df10cd..f524444ae56 100644
--- a/tools/process_registry_ownership.py
+++ b/tools/process_registry_ownership.py
@@ -61,6 +61,15 @@ class ProcessOwnershipMixin:
elif owned_pgid is not None:
release_process_groups([owned_pgid])
+ def _return_handoff_group(self, session: Any, proc: subprocess.Popen, owned_pgid: Any) -> None:
+ """The handoff failed (the reader could not start): the foreground wait resumes and must own
+ the group again, covered by the supervisor, so its own completion releases it."""
+ with self._owned_pgid_lock:
+ session._owned_pgid = None
+ if owned_pgid is not None:
+ own_process_groups([owned_pgid]) # re-covers a group the gateway path released
+ proc._hermes_owned_pgid = owned_pgid
+
def _release_job_group(self, session: Any) -> None:
"""The job's own process exited or was killed: stop covering its group. What it left behind on
purpose (`server &`) is no longer this job's, exactly as before."""