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
View File
@@ -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 | | `FAIL_ON_PARTIAL` | `false` | treat restic exit 3 as a failure |
| `CRON` | `0 3 * * *` | crond schedule (standard 5-field) | | `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 `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` are read by restic itself for the
`s3:` backend. `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 host, pid and verdict, and the ids that were removed are re-checked with a
second `list locks` before being reported. 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 `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 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 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 `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. 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 ## Restore
```sh ```sh
+15 -4
View File
@@ -227,11 +227,17 @@ def restic(
log_output: bool = False, log_output: bool = False,
check: bool = True, check: bool = True,
step: str = "restic", step: str = "restic",
no_lock: bool = False,
) -> subprocess.CompletedProcess: ) -> subprocess.CompletedProcess:
digest = hashlib.sha256(repository.encode()).hexdigest()[:12] digest = hashlib.sha256(repository.encode()).hexdigest()[:12]
# --retry-lock makes a lock we could not clear wait instead of failing the # --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. # run outright; restic drops it itself once it passes its stale window.
global_args = ["--retry-lock", cfg.retry_lock] if cfg.retry_lock else [] 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( return run(
["restic", *global_args, *args], ["restic", *global_args, *args],
env_extra={ 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]: def list_locks(cfg: Config, name: str, repository: str, password: str) -> list[dict]:
"""Return every lock in the repository, with its id attached.""" """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: if result.returncode != 0:
detail = (result.stderr or "").strip().splitlines() detail = (result.stderr or "").strip().splitlines()
log( log(
@@ -411,7 +419,8 @@ def list_locks(cfg: Config, name: str, repository: str, password: str) -> list[d
locks: list[dict] = [] locks: list[dict] = []
for lock_id in (result.stdout or "").split(): for lock_id in (result.stdout or "").split():
info = restic( 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: if info.returncode != 0:
log(f"WARNING: cannot read lock {lock_id[:8]} in {name}") 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: 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: if result.returncode == 0:
return return
if result.returncode == 10: if result.returncode == 10:
@@ -603,7 +614,7 @@ def latest_snapshot(cfg: Config, repository: str, password: str) -> dict | None:
result = restic( result = restic(
cfg, repository, password, cfg, repository, password,
["snapshots", "--latest", "1", "--json"], ["snapshots", "--latest", "1", "--json"],
capture=True, check=False, capture=True, check=False, no_lock=True,
) )
if result.returncode != 0: if result.returncode != 0:
return None return None