Skip to content

Latest commit

 

History

History
85 lines (63 loc) · 3.94 KB

lock.md

File metadata and controls

85 lines (63 loc) · 3.94 KB

lock.sh — guide

Revision: 1 Last modified: 2026-12-04T00:00:00Z

Library: scripts/pr/lib/lock.sh (sourced by scripts/pr/lib/steps.sh; never executed on its own). Engine guide: docs/scripts/pr_deliver.md. Manual: docs/manuals/pr_delivery_engine_manual.md.

Overview

One exclusive lock per (repository slug, task id), taken BEFORE the engine mutates anything. Two agents delivering the same task at the same time would otherwise create two branches or two pull requests for one task id — the duplicate-primitive mess the engine exists to prevent. The second run exits 7 LOCKED instead of interleaving.

The lock is a DIRECTORY, <journal dir>/.locks/<repo>:<task>.lock/, holding two files: pid (the holder) and key (the locked key). mkdir is the atomic acquisition primitive — it either wins or it does not, with no read-then-write window.

Prerequisites

  • bash 4+, mkdir, kill, cat.
  • The journal directory must already resolve (see journal.md); the lock lives under it.
  • Nothing else: no provider, no credential, no network.

Usage examples

. "$PR_SELF_DIR/lib/lock.sh"

pr_lock_acquire "$(pr_cfg_get pr_repository):$PR_TASK" || exit 7
trap 'pr_lock_release' EXIT          # what the engine does: release on every exit path

Waiting instead of failing fast:

PR_LOCK_WAIT_S=30 bash scripts/pr/pr_deliver.sh create --task CON-641 --roadmap-item CON-641

Inspecting who holds a task (the same path status implies):

$ cat "$HOME/.local/state/helix-pr-delivery/owner__repo/.locks/owner/repo:CON-641.lock/pid"
4711

Edge cases

  • A held lock with a live holder is never stolen. pr_lock_holder reads pid; kill -0 proves the process is alive; the caller then waits up to PR_LOCK_WAIT_S (default 0, fail fast) and exits 7.
  • A provably dead holder is reaped and the acquisition retried once. Liveness is proven with kill -0, never inferred from the lock's age or mtime — the same discipline as §11.4.180.
  • A lock directory with no readable pid is treated as HELD (conservative default). An unreadable lock is not evidence that nobody holds it.
  • pr_lock_release only removes a lock this process owns: it compares the recorded pid with $$, so a reaped-and-retaken lock cannot be released by the process that lost it. Releasing a lock you do not hold is a no-op, which makes the EXIT trap safe on every path.
  • The key contains a slash (owner/repo:TASK), so the lock path nests one directory deeper. mkdir -p on the parent makes that harmless.
  • Two different tasks never block each other — the key includes the task id.

Internal behaviour

  • pr_lock_path KEY = $(pr_journal_dir)/.locks/<key>.lock.
  • pr_lock_acquire KEY loops: mkdir → on success write pid and key, set PR_LOCK_HELD, return 0; on failure read the holder → alive and budget exhausted ⇒ return 7; alive and budget left ⇒ sleep 1 and retry; dead ⇒ rm -rf the lock directory, print a reaping note to stderr, and retry; no readable pid ⇒ return 7.
  • pr_lock_release [KEY] defaults to PR_LOCK_HELD, removes the directory only when pid equals $$, and clears PR_LOCK_HELD.
  • The engine calls pr_lock_acquire in create, review and merge, and installs trap … EXIT in the same block — an interrupted run therefore does not leave the task permanently locked unless its process was killed without the trap firing, in which case the dead-holder reap path recovers it.

Related scripts

  • journal.md — supplies the directory the lock lives in.
  • steps.md — the three subcommands that take the lock.
  • pr_deliver.md — exit code 7 and the lock_held:<repo>:<task> reason.

Last verified

2026-12-04 — read against scripts/pr/lib/lock.sh (78 lines). The PR_LOCK_WAIT_S default (0) and the reap-then-retry-once behaviour are the code's, not an assumption.