Skip to content

fix(lock): queue and report on a contended flock instead of failing at 2m0s - #132

Merged
github-actions[bot] merged 1 commit into
mainfrom
feat/lock-queue-and-report
Sep 13, 2026
Merged

github-actions[bot] merged 1 commit into
mainfrom
feat/lock-queue-and-report

Conversation

@atrawog

@atrawog atrawog commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Summary

A blocking AcquireFileLock gave up after 2 minutes and named nobody. Measured in a real check bed (run 2026.255.2310, the check-githubrunner-pod image-build step):

charly: error: command "build": building githubrunner: acquiring build lock for githubrunner:
flock /home/atrawog/.cache/charly/locks/image-abNcNfNcNaeN.lock: lock held by another process for > 2m0s

The holder was a peer cold-building the SAME image — a legitimate hold (a cold image build measured up to ~26 min in the beds: pod-immich-ml/cachyos image-build 1529 s), not a stuck one. The correct behaviour is to QUEUE: the peer finishes, and the waiter then cache-hits. Any two concurrent check beds that share an intermediate therefore turned into a hard failure of one of them, at a bound shorter than one cold build.

What changed

  • lockTimeout 2m → 30m. The bound must exceed the longest LEGITIMATE hold or a healthy peer is reported as a failure. 30 m covers the longest measured cold build (~26 min) with headroom and stays bounded — the deploy-del guard (never an unbounded flock) is untouched.
  • flockBounded reports WHILE it waits. Every lockWaitReportInterval (30 s) a waiting acquire prints to stderr what it is waiting for, who holds it, how long it has waited, and when it will give up. Without this a legitimate minutes-long queue is indistinguishable from a hang — this is the difference between a reported wait and a silent stall.
  • The timeout error NAMES the holder and the next step, resolved from the KERNEL, not from the lock file:
    flock <path>: still held by pid 12345 (/usr/local/bin/charly box build cachyos) after 30m0s —
    that process is still running; wait for it to finish and retry (the kernel releases the lock when it exits, so there is nothing to clean up)
    
    /proc/<pid>/fdinfo/<fd> carries the locked file's inode AND the lock (lock:\t1: FLOCK ADVISORY WRITE <pid> <maj:min:ino> 0 EOF), so a waiter matches the inode, resolves the pid, and reads its command line. This is deliberately not a pid line written into the lock file: the file stays content-free (the removal those WritesNothing/TruncatesStaleContent tests pin shut), a dead holder can never be named (the kernel drops its flock on exit), and a waiter that has merely OPENED the file is never mistaken for the holder (it has no lock: line). Where /proc cannot answer (non-Linux, kernels without fdinfo locks) the description degrades to another process (holder not identifiable on this platform) — the WAIT is unaffected.
  • Regression-guard rename: TestAcquireFileLock_FailsFastOnContendedLock → TestAcquireFileLock_BoundedOnContendedLock (the bound still terminates, but "fast" stopped being true once the normal hold became minutes) with the doc comment updated.
  • New tests: the timeout names the holder + the bound + the next step; the waiter reports while waiting; the kernel-side lookup positively resolves the holding pid; the description falls back to text for an unheld lock.

Follow-on (NOT in this PR)

The consumer is opencharly/charly (charly/go.mod requires github.com/opencharly/spec) — the new semantics reach charly's own sibling build lock (charly/plugin_loader.go's plugin-binary AcquireFileLock) only through a spec pin bump, which lands as its own charly PR (together with the call-site message hardening). Likewise candy/plugin-build's acquiring build lock for %s wrap (podman.go:37, wrapped by drive.go:158) now carries the holder identity through %w; its call-site message is a separate plugin-build PR. This PR deliberately contains neither (pin discipline / one repo per PR).

How tested

1. Full gate — the repo's own CI (ci.yml: Build / Test / gofmt / golangci-lint v2.13.2)

$ cd /tmp/ws-spec && gofmt -l .
(no output)
$ go build ./...
build-exit=0
$ go test -count=1 ./...
ok  github.com/opencharly/spec/lock  4.213s
ok  github.com/opencharly/spec/exec  11.589s
ok  github.com/opencharly/spec/refs  16.056s
... (every package ok)
$ go vet ./lock/
vet-exit=0
$ golangci-lint run ./...
0 issues.

2. The package's own tests, including the PRE-EXISTING contract this change must not break

$ go test -count=1 -v -run 'TimeoutNamesHolder|ReportsWhileWaiting' ./lock/
=== RUN   TestAcquireFileLock_TimeoutNamesHolder
    filelock_wait_test.go:41: timeout error: flock /tmp/.../held.lock: still held by pid 3575926 (/tmp/go-build.../lock.test ...) after 200ms — that process is still running; wait for it to finish and retry (the kernel releases the lock when it exits, so there is nothing to clean up)
--- PASS: TestAcquireFileLock_TimeoutNamesHolder (0.80s)
=== RUN   TestAcquireFileLock_ReportsWhileWaiting
--- PASS: TestAcquireFileLock_ReportsWhileWaiting (1.28s)

