Cotton code
97 lines · 3.8 KB · TypeScriptRaw
/** * `scripts/ci/sccache-up.sh` — the compile cache's server is checked before * cargo runs and, when dead, started in a session of its own. Run 5645's mac * job on one Mac (11 Oct 2026) lost its server mid-build — a server stays in the * process group of the client that started it, and dies with it — and spent * 19m33s compiling uncached with nothing louder than one warning per crate. */import { spawnSync } from "node:child_process";import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, describe, expect, it } from "vitest";const ROOT = join(__dirname, "..", "..");const SCRIPT = join(ROOT, "scripts/ci/sccache-up.sh");const temps: string[] = [];const listeners: number[] = [];afterEach(() => {  for (const pid of listeners.splice(0)) {    try {      process.kill(pid, "SIGKILL");    } catch {      // already gone    }  }  for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true });});/** A fake sccache whose `--start-server` leaves a listener on the socket behind, as the real one does. */function fake() {  const dir = mkdtempSync(join(tmpdir(), "sccache-up-"));  temps.push(dir);  const socket = join(dir, "s.sock");  const bin = join(dir, "sccache");  writeFileSync(    bin,    [      "#!/usr/bin/env bash",      `echo "$1" >> "${dir}/calls"`,      `python3 -c 'import socket, syss = socket.socket(socket.AF_UNIX); s.bind(sys.argv[1]); s.listen(5)while True: s.accept()[0].close()' "${socket}" &`,      `echo $! > "${dir}/listener"`,      "sleep 0.5",    ].join("\n"),    { mode: 0o755 },  );  const run = () =>    spawnSync("bash", [SCRIPT], {      encoding: "utf8",      env: { ...process.env, SCCACHE_BIN: bin, SCCACHE_SERVER_UDS: socket },      timeout: 30_000,    });  return { dir, run };}// Read in-process (scripts/lib/mac_disk_policy.py), never through the setuid `ps` a lane's seatbelt refuses.const pgid = (pid: number): string => {  const read = spawnSync("python3", ["-I", "-c", `import sys; sys.path.insert(0, sys.argv[1])from mac_disk_policy import process_tableprint(next((str(g) for p, _, g in process_table() if p == int(sys.argv[2])), ""))`, join(ROOT, "scripts/lib"), String(pid)], { encoding: "utf8" });  expect(read.status, read.stderr).toBe(0);  return read.stdout.trim();};describe("sccache-up.sh", () => {  // RULE: A dead compile-cache server is started again before cargo runs, in a session of its own so no job's group kill takes it, and the log warns that it was dead.  it("starts a dead server outside the caller's process group, and says so", () => {    const { dir, run } = fake();    const out = run();    expect(out.status, out.stderr).toBe(0);    expect(out.stderr).toContain("::warning::sccache: no server was listening");    expect(out.stderr).toContain("server up again");    const listener = Number(readFileSync(join(dir, "listener"), "utf8").trim());    listeners.push(listener);    expect(pgid(listener)).not.toBe("");    expect(pgid(listener)).not.toBe(pgid(process.pid));  });  it("leaves a listening server alone, says nothing and starts nothing", () => {    const { dir, run } = fake();    expect(run().status).toBe(0);    listeners.push(Number(readFileSync(join(dir, "listener"), "utf8").trim()));    rmSync(join(dir, "calls"));    const again = run();    expect(again.status).toBe(0);    expect(again.stderr).toBe("");    expect(existsSync(join(dir, "calls"))).toBe(false);  });  it("the cargo lock supervises the command with the lock descriptor closed", () => {    const lock = readFileSync(join(ROOT, "scripts/ci/with-cargo-lock.sh"), "utf8");    expect(lock).toMatch(/sccache-up\.sh" --run "\$@"/);    expect(lock).toContain('"${cargo_command[@]}" 9>&-');  });});