Cotton code
104 lines · 4.6 KB · ShellRaw
#!/usr/bin/env bash# Empty the sccache compile cache WITHOUT breaking the server that owns it.##   scripts/reclaim-sccache.sh [--dir DIR] [--dry-run]## WHY THIS IS A SCRIPT AND NOT AN `rm -rf`. `scripts/disk-guard.sh`'s deep tier# used to reclaim the cache with a bare# `rm -rf ~/.cache/cottondb/rust/sccache`, and the sccache SERVER kept running# across it. The server opens its cache directory once, at start, and never# recreates it: every write after that delete fails, silently, for as long as# that server lives — and `SCCACHE_IDLE_TIMEOUT=0` (set by# `scripts/ci/setup-sccache.sh`, so a job's `--show-stats` reports its own work)# means it lives until the Mac reboots. Measured on one Mac, 2026-09-22, after# one such reclaim: 29,679 cache WRITE errors and a 4.32% hit rate on Rust# compiles — a compile cache that cost every build its wrapper overhead and# returned nothing. The disk was reclaimed once; the cache never came back.## So the order is: STOP THE SERVER, remove the cache, RECREATE the directory,# and let the next build start a fresh server against it. A stop is cheap — the# server is a daemon holding no work — and the next `sccache` client daemonises# a new one automatically.## THE ORDINARY BOUND IS NOT THIS SCRIPT. `SCCACHE_CACHE_SIZE` (40G) makes the# cache LRU-evicting: it never grows past its cap, which is why the sweeps are# taught nothing about it and `scripts/ci/require-free-disk.sh` says to leave it# alone. This runs only in disk-guard's DEEP tier — the last resort on a machine# that is still below its headroom after everything else has been taken.## Guard: `scripts/__tests__/reclaim-sccache.test.ts`.set -uo pipefailDIR="${SCCACHE_DIR:-$HOME/.cache/cottondb/rust/sccache}"DRY_RUN=0while [ $# -gt 0 ]; do  case "$1" in    --dir) DIR="${2-}"; shift 2 ;;    --dry-run) DRY_RUN=1; shift ;;    *) echo "usage: reclaim-sccache.sh [--dir DIR] [--dry-run]" >&2; exit 2 ;;  esacdonelog() { echo "[reclaim-sccache] $*" >&2; }if [ -z "$DIR" ] || [ "$DIR" = "/" ]; then  log "refusing to reclaim '$DIR' — that is not a cache directory"  exit 2fi# shellcheck source=/dev/null. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/owned-root.sh"refuse_wide_root "the sccache directory (--dir / SCCACHE_DIR)" "$DIR"if evidence_kept "$DIR"; then exit 0; fi# THE PINNED COPY FIRST. `scripts/ci/setup-sccache.sh` installs one per machine# under `~/.cache/cottondb/rust/bin` and that is the binary whose server is# holding this directory; a Homebrew sccache of some other version on somebody's# PATH would talk to a different server on a different port.BIN=""for candidate in "$HOME/.cache/cottondb/rust/bin/sccache" "$(command -v sccache 2>/dev/null || true)"; do  if [ -n "$candidate" ] && [ -x "$candidate" ]; then BIN="$candidate"; break; fidoneif [ "$DRY_RUN" = 1 ]; then  log "would stop the server (${BIN:-no sccache binary found}), empty $DIR, and recreate it"  exit 0fi# STOP BEFORE DELETE, and never the other way round. `--stop-server` against a# server that is not running exits non-zero and is not a failure: there is# simply nothing holding the directory, which is the state this script is trying# to reach.if [ -n "$BIN" ]; then  SCCACHE_DIR="$DIR" "$BIN" --stop-server >/dev/null 2>&1 \    && log "stopped the sccache server holding $DIR" \    || log "no sccache server was holding $DIR"else  log "no sccache binary on this machine — nothing to stop"fi# AND ONLY IF NOTHING IS LEFT RUNNING. `--stop-server` speaks to ONE server —# the one on this binary's port — and says nothing about any other: a server# started by another sccache (a Homebrew copy, `rust-verify.sh`'s own, one on a# different `SCCACHE_SERVER_PORT`), or a client mid-compile, is still writing# into this directory. `pgrep -x sccache` sees every one of them, server or# client, whatever started it. One second's grace for the stopped server to# exit; after that a live sccache is a reason to leave the cache alone, and a# machine with no `pgrep` to ask is the same answer — the guard cannot see.sccache_alive() {  command -v pgrep >/dev/null 2>&1 || return 0  pgrep -x sccache >/dev/null 2>&1}if sccache_alive; then  sleep 1fiif sccache_alive; then  log "an sccache process is still running (or pgrep cannot say) — leaving $DIR alone; a cache is never removed under a live server"  exit 1firm -rf "$DIR" 2>/dev/null || true# RECREATED, ALWAYS. A missing directory is the other half of the same failure:# a server started against a path that does not exist writes nothing either.mkdir -p "$DIR" 2>/dev/null || truelog "emptied $DIR — the next build starts a fresh server against it"