Skip to content

The agent that grows with you

fix(update): only a killed update's index.lock is reclaimed at once

Tekniumf5f912efba4d
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index 00b4265bcf5..94d615eb9c9 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -124,13 +124,35 @@ def release_dead_index_lock(repo_root: Path) -> bool:
root = Path(repo_root)
git_dir = _git_dir(root)
- if not (git_dir / "index.lock").exists() or interrupted_pull_marker(root).exists():
+ lock = git_dir / "index.lock"
+ if not lock.exists() or interrupted_pull_marker(root).exists():
+ return False
+ # No holder is not proof of a dead one: a git that just created the lock may not have it
+ # open yet. Only a lock a killed update left is ours to take; anything else waits for the
+ # age-floor sweep and the update refuses on it truthfully.
+ if not _killed_update_owns(lock):
return False
if os.name == "nt" and _windows_git_in_checkout(root) is not False:
return False
return _release_dead_index_lock(git_dir, root)
+def _killed_update_owns(lock: Path) -> bool:
+ """The latest update run died without finishing and ``lock`` was created after it started."""
+ from datetime import datetime
+
+ from hermes_cli.update_receipt import _owner_alive, read_latest_receipt
+
+ latest = read_latest_receipt() or {}
+ if latest.get("outcome") not in ("running", "interrupted") or _owner_alive(latest):
+ return False
+ try:
+ started = datetime.fromisoformat(str(latest.get("started_at"))).timestamp()
+ return lock.stat().st_mtime >= started
+ except (OSError, ValueError):
+ return False
+
+
def _windows_git_in_checkout(root: Path) -> "bool | None":
"""Whether a ``git.exe`` works in ``root`` (its cwd or argv names it); None when unknowable.
diff --git a/tests/hermes_cli/test_gitlock.py b/tests/hermes_cli/test_gitlock.py
index c14d6154ead..bbd6d3ae9d3 100644
--- a/tests/hermes_cli/test_gitlock.py
+++ b/tests/hermes_cli/test_gitlock.py
@@ -349,9 +349,11 @@ def _status_blocked_on_a_fifo(repo: Path, *argv: str) -> subprocess.Popen:
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 import update_receipt
from hermes_cli.gitlock import release_dead_index_lock
lock = repo / ".git" / "index.lock"
+ _killed_update_receipt()
proc = _status_blocked_on_a_fifo(repo)
try:
assert release_dead_index_lock(repo) is False
@@ -361,5 +363,32 @@ def test_a_killed_gits_index_lock_goes_at_once_and_a_live_ones_stays(repo: Path)
proc.wait()
assert lock.exists(), "premise: a SIGKILLed status strands index.lock"
+ assert update_receipt.read_latest_receipt()["outcome"] == "running"
assert release_dead_index_lock(repo) is True
assert not lock.exists()
+
+
+def test_a_fresh_lock_with_no_killed_update_behind_it_is_kept(repo: Path) -> None:
+ """No process holding a lock it just created proves nothing (the git may not have opened it
+ yet): without a killed update run behind it, the update refuses on it and the lock stays."""
+ from hermes_cli.gitlock import release_dead_index_lock
+
+ lock = repo / ".git" / "index.lock"
+ lock.write_text("", encoding="utf-8")
+ assert release_dead_index_lock(repo) is False
+ assert lock.exists()
+
+
+def _killed_update_receipt() -> None:
+ """A real running receipt whose owner is a pid that already exited (the killed update)."""
+ import json
+
+ from hermes_cli import update_receipt
+
+ dead = subprocess.Popen(["true"])
+ dead.wait()
+ update_receipt.begin_update_receipt()
+ current = update_receipt._current.get()
+ current.data.update(pid=dead.pid, pid_create_time=None, writer_pid=dead.pid, writer_create_time=None)
+ update_receipt._write_latest((json.dumps(current.data) + "\n").encode("utf-8"))
+ update_receipt._current.set(None)