$ go test -count=1 -v ./lock/
--- PASS: TestAcquireFileLock_WritesNothing
--- PASS: TestAcquireFileLock_TruncatesStaleContent
--- PASS: TestAcquireFileLock_WriteAfterAcquireSurvives
--- PASS: TestAcquireFileLock_NonBlockingBusy
--- PASS: TestAcquireFileLock_BoundedOnContendedLock
--- PASS: TestAcquireFileLock_TimeoutNamesHolder
--- PASS: TestAcquireFileLock_ReportsWhileWaiting
--- PASS: TestFlockHolderPIDs_NamesThisProcess
--- PASS: TestHolderDescription_FallsBackWhenUnheld
ok  github.com/opencharly/spec/lock  3.996s

The two content tests (WritesNothing, TruncatesStaleContent) still pass unchanged — this change adds no bytes to a lock file.

3. Live end-to-end queue: a REAL external holder, a real waiter, the real exported API

$ flock /tmp/specprobe/demo.lock sleep 40 &   # the peer "cold-building the image"
HOLDER: pid=3574639 cmd=flock /tmp/specprobe/demo.lock sleep 40
$ /tmp/specprobe/probe /tmp/specprobe/demo.lock
waiter: AcquireFileLock(/tmp/specprobe/demo.lock, blocking=true) -- peer is holding it
charly: waiting 30s for file lock /tmp/specprobe/demo.lock — held by pid 3574639 (flock /tmp/specprobe/demo.lock sleep 40), pid 3574641 (sleep 40); giving up after 30m0s
waiter: ACQUIRED (queued) after 40s -- the peer finished, the waiter proceeds (in a real build it now cache-hits)
waiter wall: 39.60s
waiter-exit=0

(pid 3574641 is the sleep child, which inherits the file description the flock lives on — both processes genuinely hold it; the holder list reports what the kernel reports.)

4. What was NOT run

No charly build-contention bed run was staged here. The pre-fix failure it would reproduce is already an artifact from the real run (2026.255.2310, pasted in Summary), and the changed path ran live above (section 3) with the full gate green on the final tree (sections 1–2). A bed-level re-run belongs to the consumer once the pin bump lands.

Rulebook compliance

