Skip to content

feat(deploy): one deploy of a stack at a time, under a per-checkout lock - #1041

Merged
Gerrrt merged 3 commits into
mainfrom
gerrrt/deploy-lock
Oct 7, 2026
Merged

Gerrrt merged 3 commits into
mainfrom
gerrrt/deploy-lock

Conversation

@Gerrrt

@Gerrrt Gerrrt commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

Why

On 2026-10-07 two sessions deployed the observability stack from the deployment checkout on prometheus eighteen seconds apart: a make up at 15:41:49 UTC and a make converge at 15:42:07. Both recreated alertmanager and blackbox-exporter at once. Compose renames a container to <id>_<name> while it replaces it, so the second run's recreate hit the first's temporary name (/ee680d332de7_alertmanager is already in use). check_mounted_config.py failed, and that make up exited 2 before record-applied.sh ran. The first run happened to finish and record, so the host ended up fine, but nothing guaranteed it. The hourly converge timer and a hand make up can collide the same way.

What

  • scripts/deploy-lock.sh (new): a per-stack flock in the checkout's .git (git rev-parse --git-path homelab-deploy-<stack>.lock, beside record-applied.sh's record). Each checkout has its own, so a worktree never blocks the deployment checkout, and it never shows in git status.
    • Run as deploy-lock.sh [--wait S] STACK -- CMD…, or sourced for deploy_lock_acquire.
    • It writes the holder (pid, user, since, cmd) into the file, and --holder STACK prints it. The lock itself is the flock, not the text, so a holder that died leaves the lock free.
    • A deadline that passes exits 75 (EX_TEMPFAIL), and the command never runs.
    • The command runs with the lock's descriptor closed, so a daemon it leaves behind can't keep holding the lock. That makes the wrapper the lock's only owner, so it passes TERM, INT and HUP on to the command and waits until the command has really exited: killing the wrapper alone can't free the lock under a running deploy (from review). SIGKILL is the one gap.
    • Without flock it warns and runs unlocked, rather than turning a missing guard into an outage.
  • make up: the existing recipe moves unchanged to an internal _up, rendering included. up runs it under the lock and waits up to HOMELAB_DEPLOY_LOCK_WAIT (900s), naming the holder. make help still lists only up.
  • converge.sh: takes the lock right after its checkout preconditions and holds it from reading HEAD to recording. If another deploy still holds it after HOMELAB_CONVERGE_LOCK_WAIT (600s), it logs "still held the deploy lock", exits 0 and records nothing. HEAD may be mid-move at that point, so anything it recorded would describe a deploy in progress; the next hourly run asks again. Its own make up passes straight through via HOMELAB_DEPLOY_LOCK_HELD, since a second flock on the same file from the child would otherwise wait forever on its parent. The comment listing the guards that may exit before recording now names this one.
  • Docs: a "When another deploy is running" section in docs/runbooks/converge-the-host.md, a row in scripts/README.md, and a changelog entry.

Evidence

  • deploy-lock.sh --self-test passes 17/17. It covers:
    • the exit status coming back;
    • two concurrent holders running in order, never overlapping;
    • a deadline giving exit 75 without running the command;
    • another stack not being blocked;
    • the nested child passing through (under timeout, so a regression fails rather than hangs);
    • the sourced form blocking an unrelated deploy;
    • a dead holder's stale text neither blocking nor being reported;
    • a TERM to the wrapper with a TERM-ignoring child: the lock stays held (75), the child's status comes back, and the lock frees after. Reverting to the foreground run fails these;
    • a TERM reaching an ordinary child, and stdin still arriving.
  • converge.sh --self-test passes 95/95, after rebasing onto feat(converge): deploy the newest commit that passed CI, not only the tip (#1026) #1032, which changed converge. The new case, "leaves alone a stack another deploy holds the lock for", checks exit 0, the log line, and that make was never reached. Mutation: with deploy_lock_acquire replaced by true, that case fails (make reached).
  • self-tests.sh --require-all: 854 fixtures across 47 suites (46 before this, since discovery found the new suite).
  • make -n up reaches the real recipe through the lock. make help shows up and not _up.
  • validate.sh passes, with the 4 usual worktree-only skips. lint.sh (pinned ruff and ansible-lint via pipx) passes, as do check_docs.py and shellcheck -x.

Not exercised: a real concurrent make up against the live stack. Running one from a worktree would recreate production containers. After merge, the converge journal will show the lock is invisible on an uncontended run, and deploy-lock.sh --holder observability reads empty between deploys.

🤖 Generated with Claude Code

On 2026-10-07 a hand `make up` and a `make converge` ran eighteen seconds
apart from the deployment checkout. Both recreated alertmanager and
blackbox-exporter, and the second failed halfway on a container-name
conflict before recording what it applied.

scripts/deploy-lock.sh is a per-stack flock in the checkout's git dir.
`make up` (render included, via an internal `_up`) runs under it and
waits up to HOMELAB_DEPLOY_LOCK_WAIT (900s), naming the holder, then exits
75. converge.sh holds it from reading HEAD to recording, waits up to
HOMELAB_CONVERGE_LOCK_WAIT (600s), and otherwise leaves the stack for the
next run with exit 0. Its own `make up` passes through via
HOMELAB_DEPLOY_LOCK_HELD.

Fixtures: 12 in deploy-lock.sh --self-test (concurrent holders, deadline,
other stack, nesting, sourced form, stale holder), and a converge case
that fails when the lock is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 7, 2026 17:06
@Gerrrt
Gerrrt enabled auto-merge (squash) October 7, 2026 17:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Terminating the lock wrapper can release the lock while its deployment command remains running.

1 open finding
What changed in this PR

Adds per-stack deployment locking to prevent concurrent Compose deployments from the same checkout.

Changes:

  • Introduces a reusable flock-based deploy lock with self-tests.
  • Applies locking to make up and convergence.
  • Documents lock behavior and the triggering incident.
File Description
scripts/​deploy-lock.sh Implements lock acquisition, holder reporting, command wrapping, and tests.
scripts/​converge.sh Holds the deploy lock throughout convergence.
Makefile Runs the complete up workflow under the lock.
scripts/​README.md Catalogs the new utility.
docs/​runbooks/​converge-the-host.md Documents contention behavior and diagnostics.
docs/​changelog.md Records the deployment incident and remediation.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread scripts/deploy-lock.sh Resolved
Gerrrt and others added 2 commits October 7, 2026 17:17
The wrapper is the lock's only owner, because its command runs with the
descriptor closed. A SIGTERM to the wrapper alone freed the lock while
the deploy carried on, so another deploy could start on top of it.

The command now runs in the background with stdin passed explicitly.
TERM, INT and HUP are forwarded to it, and the wrapper waits until it has
really exited, returning its status. SIGKILL remains the one gap.

Five new self-test cases: a TERM-ignoring child keeps the lock held, the
wrapper returns the child's status, the lock frees after, TERM reaches an
ordinary child, and stdin still arrives. Reverting to the foreground run
fails the first two.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gerrrt
Gerrrt merged commit a223a7a into main Oct 7, 2026
8 checks passed
@Gerrrt
Gerrrt deleted the gerrrt/deploy-lock branch October 7, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants