Skip to content

The agent that grows with you

fix(git): lock sweep keeps every lock unless it can see no git of any form

Teknium3f29cc2c2b06
diff --git a/hermes_cli/git_commit_graph.py b/hermes_cli/git_commit_graph.py
index c8dd4f7f83a..af090ed4a6f 100644
--- a/hermes_cli/git_commit_graph.py
+++ b/hermes_cli/git_commit_graph.py
@@ -196,12 +196,28 @@ def refresh_commit_graph(repo_root: str, git_dir: Path) -> None:
logger.info("core.commitGraph=false in %s; not writing a commit-graph", repo_root)
return
# Object-store debris only: ``hermes -w`` runs this in whatever repo it was launched from, where
- # index.lock / HEAD.lock belong to the user's own git (and pgrep can be blind to it).
+ # index.lock / HEAD.lock belong to the user's own git.
clear_stale_object_store_locks(git_dir)
clear_stale_tmp_packs(git_dir)
logger.info("git commit-graph missing or stale — writing it in background")
- if run_bounded_git(repo_root, _WRITE, _TIMEOUT, env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV}) == 0:
+ if run_bounded_git(repo_root, _WRITE, _TIMEOUT, env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV}) != 0:
+ return
+ # The stamp vouches for a graph on disk: a write another git undid (or whose files vanished)
+ # exits 0 too, and a stamp then would hold the retry off for a day with no graph at all.
+ if _graph_on_disk(git_dir):
(git_dir / _FRESH_STAMP).touch()
+ else:
+ logger.info("git commit-graph write in %s exited 0 but left no graph (no commits yet?)", repo_root)
+
+
+def _graph_on_disk(git_dir: Path) -> bool:
+ """A split chain whose every layer file exists, or a monolithic graph."""
+ info = git_dir / "objects" / "info"
+ try:
+ layers = (info / "commit-graphs" / "commit-graph-chain").read_text(encoding="ascii").split()
+ except (OSError, UnicodeDecodeError):
+ return (info / "commit-graph").is_file()
+ return bool(layers) and all((info / "commit-graphs" / f"graph-{layer}.graph").is_file() for layer in layers)
class _LogDirOnFailureHandler(logging.FileHandler):
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index a6fc500bb35..39ce0d592f7 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -51,25 +51,57 @@ _TMP_PACK_PREFIXES = ("tmp_pack_", "tmp_idx_", "tmp_rev_", "tmp_mtimes_", ".tmp-
_TMP_GRAPH_PREFIX = "tmp_graph_"
+# pgrep's name pattern for every form git runs as: ``git``, the dashed executables it ships and
+# launches (git-core's ``git-commit-graph``, ``git-repack``; comm is cut to 15 chars), ``git.exe``.
+_PGREP_GIT = r"^git(-.*|\.exe)?$"
+
+
def _git_proc_running() -> bool:
- """True when a ``git`` process is running — the check that stops us yanking a lock a live fetch holds.
+ """Whether a git process may be running: the check that stops us yanking a lock a live git holds.
- A failed probe logs and returns False; the age floor in the sweep still applies.
+ Fails closed: a probe that cannot tell (pgrep missing or failing, tasklist failing, a process
+ whose name psutil may not read) answers True and the sweep removes nothing. The age floor is
+ no proof on its own: a stopped or slow writer's lock ages like an abandoned one.
"""
+ if os.name != "nt":
+ try:
+ import psutil
+ except ImportError: # the dependency-free bootstrap interpreter
+ pass
+ else:
+ return _psutil_git_running(psutil)
+ from hermes_cli._early_recovery import _git_program
+
try:
# One shared spawn for both probes: the argv is the platform fork, the
# hide-flags wiring is common, so a console-less parent (pythonw backend)
# never flashes a console for either (#117781).
- argv = (["tasklist", "/FI", "IMAGENAME eq git.exe", "/FO", "CSV"]
- if os.name == "nt" else ["pgrep", "-x", "git"])
+ argv = ["tasklist", "/FO", "CSV", "/NH"] if os.name == "nt" else ["pgrep", _PGREP_GIT]
proc = subprocess.run(argv, capture_output=True, text=True, encoding="utf-8", errors="replace",
timeout=10, creationflags=windows_hide_flags(), check=False)
- if os.name == "nt":
- return "git.exe" in proc.stdout.lower()
- return proc.returncode == 0
- except Exception:
- logger.debug("git process probe failed; assuming no git running", exc_info=True)
- return False
+ except (OSError, subprocess.SubprocessError):
+ logger.debug("git process probe failed; assuming git may be running", exc_info=True)
+ return True
+ if os.name == "nt":
+ return proc.returncode != 0 or any(
+ _git_program(line.split(",", 1)[0].strip('"')) for line in proc.stdout.splitlines())
+ return proc.returncode != 1 # 1 is pgrep's "no match"; 2 and 3 mean it could not tell
+
+
+def _psutil_git_running(psutil) -> bool:
+ from hermes_cli._early_recovery import _git_program
+
+ try:
+ for proc in psutil.process_iter():
+ try:
+ if _git_program(proc.name()) is not None:
+ return True
+ except psutil.NoSuchProcess: # exited between the listing and the read
+ continue
+ except Exception: # health: allow BLE001 -- AccessDenied or a failed scan keeps the locks
+ logger.debug("git process scan failed; assuming git may be running", exc_info=True)
+ return True
+ return False
def _sweep_stale(directory: Path, candidates: Callable[[], Iterable[Path]], *, min_age_seconds: Optional[int],
diff --git a/tests/hermes_cli/test_gitlock.py b/tests/hermes_cli/test_gitlock.py
index 60940b3faa9..b27fdd6f3a5 100644
--- a/tests/hermes_cli/test_gitlock.py
+++ b/tests/hermes_cli/test_gitlock.py
@@ -27,6 +27,7 @@ import pytest
from hermes_cli.gitlock import (
LOCK_NAMES,
STALE_LOCK_MIN_AGE_SECONDS,
+ _git_proc_running as _REAL_GIT_GUARD,
clear_stale_git_locks,
)
@@ -91,10 +92,18 @@ def test_clear_removes_all_stale_lock_kinds(repo: Path, no_git_running: None) ->
assert not (repo / ".git" / name).exists()
-def test_graph_refresh_clears_object_store_debris_and_leaves_the_index_lock(repo: Path, no_git_running: None) -> None:
+def test_graph_refresh_clears_object_store_debris_and_leaves_the_index_lock(
+ repo: Path, no_git_running: None, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
"""A killed graph/midx writer's lock fails every later write of that file (git: "File exists"). The
- refresh also runs in whatever repo ``hermes -w`` started in, where index.lock is the user's git's."""
- from hermes_cli.git_commit_graph import refresh_commit_graph
+ refresh also runs in whatever repo ``hermes -w`` started in, where index.lock is the user's git's.
+
+ An old lock is no proof its writer is dead (a stopped writer's lock ages too): while a git of any
+ form runs (git-core's dashed executables included), or when no process list can be read, every
+ lock stays, and a write that could not run leaves no freshness stamp."""
+ import sys
+
+ from hermes_cli import gitlock
+ from hermes_cli.git_commit_graph import _FRESH_STAMP, refresh_commit_graph
git_dir = repo / ".git"
debris = [git_dir / "objects" / rel for rel in (
@@ -110,6 +119,33 @@ def test_graph_refresh_clears_object_store_debris_and_leaves_the_index_lock(repo
# The install checkout's own sweep (every update) still takes it.
assert clear_stale_git_locks(repo) == [str(git_dir / "index.lock")]
+ monkeypatch.setattr(gitlock, "_git_proc_running", _REAL_GIT_GUARD)
+ lock = git_dir / "objects" / "info" / "commit-graphs" / "commit-graph-chain.lock"
+ exec_path = Path(subprocess.run(["git", "--exec-path"], capture_output=True, text=True, check=True).stdout.strip())
+ dashed = next(exec_path.glob("git-cat-file*"), None)
+ if dashed is None:
+ pytest.skip("this git ships no dashed executables")
+ blind = tmp_path / "bin" # git and nice only: no pgrep
+ blind.mkdir()
+ for tool in ("git", "nice"):
+ (blind / tool).symlink_to(shutil.which(tool))
+ for case in ("dashed git running", "no process list"):
+ # A commit the graph lacks, so the write needs its lock (with nothing new it touches nothing).
+ subprocess.run(["git", "-c", "maintenance.auto=false", "-c", "gc.auto=0", "commit", "-q", "--allow-empty",
+ "-m", case], cwd=repo, check=True)
+ _touch(lock, STALE_LOCK_MIN_AGE_SECONDS + 60)
+ (git_dir / _FRESH_STAMP).unlink(missing_ok=True)
+ if case == "dashed git running":
+ with subprocess.Popen([str(dashed), "--batch"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL) as live:
+ refresh_commit_graph(str(repo), git_dir)
+ live.communicate()
+ else:
+ monkeypatch.setitem(sys.modules, "psutil", None)
+ monkeypatch.setenv("PATH", str(blind))
+ refresh_commit_graph(str(repo), git_dir)
+ assert lock.exists(), case
+ assert not (git_dir / _FRESH_STAMP).exists(), case
+
def test_clear_skips_sweep_while_git_running(repo: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A running git process must block the sweep even for stale locks."""
diff --git a/website/docs/getting-started/updating.md b/website/docs/getting-started/updating.md
index 30a65740b49..52694bf6a9d 100644
--- a/website/docs/getting-started/updating.md
+++ b/website/docs/getting-started/updating.md
@@ -323,7 +323,9 @@ stopped (asked to exit first, so git removes its own lock). A failed or stopped
repository it shares objects with. A repo with `core.commitGraph=false` in its own, your global or
the system git config is left without a graph. A
`commit-graph.lock` or `multi-pack-index.lock` that a killed git left behind is removed by the same
-stale-lock sweep as `index.lock` once it is 10 minutes old and no git is running; in a repo you
+stale-lock sweep as `index.lock` once it is 10 minutes old and no git process of any form (`git`,
+`git-commit-graph`, `git.exe`, ...) is running; when Hermes cannot list running processes, no lock is
+removed. In a repo you
launch `hermes -w` from, only those object-store locks are swept, never `index.lock` or `HEAD.lock`.
Each `hermes update` also spends at most 60 seconds cleaning those packs up, picking up where the