#!/usr/bin/env bash # Machine-wide build slots — bounds heavy builds across EVERYTHING running # on this Mac: CI runner jobs, Claude sessions, and manual shells. # # Archives saturate a 16 GB Mac alone. Ordinary builds can share two slots # on a Mac with at least 24 GB; smaller machines keep one. A caller wrapping # xcodebuild archive, fastlane gym or release-app.sh declares # BUILD_LOCK_EXCLUSIVE=1 on its outermost lock. Releases are always exclusive. # The first slot keeps the shared Tinmen lock address; old holders that lack # an exclusivity marker are treated as exclusive while they finish. # # Usage: # scripts/with-build-lock.sh # BUILD_LOCK_LABEL=ci-user scripts/with-build-lock.sh bundle exec fastlane ios user_release # # Semantics: # - Atomic acquire via mkdir; the holder records its pid + label inside. # - Waiters poll every 30s. A lock whose holder pid is DEAD is reclaimed # immediately (crash without cleanup). A lock directory with no pid file # older than BUILD_LOCK_STALE_MIN is reclaimed (holder died mid-acquire). # A lock whose holder is ALIVE is honored indefinitely up to the timeout — # long legit builds are never stolen. # - Timeout exits 75 (EX_TEMPFAIL) so a CI step fails loudly instead of # silently piling on. # - RE-ENTRANT. A command run under the lock may wrap one of its own steps in # this script again — `scripts/ios-test.sh` and `scripts/build-xcframework.sh` # take the lock themselves, so any caller wrapping them nests by accident — # and the inner call runs straight through instead of waiting on a lock its # own ancestor holds. Wrapping is harmless; nothing has to remember not to. # # HOW RE-ENTRANCY IS DECIDED, AND WHY THE ENVIRONMENT VARIABLE IS NOT ENOUGH. # The holder exports TINMEN_BUILD_LOCK_HELD= (plus the lock path and # label) for its children. An inner call trusts that marker only after walking # its OWN parent chain (in-process parent reads) and finding that pid in it, and only when # the marker names the SAME lock path. A bare variable would be a skeleton key: # an export left in a long-lived shell (or inherited by a build kicked off after # the holder exited, with the pid recycled onto some unrelated process) would # wave every later build past a lock somebody else is really holding, which is # the swap-crash this whole file exists to prevent. An ancestor is different in # kind — the holder is still alive, and it is still running the thing that is # asking. Guard: `scripts/__tests__/build-lock-reentrant.test.ts`. # # WHY THE LOCK IS STILL CALLED tinmen-build.lock. It is machine-wide, and this # machine also runs the Tinmen monorepo's CI and dev sessions. The lock's whole # job is to make every heavy build on this Mac queue behind every other one — so # both repos MUST agree on the path. Renaming it to something CottonDB-flavoured # would give this repo its own private lock, which is the same as having no lock # at all: two archives would run concurrently and swap-crash the machine. The # name is a shared address, not a brand. # # Env knobs: # BUILD_LOCK_DIR lock path (default /tmp/tinmen-build.lock — /tmp is # machine-local and cleared on reboot, which is # exactly the lifetime a build lock should have) # BUILD_LOCK_LABEL who's asking (default user-pid; CI sets ci-) # BUILD_LOCK_TIMEOUT_MIN max minutes to wait for the lock (default 10 — the # build policy's admit timeout; see "NOTHING WAITS # WITHOUT A LINE") # BUILD_LOCK_STALE_MIN reclaim an owner-less lock after this (default 5) # BUILD_LOCK_LOG the wait log (default ~/.cache/cottondb/build-lock.log) # BUILD_LOCK_MAX_MIN per-job CEILING on the wrapped command (default 0 = # off). See below. # BUILD_LOCK_CEILING_SECONDS / _GRACE_SECONDS # the ceiling and the TERM→KILL grace in seconds — the # seam the guard drives; no lane sets either. # BUILD_LOCK_EXCLUSIVE 1 for archives (xcodebuild archive, fastlane gym, # release-app.sh); declare on the outermost caller # BUILD_LOCK_MEMORY_BYTES test seam; normally sysctl -n hw.memsize decides # BUILD_LOCK_CLASS release | ci | walk | local — overrides the runner's # CI variables (`scripts/lib/caller-class.sh`); a # release or install lane sets `release`, a walk's # simulator build `walk` (`rank_as_walk`) # BUILD_LOCK_YIELD `never` — a local run that must not be interrupted # (the Mac release lane). It still waits behind claims. # BUILD_LOCK_LOCAL_NICE the niceness a local run's command runs at (default 19, 0 = off) # BUILD_LOCK_POLL_SECONDS / _CI_POLL_SECONDS / _YIELD_POLL_SECONDS / # BUILD_LOCK_YIELD_GRACE_SECONDS # the intervals (30, 5, 5) and the TERM→KILL grace on a # yield (20) — seams the guard drives # BUILD_LOCK_TIMEOUT_SECONDS the lock wait in seconds, over the minutes — a seam # BUILD_LOCK_SAY_SECONDS / _TERM_GRACE_SECONDS # how often a wait is said again (60), and the # TERM→KILL grace when this wrapper is itself stopped # (20) — seams # # NOTHING WAITS WITHOUT A LINE (architect, 14 Oct 2026 23:45 IST). Cut #3 of # 0.29.57 sat 28 minutes held with no line printed, and cut #2's wrapper kept # the lock through SIGTERM until a KILL. So every wait here — on the lock, on a # claim that outranks this caller — says what it waits on and since when as it # starts and every minute after, and gives up with that line at # BUILD_LOCK_TIMEOUT_MIN, ten by default — the policy's RAM admission (inside # the lock, scripts/lib/mac_disk_policy.py) does the same with the same clock. # Every sleep is a background `sleep` this shell `wait`s on, so a TERM is taken # at once, never after the poll. A TERM, INT or HUP stops the command's whole # tree — TERM, a bounded grace, then KILL — and only then releases the lock and # the claim and drops the tree's reservations from the ledger: nothing it held # is left to a KILL from outside. Guard: `scripts/__tests__/build-lock-held.test.ts`. # # THE CEILING, AND WHY IT IS OFF BY DEFAULT. Waiting is correct; waiting on a # holder that has stopped making progress is not. Measured on one Mac on 23 Sep # (PR #915, run 100): sccache's server wedged, three `sccache … rustc` children # sat at 0% CPU for 77 minutes, and the `swift` job held this lock for the whole # household while doing nothing. Nothing timed out, because a job that never # fails never releases. # # So a CALLER may declare how long its lane can honestly take — # `BUILD_LOCK_MAX_MIN=50` on the swift and ios steps, which measure ~50 min at # their slowest — and a command that outlives that is TERMed, then KILLed, and # this script exits 124 (the exit `timeout(1)` uses, which macOS does not ship). # The lock is released by the same EXIT trap that releases it on success, so the # next job starts instead of queueing behind a corpse. # # It is OFF unless a caller sets it: a ceiling guessed for someone else's lane # is a build killed for being slow, which is worse than the wedge it was meant # to catch. A re-entrant call never applies one either — the outer call owns the # clock as it owns the lock. # # THE WAIT LOG. One TAB-separated line per ACQUIRE, appended: # # \t