Cotton code
106 lines · 4.6 KB · TypeScriptRaw
/** * A WEDGED SCCACHE SERVER IS STOPPED BEFORE A JOB ADOPTS IT. * * Measured on one Mac on 2026-09-23 (PR #915, run 100, the `swift` job): `cargo * build --profile release -p cotton-ffi` had three `sccache … rustc` children * pinned at 0% CPU for seventy-seven minutes while `sccache --show-stats` * answered normally — 64,907 requests, 51,118 executed. The "is it listening" * probe in `scripts/ci/setup-sccache.sh` said yes, the job inherited the wedge, * and it held the machine-wide build lock for the household the whole time. * `sccache --stop-server` unwedged it instantly. * * The decision lives in one awk program behind `--wedged-from-ps`, and this * drives it with the exact shapes `ps` prints on both platforms: GNU `etimes` * (whole seconds) on the Linux CI host, BSD `etime` ([[dd-]hh:]mm:ss) on the * check Macs. What is asserted is the DISTINCTION the watchdog exists for — old * AND idle is a wedge, old and busy is a long compile, new and idle is a * process between slices — because a watchdog that cannot tell those apart * would kill the cache under every release LTO unit in the tree. */import { spawnSync } from "node:child_process";import { readFileSync } from "node:fs";import { join, resolve } from "node:path";import { describe, expect, it } from "vitest";const ROOT = resolve(__dirname, "../..");const SCRIPT = join(ROOT, "scripts/ci/setup-sccache.sh");/** The verdict for one `ps` listing: minutes, or "" for "nothing wedged". */function verdict(psLines: string[], env: NodeJS.ProcessEnv = {}): string {  const r = spawnSync("bash", [SCRIPT, "--wedged-from-ps"], {    input: `${psLines.join("\n")}\n`,    encoding: "utf8",    env: { ...process.env, ...env },  });  expect(r.status, r.stderr).toBe(0);  return r.stdout.trim();}const RUSTC = "sccache /Users/someone/.rustup/toolchains/stable/bin/rustc --crate-name cotton_ffi";describe("setup-sccache.sh — the wedge decision", () => {  it("calls a 77-minute compile at 0% CPU a wedge, in GNU seconds", () => {    expect(verdict([`4620 0.0 ${RUSTC}`])).toBe("77");  });  it("reads the BSD elapsed-time spelling the check Macs print", () => {    expect(verdict([`01:17:00 0.0 ${RUSTC}`])).toBe("77");    expect(verdict([`1-00:00:00 0.0 ${RUSTC}`])).toBe("1440");    expect(verdict([`12:30 0.0 ${RUSTC}`])).toBe("12"); // mm:ss — 12m30s, past the 10m floor  });  it("leaves a long compile that is USING the CPU alone", () => {    expect(verdict([`4620 91.4 ${RUSTC}`])).toBe("");  });  it("leaves a young idle process alone — a process sampled between slices reads as 0%", () => {    expect(verdict([`120 0.0 ${RUSTC}`])).toBe("");  });  it("reports the OLDEST wedged compile when several are stuck together", () => {    expect(      verdict([`600 0.0 ${RUSTC}`, `4620 0.0 ${RUSTC}`, `1800 0.0 ${RUSTC}`]),    ).toBe("77");  });  it("only looks at sccache's own rustc children", () => {    expect(verdict(["4620 0.0 /usr/bin/rustc --crate-name serde"])).toBe("");    expect(verdict(["4620 0.0 sccache --show-stats"])).toBe("");    expect(verdict(["4620 0.0 xcodebuild -scheme Cotton"])).toBe("");  });  it("says nothing at all when ps says nothing", () => {    expect(verdict([])).toBe("");  });  it("takes its two thresholds from the environment, so a lane can widen them", () => {    expect(verdict([`900 0.0 ${RUSTC}`])).toBe("15"); // past the 10m floor    expect(verdict([`900 0.0 ${RUSTC}`], { SCCACHE_WEDGE_MIN: "30" })).toBe("");    expect(verdict([`2400 0.0 ${RUSTC}`], { SCCACHE_WEDGE_MIN: "30" })).toBe("40");    expect(verdict([`4620 5.0 ${RUSTC}`])).toBe("");    expect(verdict([`4620 5.0 ${RUSTC}`], { SCCACHE_WEDGE_CPU: "9" })).toBe("77");  });});describe("setup-sccache.sh — the watchdog runs before the server is adopted", () => {  const text = readFileSync(SCRIPT, "utf8");  it("stops the server rather than starting a second one", () => {    expect(text).toContain("sccache --stop-server");  });  it("decides BEFORE the `is it listening` probe adopts the old server", () => {    // The probe is a socket connect (`scripts/lib/sccache-alone.sh`), never a    // client command, which would start a server in the step's process group.    expect(text.indexOf("if sccache_listening; then")).toBeGreaterThan(-1);    expect(text.indexOf("wedged_minutes_from")).toBeLessThan(text.indexOf("if sccache_listening; then"));  });  it("fails open — a stop that does not work continues rather than failing the job", () => {    expect(text).toMatch(/sccache --stop-server[^\n]*\|\| echo/);  });  it("can be turned off for a lane that must not touch the server", () => {    expect(text).toContain("SCCACHE_NO_WATCHDOG");  });});