Skip to content

The agent that grows with you

fix(update): a killed git's index.lock no longer blocks the next update

Tekniuma81d3408bc18
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index 6da3fbfb8a2..7a382b75e59 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -107,6 +107,29 @@ def clear_stale_git_locks(repo_root: Path, *, min_age_seconds: Optional[int] = N
)
+def release_dead_index_lock(repo_root: Path) -> bool:
+ """Drop ``.git/index.lock`` when the git that took it is proven gone, whatever its age.
+
+ A git killed while it held the index lock (an update's tree killed by the user, a probe killed
+ by its own timeout) leaves it behind, and every later merge/stash/reset refuses with "File
+ exists". The age floor above keeps such a lock for 10 minutes, so the next ``hermes update``
+ died at its fast-forward (#132089; the Windows crash cell ``mid_fetch``). Ownership is the
+ launch-time repair's proof (``_early_recovery._release_dead_index_lock``): a live holder keeps
+ it. Windows can only prove it by the unlink, which a lock-keeping git with its fd closed (a
+ ``git commit`` waiting in the editor) would not stop, so there any running git keeps it. An
+ interrupted tree move owns its own lock judgement, so its marker defers to that repair.
+ """
+ from hermes_cli._early_recovery import _git_dir, _release_dead_index_lock, interrupted_pull_marker
+
+ root = Path(repo_root)
+ git_dir = _git_dir(root)
+ if not (git_dir / "index.lock").exists() or interrupted_pull_marker(root).exists():
+ return False
+ if os.name == "nt" and _git_proc_running():
+ return False
+ return _release_dead_index_lock(git_dir, root)
+
+
def _pack_dir(repo_root: Path) -> Path:
git_dir = Path(repo_root) / ".git"
return (git_dir if git_dir.is_dir() else Path(repo_root)) / "objects" / "pack"
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index dc903fd98ac..810413bd0fc 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -1792,7 +1792,12 @@ def _cmd_update_impl(args, gateway_mode: bool):
# before anything else touches the checkout. Run at START, not only in the apply path
# below: a run that dies between here and the fetch would otherwise leave the next run to
# fail with "File exists" until an operator removed the lock by hand (#132089). Idempotent,
- # and _sweep_stale skips everything while a git process holds it.
+ # and _sweep_stale skips everything while a git process holds it. A killed update's
+ # index.lock is younger than the sweep's age floor, so it goes here once its git is proven
+ # dead (we hold the update lock: no other update's git can own it).
+ from hermes_cli.gitlock import release_dead_index_lock
+ if release_dead_index_lock(_m().PROJECT_ROOT):
+ print(" (removed .git/index.lock left by a git that was killed)")
_check.clear_git_debris(_m().PROJECT_ROOT)
opts = _resolve_update_options(args, gateway_mode)
diff --git a/hermes_cli/version_info.py b/hermes_cli/version_info.py
index fd0c38ffe8a..75bf27e39ee 100644
--- a/hermes_cli/version_info.py
+++ b/hermes_cli/version_info.py
@@ -217,7 +217,11 @@ def _git_version_info(repo_dir: Path, *, include_untracked: bool = False) -> Ver
# -uno: skip the untracked-file scan. This runs on the startup-banner
# path, and a full working-tree walk costs real time on large or cold
# checkouts. Same semantics as write_install_stamp.py.
- status_command = ["git", "status", "--porcelain"]
+ # The 3 s timeout kills a slow status (a partial clone lazily fetching a tree, a cold or
+ # scanned disk), and a status killed while refreshing the index strands .git/index.lock:
+ # the update's own merge then dies on "File exists" (#132089). A read-only probe takes
+ # no optional lock.
+ status_command = ["git", "--no-optional-locks", "status", "--porcelain"]
if not include_untracked:
status_command.append("-uno")
dirty_result = subprocess.run(
diff --git a/tests/hermes_cli/test_gitlock.py b/tests/hermes_cli/test_gitlock.py
index 265a286e09f..c14d6154ead 100644
--- a/tests/hermes_cli/test_gitlock.py
+++ b/tests/hermes_cli/test_gitlock.py
@@ -324,3 +324,42 @@ def test_non_partial_checkout_is_left_alone(repo: Path) -> None:
keys = subprocess.run(["git", "config", "--local", "--get-regexp", "maintenance|writecommitgraph"], cwd=repo,
capture_output=True, text=True).stdout
assert keys == "", "a full clone keeps git's stock maintenance"
+
+
+# ---- a killed git's index.lock (#132089) ----
+
+
+def _status_blocked_on_a_fifo(repo: Path, *argv: str) -> subprocess.Popen:
+ """A real ``git status`` that takes ``.git/index.lock`` and then blocks: its untracked scan
+ opens a FIFO ``.gitignore`` nobody writes."""
+ (repo / "junk").mkdir()
+ os.mkfifo(repo / "junk" / ".gitignore")
+ (repo / "junk" / "x").touch()
+ (repo / "a.txt").touch() # stat-dirty: status refreshes (and so locks) the index
+ proc = subprocess.Popen(["git", *argv, "status", "--porcelain"], cwd=repo,
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ deadline = time.monotonic() + 20
+ while not (repo / ".git" / "index.lock").exists():
+ assert proc.poll() is None and time.monotonic() < deadline, "git status never took index.lock"
+ time.sleep(0.05)
+ return proc
+
+
[email protected](not Path("/proc/self/fd").is_dir(), reason="ownership proof via /proc (Linux)")
+def test_a_killed_gits_index_lock_goes_at_once_and_a_live_ones_stays(repo: Path) -> None:
+ """The next update must not die on the lock a killed one left (the age floor kept it for 10
+ minutes), and must never take the lock of a git that is still running."""
+ from hermes_cli.gitlock import release_dead_index_lock
+
+ lock = repo / ".git" / "index.lock"
+ proc = _status_blocked_on_a_fifo(repo)
+ try:
+ assert release_dead_index_lock(repo) is False
+ assert lock.exists()
+ finally:
+ proc.kill()
+ proc.wait()
+
+ assert lock.exists(), "premise: a SIGKILLed status strands index.lock"
+ assert release_dead_index_lock(repo) is True
+ assert not lock.exists()
diff --git a/tests/hermes_cli/test_version_info.py b/tests/hermes_cli/test_version_info.py
index c0dddeda20f..da3f269a4d6 100644
--- a/tests/hermes_cli/test_version_info.py
+++ b/tests/hermes_cli/test_version_info.py
@@ -1,10 +1,14 @@
import json
+import os
from pathlib import Path
import subprocess
+import pytest
+
from hermes_cli.version_info import (
VersionInfo,
_derived_version,
+ _git_version_info,
_reset_version_info_cache,
_resolve_stamp_file,
_stamp_version_info,
@@ -274,3 +278,25 @@ def test_old_updater_version_stub_reads_the_same_stamp_as_version_info(tmp_path)
unstamped = tmp_path / "unstamped"
unstamped.mkdir()
assert read(unstamped)[0] == "0.0.0"
+
+
[email protected](not hasattr(os, "mkfifo"), reason="the blocking untracked scan uses a FIFO")
+def test_a_timed_out_status_probe_never_strands_index_lock(tmp_path):
+ """The update opens its receipt with this probe; killed by its 3 s timeout while refreshing
+ the index, it used to leave .git/index.lock for the same run's merge to die on (#132089)."""
+ def git(*args):
+ subprocess.run(["git", "-c", "[email protected]", "-c", "user.name=t", *args],
+ cwd=tmp_path, check=True, capture_output=True)
+
+ git("init", "-q")
+ (tmp_path / "a.txt").write_text("a", encoding="utf-8")
+ git("add", "a.txt")
+ git("commit", "-qm", "a")
+ (tmp_path / "junk").mkdir()
+ os.mkfifo(tmp_path / "junk" / ".gitignore") # the untracked scan blocks reading it
+ (tmp_path / "junk" / "x").touch()
+ (tmp_path / "a.txt").touch() # stat-dirty: a locking status would refresh the index
+
+ _git_version_info(tmp_path, include_untracked=True)
+
+ assert not (tmp_path / ".git" / "index.lock").exists()