Skip to content

The agent that grows with you

fix: a background git hook never keeps a completed update's checkout locked

Tekniumd02352efcec0
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.py
index acd16ed5e10..6801a40a90a 100644
--- a/hermes_cli/update_custody.py
+++ b/hermes_cli/update_custody.py
@@ -8,7 +8,7 @@ can still write the checkout, and must NOT leak into processes that outlive the
forks a detached gc/maintenance child that would inherit (POSIX) or outlive (Windows) the lock.
* POSIX: the lock fd is inherited ONLY by git commands that mutate the worktree, index or refs
locally (:data:`LOCAL_MUTATORS`, run with ``core.fsmonitor=false`` so no fsmonitor daemon
- starts under them, and with no credential helper). Network/credential commands (fetch,
+ starts under them, with no credential helper and no repository hooks). Network/credential commands (fetch,
ls-remote, credential) and readers run without it: a ``git credential-cache--daemon`` they
start never holds the checkout. A partial clone's mutator would lazily fetch the objects a move
needs as a child holding the fd (and start the daemon under it), so before a move the objects
@@ -39,6 +39,7 @@ from __future__ import annotations
import contextlib
import logging
+import os
import subprocess
import sys
from collections.abc import Sequence
@@ -50,8 +51,11 @@ logger = logging.getLogger(__name__)
GIT_NO_DETACH = ("-c", "gc.autoDetach=false", "-c", "maintenance.auto=false")
# Local mutators only (they hold the lock fd): never start an fsmonitor daemon under it, and no
# credential helper — a promisor lazy fetch under a mutator would start `git
-# credential-cache--daemon` holding the fd for its lifetime (900 s by default, m3).
-_MUTATOR_CONFIG = ("-c", "core.fsmonitor=false", "-c", "credential.helper=")
+# credential-cache--daemon` holding the fd for its lifetime (900 s by default, m3). No repository
+# hooks either (F1): a hook inherits the fd, and one that backgrounds a process (a post-merge
+# daemon) kept a completed update's checkout locked until it exited. Per command only: the
+# repository's own hook configuration is untouched.
+_MUTATOR_CONFIG = ("-c", "core.fsmonitor=false", "-c", "credential.helper=", "-c", f"core.hooksPath={os.devnull}")
# git subcommands that write the worktree, the index or refs on THIS machine. Only these inherit
# the checkout lock fd: if the updater dies mid-command, the checkout stays locked until git exits.
diff --git a/tests/hermes_cli/test_update_custody.py b/tests/hermes_cli/test_update_custody.py
index 02357d6ea97..0ae8a063feb 100644
--- a/tests/hermes_cli/test_update_custody.py
+++ b/tests/hermes_cli/test_update_custody.py
@@ -111,6 +111,42 @@ def test_lock_fd_reaches_local_mutators_only(repo, tmp_path, monkeypatch):
assert "yes" in out_stash.read_text(encoding="utf-8-sig").split(), "a local mutator's child lost the checkout lock"
[email protected]_system_guard_bypass # kills the orphaned hook child it planted
+def test_a_background_hook_never_keeps_a_completed_update_locked(repo, tmp_path):
+ """F1: a local mutator holds the lock fd, and a repository hook it ran inherited that fd; a
+ hook that backgrounds a daemon kept the checkout locked after the merge returned and the
+ owner released, so the next update was refused. Once the owner releases, a contender acquires."""
+ import signal
+
+ base = _git(repo, "rev-parse", "HEAD")
+ (repo / "f.txt").write_text("two\n", encoding="utf-8")
+ _git(repo, "commit", "-qam", "two")
+ target = _git(repo, "rev-parse", "HEAD")
+ _git(repo, "reset", "-q", "--hard", base)
+ pid_file = tmp_path / "hook-child.pid"
+ hook = repo / ".git" / "hooks" / "post-merge"
+ hook.write_text(f"#!/bin/sh\nsleep 60 </dev/null >/dev/null 2>&1 &\necho $! > {pid_file}\n", encoding="utf-8")
+ hook.chmod(0o755)
+ from hermes_cli.update_custody import run_git
+
+ lock = ul.UpdateLock(path=tmp_path / "marker", install_root=repo)
+ assert lock.acquire()
+ try:
+ merged = run_git(["git"], ["merge", "--ff-only", target], cwd=repo, capture_output=True, text=True, timeout=30)
+ assert merged.returncode == 0, merged.stderr
+ finally:
+ lock.release()
+ contender = ul.UpdateLock(path=tmp_path / "next-marker", install_root=repo)
+ try:
+ assert (repo / "f.txt").read_text(encoding="utf-8") == "two\n"
+ assert contender.acquire(), f"a background git hook kept the completed update's checkout locked: {contender.holder}"
+ finally:
+ contender.release()
+ if pid_file.exists():
+ os.kill(int(pid_file.read_text(encoding="utf-8")), signal.SIGKILL) # windows-footgun: ok - POSIX-only test
+
+
@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="parent-death signal is Linux-only")
def test_network_git_dies_with_its_killed_owner(repo, tmp_path):
"""The fd-less fetch must not keep rewriting refs after the owner died (the next owner's lease)."""