From 58e81b633ac70254953d9912bf28e42a26dc6a28 Mon Sep 17 00:00:00 2001 From: trex Date: Tue, 24 Nov 2026 23:31:58 -0600 Subject: [PATCH] fix: read locks with --no-lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit '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. --- README.md | 15 +++++++++++++++ seafile-backup.py | 19 +++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 102afed..dfa47a3 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ record nothing, which is why the query string is the only form used here. | `FAIL_ON_PARTIAL` | `false` | treat restic exit 3 as a failure | | `CRON` | `0 3 * * *` | crond schedule (standard 5-field) | +`LOCK_STALE_AFTER`, `LOCK_GRACE` and `RETRY_LOCK` are read when the job starts, +so changing them needs a container recreate (a Komodo deploy), not just a +restart of the job. + `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` are read by restic itself for the `s3:` backend. @@ -96,6 +100,11 @@ ones past restic's own 30 minute window. Every lock is logged with its age, host, pid and verdict, and the ids that were removed are re-checked with a second `list locks` before being reported. +Reading the locks has to happen with `--no-lock`: `restic cat` and +`restic snapshots` take a lock of their own, so an exclusive lock left by a +dead run would block the very code that is trying to clear it. `restic list +locks` does not lock, which is why the listing works either way. + The `orphaned` check compares the lock's `hostname` with the container's own, so the compose file pins `hostname: seafile-backup` — without it the hostname is the container id and changes on every recreate, which is exactly the case this @@ -106,6 +115,12 @@ window rather than one that is removed while still in use. `RETRY_LOCK` is the backstop: every restic call carries `--retry-lock`, so a lock that survives the cleanup makes the run wait instead of failing. +Verified against a real orphan: `restic check` killed with `SIGKILL` mid-run +leaves an exclusive lock, and the next cleanup removes it (`orphaned`, age 13s, +`unlock --remove-all`, `successfully removed 1 locks`). With the default +`LOCK_GRACE` the same lock is reported `live` and left alone until it is ten +minutes old. + ## Restore ```sh diff --git a/seafile-backup.py b/seafile-backup.py index 54ecaba..100d789 100644 --- a/seafile-backup.py +++ b/seafile-backup.py @@ -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