(opencharly/spec ships no AGENTS.md/CLAUDE.md — verified at the repo root — so the umbrella rulebook governs.)

  • Rule 1 (never edit inside a submodule) — HOW: authored in a scratch clone (/tmp/ws-spec = github.com/opencharly/spec) and landed by PR; the umbrella gitlink is untouched.
  • Rule 2 (git through git -C <absolute path>) — HOW: every git command used git -C /tmp/ws-spec.
  • Rule 3 (no nested go.work) — HOW: none added; the module builds standalone (go build ./...).
  • Rule 4 (no worktrees inside submodules) — HOW: a standalone clone in /tmp, not a worktree.
  • Rule 5 (pin discipline) — HOW: no pin in this PR; the consumer (charly go.mod) bumps to this PR's merge tag in its own PR.
  • Rule 6 (policy B) — N/A: this repo records no charly gitlinks.
  • Rule 7 (read the subrepo's own rulebook first) — HOW: checked — no AGENTS.md/CLAUDE.md exists in this repo, so the umbrella rulebook applies.
  • Rule 8 (harness config parity) — N/A: no harness config in this repo.
  • R0 (skills first) — HOW: /charly-internals:git-workflow loaded before the first tool call.
  • R1 (RCA before remediation) — HOW: the 2-minute bound was introduced deliberately (PR fix(lock): bound the blocking acquire — fail fast on a contended flock #73, the deploy-del stall) and its premise was re-examined here: it bounds STUCK holders, but the measured holder was SLOW (a cold build), so the bound was raised rather than the failure tolerated; the now-inaccurate fails fast docs/name are corrected in-PR.
  • R3 (no duplication) — HOW: one holder-resolution helper used by both the progress report and the timeout error; no second lock implementation, no parallel wait.
  • R4 (no workarounds) — HOW: the hold failure is not suppressed, retried blindly, or hidden — the message is made actionable and the wait is made long enough for the legitimate case; no sleep-at-caller, no env-var escape hatch.
  • R5 (delete legacy completely) — HOW: the old 2m bound and old error text are REPLACED (no alternative code path); the only reversal — writing lock-file content — was deliberately NOT taken, so the earlier cutover's contract stays intact.
  • R6 (git safety) — HOW: git status clean before commit; branch feat/lock-queue-and-report; no force-push, no hook bypass, no direct push to main.
  • R7 (prove the gate) — HOW: the repo's CI gate (build / test / gofmt / golangci-lint v2.13.2 — exactly ci.yml) ran on the final tree; output pasted.
  • R10 (fresh disposable proof) — HOW: the live queue demo uses the built package against an external holder, and the gate ran on the committed tree.

Change classification

  • Change class: behavioural fix in a shared fabric primitive (lock) — no schema/CUE change, no wire change, no API change (all exported signatures unchanged), no pin.
  • Verification gate: the repo's CI gate (ci.yml: go build ./..., go test ./..., gofmt -l ., golangci-lint run ./...) — PASSED on the final tree; plus the live external-holder queue demo and the package's own pre-existing content tests still green.
  • Attribution tier: fully tested and validated — the repo's full gate passed on the final tree and the changed path (blocking acquire → bounded queue with live holder report → acquire on release) executed live; the one thing a stronger claim could add is a consumer-side charly build-contention run, which lands with the charly pin bump (section "Follow-on").

Assisted-by: pi ollama-cloud/deepseek-v4.1-flash (fully tested and validated)

…t 2m0s

A blocking acquire gave up after 2 minutes and named nobody. Measured in a check
bed as

  acquiring build lock for githubrunner: flock ~/.cache/charly/locks/image-<sha8>.lock:
  lock held by another process for > 2m0s

(check run 2026.255.2310) while the holder was a PEER cold-building the SAME
image — a legitimate hold (a cold image build measured up to ~26 min in the
beds), not a stuck one. The correct behaviour is to queue: the peer finishes and
the waiter then cache-hits.

  - lockTimeout 2m -> 30m: a bounded wait that covers a cold image build with
    headroom (still bounded — the deploy-del stall guard is unchanged).
  - flockBounded reports WHILE it waits (holder, elapsed, giving-up bound), so a
    legitimate minutes-long queue is distinguishable from a hang.
  - the timeout error NAMES the holder (pid + command line) and says what to do,
    resolved from the KERNEL: /proc/<pid>/fdinfo carries the lock file's inode
    and the flock. The lock file itself stays content-free (the deletion the
    package's tests pin shut) and a DEAD holder can never be reported, because
    the kernel drops its flock on exit; the lookup degrades to plain text where
    /proc cannot answer.
  - the deploy-del regression guard is renamed ...FailsFast... ->
    ...BoundedOnContendedLock: the bound still terminates, but "fast" stopped
    being true once the per-image lock's normal hold became minutes.
@github-actions

Copy link
Copy Markdown

Review — PASS

Head SHA: 76892ad253fe0e424f20c46660d2696ec2d708c2 (branch: feat/lock-queue-and-report, per body)

Security screen: PASS — no scope mismatch (new lock/holder.go + tests are described in the summary), no secrets/egress, no guardrail/branch-protection/rulebook edits, no dependency or CI-script change; holding-pid lookup reads local /proc for a stderr diagnostic only.

Checklist: All applicable rules PASS or NA — one atomic commit; RCA replaces the 2 m bound (premise re-examined: slow vs stuck holder) rather than tolerating the failure; hard replacement, no dual-mode/legacy path; new tests fail without the change (timeout message, wait report, holder scan, fallback); no candy/schema/wire change → ADE/SDD/CUE NA; placement CORRECT (kind-blind diagnostic helper co-located with the lock primitive, no capability pulled into core); the contended-load failure is answered with a real external-holder queue demo, not an idle re-run; cross-repo consumer/pin work is structurally forced by pin discipline, not a vague deferral.

Tool-limited disposition: pasted test/probe output and the repo-root rulebook check cannot be re-run read-only — cross-checked as internally consistent with the diff (pre-change message format matches the pasted failure, 30 m bound/30 s cadence/holder pid resolution all match the code). No comments on the thread to disposition.

Verdict: PASS


opencode/deepseek-v4.1-flash — action-review.

View action run

@github-actions

Copy link
Copy Markdown

[step: review]

Review — PASS

Head SHA: 76892ad253fe0e424f20c46660d2696ec2d708c2 (branch: feat/lock-queue-and-report, per body)

Security screen: PASS — no scope mismatch (new lock/holder.go + tests are described in the summary), no secrets/egress, no guardrail/branch-protection/rulebook edits, no dependency or CI-script change; holding-pid lookup reads local /proc for a stderr diagnostic only.

Checklist: All applicable rules PASS or NA — one atomic commit; RCA replaces the 2 m bound (premise re-examined: slow vs stuck holder) rather than tolerating the failure; hard replacement, no dual-mode/legacy path; new tests fail without the change (timeout message, wait report, holder scan, fallback); no candy/schema/wire change → ADE/SDD/CUE NA; placement CORRECT (kind-blind diagnostic helper co-located with the lock primitive, no capability pulled into core); the contended-load failure is answered with a real external-holder queue demo, not an idle re-run; cross-repo consumer/pin work is structurally forced by pin discipline, not a vague deferral.

Tool-limited disposition: pasted test/probe output and the repo-root rulebook check cannot be re-run read-only — cross-checked as internally consistent with the diff (pre-change message format matches the pasted failure, 30 m bound/30 s cadence/holder pid resolution all match the code). No comments on the thread to disposition.

Verdict: PASS


opencode/deepseek-v4.1-flash — action-review.

View action run

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.

1 participant