Cotton code
103 lines · 4.4 KB · TypeScriptRaw
/** * A JOB THAT NEVER FAILS NEVER RELEASES THE LOCK. * * `scripts/with-build-lock.sh` honours a live holder indefinitely, which is the * right rule for a long legitimate build and the wrong one for a wedged job. On * one Mac on 2026-09-23 (PR #915, run 100) sccache's server wedged, the `swift` job * sat at 0% CPU for seventy-seven minutes, and the machine-wide lock stayed * taken the whole time because nothing in the lane had an opinion about how * long it should take. * * So a CALLER may declare a ceiling — the swift and ios steps of * `checks.yml` pass fifty minutes — and this drives it for real: a command * that outruns its ceiling is stopped, the script exits 124, and the LOCK * DIRECTORY IS GONE afterwards, which is the whole point. With no ceiling the * wrapper behaves exactly as it always did. */import { spawnSync } from "node:child_process";import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import { afterEach, describe, expect, it } from "vitest";import { publishBuildPeaks, publishDiskReading } from "./helpers/disk-reading";const ROOT = resolve(__dirname, "../..");const SCRIPT = join(ROOT, "scripts/with-build-lock.sh");let scratch = "";afterEach(() => {  if (scratch) rmSync(scratch, { recursive: true, force: true });  scratch = "";});function run(args: string[], env: NodeJS.ProcessEnv) {  scratch ||= mkdtempSync(join(tmpdir(), "build-lock-ceiling-"));  const lock = join(scratch, "lock");  // A scratch HOME with a fresh device reading: on a Mac the wrapper admits  // through the device policy, which reads it there — never the person's HOME.  const home = join(scratch, "home");  publishDiskReading(home);  // A byte of RAM per build: admission must not turn on the machine's free RAM.  publishBuildPeaks(home);  const result = spawnSync("bash", [SCRIPT, ...args], {    encoding: "utf8",    env: {      ...process.env, HOME: home, BUILD_LOCK_MEMORY_BYTES: "17179869184",      BUILD_LOCK_DIR: lock,      BUILD_LOCK_LOG: join(scratch, "wait.log"),      BUILD_LOCK_LABEL: "guarded-lane",      // Never inherit a real holder's marker into the child.      TINMEN_BUILD_LOCK_HELD: "",      TINMEN_BUILD_LOCK_DIR: "",      // A scratch cargo target, not named shared, as build-lock-ci-first.test.ts says:      // with none, a worktree's generated .cargo/config.toml hands the wrapper the      // machine's real shared target, and every command here waits on that target's      // cargo lock behind whatever cargo build is running on the Mac.      CARGO_TARGET_DIR: join(scratch, "target"),      ...env,    },  });  return { ...result, lock };}describe("with-build-lock.sh — the per-job ceiling", () => {  it("is off by default: a command slower than any ceiling still runs to the end", () => {    const r = run(["bash", "-c", "sleep 1; echo done"], {});    expect(r.status, r.stderr).toBe(0);    expect(r.stdout).toContain("done");    expect(r.stderr).not.toContain("ceiling");  });  it("passes a command's own exit status through untouched when a ceiling is set", () => {    const r = run(["bash", "-c", "exit 7"], { BUILD_LOCK_MAX_MIN: "50" });    expect(r.status).toBe(7);    expect(r.stderr).not.toContain("CEILING");  });  it("stops a command that outruns its ceiling, and says 124", () => {    // The ceiling is in minutes, so the shortest real one is 1 — and the    // watchdog is asserted against a command that would run for an hour.    const r = run(["bash", "-c", "sleep 3600"], {      BUILD_LOCK_MAX_MIN: "1",      // The whole point of the knob: the test must not wait 60s for it.      BUILD_LOCK_CEILING_SECONDS: "2",    });    expect(r.status, r.stderr).toBe(124);    expect(r.stderr).toContain("CEILING");  });  it("RELEASES THE LOCK when it stops a command — the failure the ceiling exists for", () => {    const r = run(["bash", "-c", "sleep 3600"], {      BUILD_LOCK_MAX_MIN: "1",      BUILD_LOCK_CEILING_SECONDS: "2",    });    expect(r.status).toBe(124);    expect(existsSync(r.lock), "the lock directory must not outlive the job it killed").toBe(false);  });  it("announces the ceiling in the log, so a reader knows one was in force", () => {    const r = run(["bash", "-c", "true"], { BUILD_LOCK_MAX_MIN: "50" });    expect(r.status, r.stderr).toBe(0);    expect(r.stderr).toContain("ceiling: this command has 50m");  });});