Skip to content

The agent that grows with you

fix(update): a git exiting mid-scan no longer makes the index.lock owner unknowable

Tekniumd4e7b6595655
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 53d498071c7..8630b0fc699 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -686,6 +686,18 @@ def _counts_as_holder(args: list[str], any_git: bool) -> bool:
return sub not in _NEVER_LOCKS and (any_git or sub not in _READER_GIT)
+def _exited(pid_dir: Path) -> bool:
+ """The process has released its memory (exiting, or a zombie) or is gone.
+
+ The kernel drops its fds before its cwd, and refuses its cwd/environ once it is exiting, so an
+ unreadable cwd there is not a hidden git: counting it made any git exiting anywhere on a busy
+ machine postpone the repair. A live process keeps a non-empty argv."""
+ try:
+ return not (pid_dir / "cmdline").read_bytes()
+ except OSError:
+ return not pid_dir.exists()
+
+
def _held_open_proc(path: Path, root: Path | None, any_git: bool) -> _Holder | bool | None:
proc = Path("/proc")
target = os.path.realpath(path)
@@ -734,6 +746,8 @@ def _held_open_proc(path: Path, root: Path | None, any_git: bool) -> _Holder | b
env = dict(entry.decode("utf-8", "replace").partition("=")[::2]
for entry in (pid_dir / "environ").read_bytes().split(b"\0") if entry)
except OSError:
+ if _exited(pid_dir):
+ continue # exited since its argv was read: no cwd/environ left, and it holds nothing
if git_works_in(args, None, None, places):
return _Holder(f"pid {pid_dir.name} ({name(args, pid_dir)})")
if pid_dir.exists() and _could_write(uid, path.parent):
diff --git a/tests/hermes_cli/test_gitlock.py b/tests/hermes_cli/test_gitlock.py
index 08691295b50..5b78ff4ccde 100644
--- a/tests/hermes_cli/test_gitlock.py
+++ b/tests/hermes_cli/test_gitlock.py
@@ -462,6 +462,38 @@ def test_a_killed_updates_lock_stays_while_any_git_works_in_the_checkout(repo: P
assert release_dead_index_lock(repo) is True, "once the checkout's git is gone the killed update's lock goes"
[email protected](not Path("/proc/self/fd").is_dir(), reason="ownership proof via /proc (Linux)")
+def test_a_git_elsewhere_exiting_mid_scan_is_gone_not_a_hidden_holder(repo: Path, tmp_path: Path, monkeypatch) -> None:
+ """A git in ANOTHER repository that exits between the scan reading its argv and its cwd/environ
+ (unreadable once it has released its memory) is gone: it holds nothing and takes no lock. Read
+ as a hidden git that may work here, any busy machine's git churn postponed the launch repair
+ ("a running git holds the index after an interrupted `hermes update`")."""
+ from hermes_cli import _early_recovery as er
+ from tests.waiting import wait_until
+
+ other = tmp_path / "other"
+ shutil.copytree(repo, other)
+ lock = repo / ".git" / "index.lock"
+ lock.write_bytes(b"") # a killed git's: nothing holds it
+ marker = "hermes.test=exits-mid-scan"
+ proc = _git_in_the_editor(other, tmp_path, ["git", "-c", marker, "commit", "-a"])
+ real = er._counts_as_holder
+
+ def exits_after_its_argv_was_read(args: list[str], any_git: bool) -> bool:
+ if marker in args and proc.returncode is None:
+ Path(f"{other / '.git' / 'COMMIT_EDITMSG'}.go").write_text("", encoding="utf-8")
+ wait_until(lambda: Path(f"/proc/{proc.pid}/stat").read_bytes().rsplit(b") ", 1)[1][:1] == b"Z",
+ what="the other repository's git to exit (unreaped)")
+ return real(args, any_git)
+
+ monkeypatch.setattr(er, "_counts_as_holder", exits_after_its_argv_was_read)
+ try:
+ assert er._held_open(lock, repo) is False
+ finally:
+ Path(f"{other / '.git' / 'COMMIT_EDITMSG'}.go").write_text("", encoding="utf-8")
+ proc.wait(timeout=20)
+
+
def test_lock_keeping_git_is_recognised_in_every_form_and_by_path_components(tmp_path: Path, monkeypatch) -> None:
"""Review P1/secondary, the hosts with no /proc: the macOS branch had only an (empty) ``lsof``,
and the Windows scan matched ``git.exe`` alone and compared paths with ``startswith``