fix: read locks with --no-lock
ci / docker (push) Successful in 17s

'restic cat' and 'restic snapshots' take a lock of their own, so an
exclusive lock left by a dead run blocked the very code trying to clear
it — and with --retry-lock it blocked for 30 minutes instead of failing.
'restic list locks' does not lock, which is why the listing worked and
the per-lock read hung.

Pass --no-lock for cat config, cat lock, list locks and snapshots.
Verified against a real orphan: restic check killed with SIGKILL leaves
an exclusive lock, ensure_repository no longer blocks on it, and the
cleanup removes it.
This commit is contained in:
trex Bot committed 2026-11-24 23:31:58 -06:00
1 parent 1d8f49980c
commit 58e81b633a
2 files changed
+30 -4

No files matched your search

+15 -4
View File
@@ -227,11 +227,17 @@ def restic(
log_output: bool = False,
check: bool = True,
step: str = "restic",
no_lock: bool = False,
) -> subprocess.CompletedProcess:
digest = hashlib.sha256(repository.encode()).hexdigest()[:12]
# --retry-lock makes a lock we could not clear wait instead of failing the
# run outright; restic drops it itself once it passes its stale window.
global_args = ["--retry-lock", cfg.retry_lock] if cfg.retry_lock else []
# Read-only introspection still takes a lock (``cat`` and ``snapshots``
# both do), so an exclusive lock left by a dead run would block the very
# code that is trying to clear it. --no-lock is what makes that possible.
if no_lock:
global_args.append("--no-lock")
return run(
["restic", *global_args, *args],
env_extra={
@@ -400,7 +406,9 @@ def pid_alive(pid: int) -> bool:
def list_locks(cfg: Config, name: str, repository: str, password: str) -> list[dict]:
"""Return every lock in the repository, with its id attached."""
result = restic(cfg, repository, password, ["list", "locks"], capture=True, check=False)
result = restic(
cfg, repository, password, ["list", "locks"], capture=True, check=False, no_lock=True
)
if result.returncode != 0:
detail = (result.stderr or "").strip().splitlines()
log(
@@ -411,7 +419,8 @@ def list_locks(cfg: Config, name: str, repository: str, password: str) -> list[d
locks: list[dict] = []
for lock_id in (result.stdout or "").split():
info = restic(
cfg, repository, password, ["cat", "lock", lock_id], capture=True, check=False
cfg, repository, password, ["cat", "lock", lock_id],
capture=True, check=False, no_lock=True,
)
if info.returncode != 0:
log(f"WARNING: cannot read lock {lock_id[:8]} in {name}")
@@ -585,7 +594,9 @@ def dump_databases(cfg: Config, stamp: str) -> dict:
def ensure_repository(cfg: Config, repository: str, password: str) -> None:
result = restic(cfg, repository, password, ["cat", "config"], capture=True, check=False)
result = restic(
cfg, repository, password, ["cat", "config"], capture=True, check=False, no_lock=True
)
if result.returncode == 0:
return
if result.returncode == 10:
@@ -603,7 +614,7 @@ def latest_snapshot(cfg: Config, repository: str, password: str) -> dict | None:
result = restic(
cfg, repository, password,
["snapshots", "--latest", "1", "--json"],
capture=True, check=False,
capture=True, check=False, no_lock=True,
)
if result.returncode != 0:
return None