The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: a group kill of the build's caller no longer kills the custodian of its detached writers
Teknium20d8a122394d
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.pyindex 6801a40a90a..3e00d0ef34f 100644--- a/hermes_cli/update_custody.py+++ b/hermes_cli/update_custody.py@@ -566,8 +566,12 @@ def _join_launcher_python() -> str:# so nothing node starts (npm, esbuild, sh) keeps the checkout locked: custody of them rests on# this stdlib parent, which holds the fd until no descendant of the command is left.# * The command stays in the CALLER's process group: every group kill that stops a build (a-# Ctrl-C'd completion child's ``killpg``, Desktop's ``kill(-pid)``) reaches this launcher,-# node and everything under it that did not start a session of its own.+# Ctrl-C'd completion child's ``killpg``, Desktop's ``kill(-pid)``) reaches node and everything+# under it that did not start a session of its own. This launcher leaves that group (E): a+# SIGKILL of the group would otherwise kill the custodian too, and a descendant in a session of+# its own would go on writing with the checkout lock free. It outlives the kill, settles the+# tree as below, and only then exits (releasing the fd). A launcher started as its group's+# leader (a caller that gave it a session) cannot leave it; that group is then its own.# * Linux: the launcher is a child subreaper, so a descendant orphaned by node's exit (or by any# intermediate's) is re-parented to it, never to init. Once node exits it SIGKILLs and reaps# its children until none is left; it only signals its own unreaped children, so no pid it@@ -641,7 +645,10 @@ def kill(pids):except OSError:pass-p = subprocess.Popen(sys.argv[2:], pass_fds=fds)+caller = os.getpgrp()+if caller != me:+ os.setpgid(0, 0)+p = subprocess.Popen(sys.argv[2:], pass_fds=fds, process_group=caller)got = []def forward(signum, frame):diff --git a/tests/hermes_cli/test_update_custody.py b/tests/hermes_cli/test_update_custody.pyindex 0ae8a063feb..bdb77c64e1e 100644--- a/tests/hermes_cli/test_update_custody.py+++ b/tests/hermes_cli/test_update_custody.py@@ -462,6 +462,53 @@ def test_a_group_kill_of_the_caller_stops_the_whole_build(repo, tmp_path):assert not late_child.exists(), "a build descendant survived a group kill of its caller"[email protected](not sys.platform.startswith("linux"), reason="POSIX sessions + /proc")[email protected]_system_guard_bypass # the caller's group kill is the scenario under test+def test_a_group_kill_of_the_caller_keeps_custody_until_a_detached_writer_is_gone(repo, tmp_path):+ """E: the completion child ``killpg``s its own group on interruption; the launcher holding+ the lock fd for node's tree was in that group and died with it, so a descendant in a session+ of its own kept writing while a contender owned the checkout. A contender must only acquire+ once that writer has stopped."""+ import contextlib+ import signal++ beat = tmp_path / "beat"+ writer = (f"import os, pathlib, time; os.setsid(); os.closerange(3, 4096); p = pathlib.Path({str(beat)!r})\n" # windows-footgun: ok - Linux-only test+ "for i in range(300):\n p.write_text(f'{os.getpid()} {i}'); time.sleep(0.1)")+ caller = textwrap.dedent(f"""+ import subprocess, sys+ sys.path.insert(0, {str(REPO_ROOT)!r})+ from pathlib import Path+ from hermes_cli import update_lock as ul+ from hermes_cli.update_custody import contained_command+ repo = Path({str(repo)!r})+ lock = ul.UpdateLock(path=Path({str(tmp_path / "marker")!r}), install_root=repo)+ assert lock.acquire()+ with contained_command({_node_starting(writer, then="time.sleep(60)")!r}, root=repo) as (argv, custody):+ subprocess.Popen(argv, stdin=subprocess.DEVNULL, **custody).wait()+ """)+ owner = subprocess.Popen([sys.executable, "-c", caller], start_new_session=True)+ deadline = time.monotonic() + 20+ while not beat.exists() and time.monotonic() < deadline:+ time.sleep(0.1)+ assert beat.exists(), "the detached writer never started"+ os.killpg(owner.pid, signal.SIGKILL) # windows-footgun: ok - Linux-only test+ owner.wait(timeout=10)+ contender = ul.UpdateLock(path=tmp_path / "next-marker", install_root=repo)+ deadline = time.monotonic() + 20+ while not contender.acquire():+ assert time.monotonic() < deadline, "the checkout stayed locked after the group kill"+ time.sleep(0.1)+ try:+ seen = beat.read_text(encoding="utf-8")+ time.sleep(1)+ assert beat.read_text(encoding="utf-8") == seen, "a detached build writer kept writing under the next owner"+ finally:+ contender.release()+ with contextlib.suppress(ProcessLookupError):+ os.kill(int(beat.read_text(encoding="utf-8").split()[0]), signal.SIGKILL) # windows-footgun: ok - Linux-only test++@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="POSIX sessions + /proc")@pytest.mark.parametrize("platform", [None, "darwin"], ids=["subreaper", "ps-walk"])def test_a_detached_grandchild_outliving_node_dies_before_the_build_returns(repo, tmp_path, platform):