#!/usr/bin/env bash# `with_rustup_lock <command…>` — ONE rustup at a time against a shared# `~/.rustup`.## THIS IS A SOURCED SNIPPET, NOT A COMMAND, so the guards beside it can run the# lock with a stub `rustup` on PATH and no real toolchain.## THE BUG IT EXISTS FOR, measured on run #158. A Mac runs several of this# workflow's jobs at once — `swift` at 19:45:46 and `ios` a minute before it, on# one Mac — and they share one `~/.rustup`. The `ios` job runs# `rustup target add aarch64-apple-ios aarch64-apple-ios-sim`; the `swift` job's# `build-xcframework.sh` runs `rustup target list --installed`. rustup builds# that list by reading the toolchain's `lib/rustlib` directory, so a listing# taken mid-write comes back **exit 0 with a PARTIAL list** — and the reader# concluded "the active toolchain has no aarch64-apple-darwin target" about a# target that has been installed for months. A short answer is not a small# answer; it is a different question.## So every rustup call this repository makes — the reads AND the writes — takes# this lock. It only orders OUR callers against each other, which is the whole# of the observed race; `require_rust_targets` re-reads on a miss for the rest.## WHERE IT LIVES: beside the directory it protects (`$RUSTUP_HOME`, else# `~/.rustup`), so it is per-MACHINE-and-per-toolchain-home by construction —# a lane with its own RUSTUP_HOME takes its own file and waits on nobody.## HOW IT IS HELD: `mkdir`, not `flock`. macOS ships no `flock(1)` and the Macs# are exactly the machines with the race, so the portable form is the only one# that helps. A holder whose pid is dead is reclaimed on the next poll; an# ownerless directory ages out. Same shape as `scripts/ci/with-cargo-lock.sh`'s# macOS branch, for the same reason.## Env knobs, for the guards and for a hand run:# COTTON_RUSTUP_LOCK lock path (default "$RUSTUP_HOME/../.rustup.lock")# COTTON_RUSTUP_LOCK_WAIT_S max seconds to wait (default 300; then runs anyway)# COTTON_RUSTUP_LOCK_POLL_S poll interval (default 1)# COTTON_RUSTUP_LOCK_OFF set to 1 to skip the lock entirelyRUSTUP_LOCK_LOG_PREFIX="${RUSTUP_LOCK_LOG_PREFIX:-[rustup-lock]}"with_rustup_lock() {if [ -n "${COTTON_RUSTUP_LOCK_OFF:-}" ]; then"$@"return $?filocal home lock wait_s poll_s deadline holder status home="${RUSTUP_HOME:-$HOME/.rustup}" lock="${COTTON_RUSTUP_LOCK:-$home.lock}" wait_s="${COTTON_RUSTUP_LOCK_WAIT_S:-300}" poll_s="${COTTON_RUSTUP_LOCK_POLL_S:-1}" mkdir -p "$(dirname "$lock")"2>/dev/null || true deadline=$(( $(date +%s) + wait_s ))until mkdir "$lock"2>/dev/null; do holder="$(cat "$lock/pid" 2>/dev/null || true)"if [ -n "$holder" ] && ! kill -0"$holder"2>/dev/null; then rm -rf "$lock" continuefiif [ "$(date +%s)" -ge "$deadline" ]; then# RUNNING ANYWAY IS THE RIGHT END, and it is the difference between this# lock and a build mutex. The build mutex exists to stop two builds; this# one exists to stop a torn READ, and a torn read is caught downstream by# `require_rust_targets`' re-read. Failing the job here would turn a# contention this lock was added to smooth into an outage it caused. echo "$RUSTUP_LOCK_LOG_PREFIX waited ${wait_s}s on ${holder:-an ownerless} lock at $lock — going ahead without it." >&2"$@"return $?fi sleep "$poll_s"done echo "$$" >"$lock/pid"2>/dev/null || true"$@" status=$? rm -rf "$lock"return $status}