Skip to content

The agent that grows with you

fix(update): never killpg a git process group after the leader was reaped

Teknium268f161d089a
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.py
index 6c7b5b93ba4..0f9422da881 100644
--- a/hermes_cli/update_custody.py
+++ b/hermes_cli/update_custody.py
@@ -381,11 +381,16 @@ def _owner_watch(pid: int):
def _kill_tree(proc: subprocess.Popen) -> None:
- """SIGKILL ``proc``'s process group: the child leads it (``start_new_session``) and is not
- reaped yet, so the group id cannot have been reused."""
+ """SIGKILL ``proc``'s process group: the child leads it (``start_new_session``), and while it
+ is unreaped its pid, hence the group id, cannot be reused. Once reaped (``communicate`` reaps
+ on a Ctrl-C inside its wait window) the id may already name another session, so it is never
+ signalled; ``returncode`` is read, not ``poll()``ed, since polling would reap a live zombie
+ and skip the helpers a timed-out git leaves running."""
import os
import signal
+ if proc.returncode is not None:
+ return
with contextlib.suppress(OSError): # already gone
os.killpg(proc.pid, signal.SIGKILL) # windows-footgun: ok -- POSIX only (run_git gates win32 out)
diff --git a/tests/hermes_cli/test_update_custody_tree.py b/tests/hermes_cli/test_update_custody_tree.py
index 6b0b7ed807a..81574f68c5a 100644
--- a/tests/hermes_cli/test_update_custody_tree.py
+++ b/tests/hermes_cli/test_update_custody_tree.py
@@ -109,3 +109,22 @@ def test_a_killed_owner_takes_the_fetch_helpers_with_it(fetch_rig, tmp_path):
finally:
if owner.poll() is None:
owner.kill()
+
+
+def test_a_reaped_git_never_signals_the_group_its_pid_now_names():
+ """``communicate`` reaps git on a Ctrl-C inside its wait window, and the cleanup then reaches
+ ``_kill_tree``. A reaped pid may already lead another session; signalling it as a group would
+ SIGKILL that unrelated process. The decoy stands in for the session that reused the id."""
+ from hermes_cli.update_custody import _kill_tree
+
+ git = subprocess.Popen([sys.executable, "-c", "pass"], start_new_session=True)
+ git.wait()
+ decoy = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"], start_new_session=True)
+ try:
+ git.pid = decoy.pid # the reused id
+ _kill_tree(git)
+ assert not _gone_within(decoy.pid, 0.5), "a reaped handle signalled another session's group"
+ finally:
+ decoy.kill()
+ decoy.wait()