diff --git a/suite/scripts/clean-helpers.sh b/suite/scripts/clean-helpers.sh index 5e27938a19..f572589c90 100644 --- a/suite/scripts/clean-helpers.sh +++ b/suite/scripts/clean-helpers.sh @@ -70,7 +70,8 @@ clean_vlt_cache() { fi } -# vlt_background_running: whether any of vlt's detached children is running. +# vlt_background_running / vlt_background_pids: vlt's detached children of +# this run (BENCH_RUN_ID), or of the user when that is unset. source "$(dirname "${BASH_SOURCE[0]}")/vlt-background.sh" # Wait for vlt's detached children to finish (cache unpacking, manifest @@ -99,6 +100,15 @@ wait_for_vlt_background() { return 1 } +# Stop the vlt children wait_for_vlt_background gave up on: only the matched +# processes, so a run on a shared machine never stops another run's (or the +# user's) vlt. +stop_vlt_background() { + local pids + pids=$(vlt_background_pids) + [ -z "$pids" ] || echo "$pids" | xargs kill || true +} + # Function to safely clean bun cache clean_bun_cache() { if command -v bun &> /dev/null; then @@ -427,9 +437,8 @@ else # Never block a benchmark forever: past the limit, stop the process # so it can't overlap what comes next. if ! wait_for_vlt_background; then - echo "Warning: vlt-cache-unzip still running after 300s, stopping it" - pkill -u "$(id -u)" -x vlt-cache-unzip || true - pkill -u "$(id -u)" -f 'cache-unzip-src-unzip[.]js' || true + echo "Warning: vlt background work still running after 300s, stopping it" + stop_vlt_background fi ;; help|--help|-h) diff --git a/suite/scripts/clean-helpers.test.js b/suite/scripts/clean-helpers.test.js index 04d8036469..300533a914 100644 --- a/suite/scripts/clean-helpers.test.js +++ b/suite/scripts/clean-helpers.test.js @@ -129,13 +129,22 @@ test("clean_node_modules removes the workspaces' node_modules too, not the tools assert.equal(exists(".upm-store/b/node_modules/b/package.json"), true); }); +// The environment the helpers run in: the test process's, minus any +// BENCH_RUN_ID, so a benchmark running on this machine can't scope the tests. +const baseEnv = { ...process.env }; +delete baseEnv.BENCH_RUN_ID; +// `runId` set: the helper runs as that benchmark run; unset: no run id. +const helperEnv = (runId, extra = {}) => + runId === undefined ? { ...baseEnv, ...extra } : { ...baseEnv, BENCH_RUN_ID: runId, ...extra }; + // A stand-in for one of vlt's detached children: `sleep` copied to a file // with the given name, so pgrep sees that process name (truncated to 15 // characters, as Linux does), or a node script at a vlt-style path when // `script` is set. Started from a throwaway sh, like vlt's detached children, // so it is reaped when it exits instead of lingering as a zombie of this -// (blocked) test process. -const startFake = (t, seconds, { name = "vlt-cache-unzip", script } = {}) => { +// (blocked) test process. `runId` puts BENCH_RUN_ID in its environment, as a +// child of that benchmark run would have. Returns its pid. +const startFake = (t, seconds, { name = "vlt-cache-unzip", script, runId } = {}) => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "vlt-child-test-")); let argv; if (script) { @@ -152,6 +161,7 @@ const startFake = (t, seconds, { name = "vlt-cache-unzip", script } = {}) => { const pid = Number( execFileSync("sh", ["-c", `"$@" >/dev/null 2>&1 & echo $!`, "sh", ...argv], { encoding: "utf8", + env: helperEnv(runId), }), ); t.after(() => { @@ -160,18 +170,29 @@ const startFake = (t, seconds, { name = "vlt-cache-unzip", script } = {}) => { } catch {} fs.rmSync(dir, { recursive: true, force: true }); }); + return pid; }; -const startFakeUnzip = (t, seconds) => startFake(t, seconds); +const startFakeUnzip = (t, seconds, opts) => startFake(t, seconds, opts); -const waitForVltBackground = (limit) => - spawnSync( - "bash", - ["-c", `source "$HELPERS" >/dev/null; wait_for_vlt_background ${limit}`], - { - encoding: "utf8", - env: { ...process.env, HELPERS: path.join(__dirname, "clean-helpers.sh") }, - }, - ); +const alive = (pid) => { + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } +}; + +// Runs `command` after sourcing clean-helpers.sh, as benchmark run `runId` +// (unset: no run id). +const runHelper = (command, runId) => + spawnSync("bash", ["-c", `source "$HELPERS" >/dev/null; ${command}`], { + encoding: "utf8", + env: helperEnv(runId, { HELPERS: path.join(__dirname, "clean-helpers.sh") }), + }); + +const waitForVltBackground = (limit, runId) => + runHelper(`wait_for_vlt_background ${limit}`, runId); test("wait_for_vlt_background: returns at once when nothing is running", () => { const start = Date.now(); @@ -194,6 +215,46 @@ test("wait_for_vlt_background: fails after the limit", (t) => { assert.equal(result.status, 1); }); +test("wait_for_vlt_background: waits for a child of this run", (t) => { + startFakeUnzip(t, 1.5, { runId: "run-a" }); + const start = Date.now(); + const result = waitForVltBackground(10, "run-a"); + assert.equal(result.status, 0, result.stderr); + assert.ok(Date.now() - start >= 1200, "returned before the process exited"); +}); + +test("wait_for_vlt_background: ignores children of other runs and of no run", (t) => { + startFakeUnzip(t, 30, { runId: "run-b" }); + startFakeUnzip(t, 30); + const start = Date.now(); + const result = waitForVltBackground(5, "run-a"); + assert.equal(result.status, 0, result.stderr); + assert.ok(Date.now() - start < 2000, "waited on another run's process"); +}); + +test("wait_for_vlt_background: with no run id, waits for any run's child", (t) => { + startFakeUnzip(t, 1.5, { runId: "run-b" }); + const start = Date.now(); + const result = waitForVltBackground(10); + assert.equal(result.status, 0, result.stderr); + assert.ok(Date.now() - start >= 1200, "returned before the process exited"); +}); + +test("stop_vlt_background: stops only this run's children", (t) => { + const own = startFakeUnzip(t, 30, { runId: "run-a" }); + const ownScript = startFake(t, 30, { script: "cache-unzip-src-unzip.js", runId: "run-a" }); + const other = startFakeUnzip(t, 30, { runId: "run-b" }); + const none = startFakeUnzip(t, 30); + execFileSync("sleep", ["0.3"]); + const result = runHelper("wait_for_vlt_background 1 || stop_vlt_background", "run-a"); + assert.equal(result.status, 0, result.stderr); + execFileSync("sleep", ["0.3"]); + assert.equal(alive(own), false, "this run's child is still running"); + assert.equal(alive(ownScript), false, "this run's script child is still running"); + assert.equal(alive(other), true, "another run's child was stopped"); + assert.equal(alive(none), true, "a child of no run was stopped"); +}); + test("hyperfine-wait.sh: wraps prepare, setup and cleanup", () => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "hyperfine-wait-test-")); try { @@ -248,9 +309,9 @@ test("hyperfine-wait.sh: wraps prepare, setup and cleanup", () => { } }); -const vltBackgroundRunning = () => +const vltBackgroundRunning = (runId) => spawnSync("bash", ["-c", `source "$VB"; vlt_background_running`], { - env: { ...process.env, VB: path.join(__dirname, "vlt-background.sh") }, + env: helperEnv(runId, { VB: path.join(__dirname, "vlt-background.sh") }), }).status; test("vlt_background_running: false when nothing is running", () => { @@ -268,6 +329,13 @@ for (const [label, opts] of [ execFileSync("sleep", ["0.3"]); assert.equal(vltBackgroundRunning(), 0); }); + test(`vlt_background_running: matches ${label} only for its run`, (t) => { + startFake(t, 30, { ...opts, runId: "run-a" }); + execFileSync("sleep", ["0.3"]); + assert.equal(vltBackgroundRunning("run-a"), 0); + assert.equal(vltBackgroundRunning("run-b"), 1); + assert.equal(vltBackgroundRunning(), 0, "no run id should match any run's child"); + }); } // Record what a cleaner removes, without touching anything real. diff --git a/suite/scripts/registry/common.sh b/suite/scripts/registry/common.sh index aa89c2954f..ee088473b4 100644 --- a/suite/scripts/registry/common.sh +++ b/suite/scripts/registry/common.sh @@ -51,6 +51,10 @@ BENCH_RUNS="${BENCH_RUNS:=5}" BENCH_TIMEOUT="${BENCH_TIMEOUT:=300}" BENCH_LOGLEVEL="${BENCH_LOGLEVEL:=http}" BENCH_OUTPUT_FOLDER="$BENCH_RESULTS/$BENCH_FIXTURE/$BENCH_VARIATION" +# Marks every process this run starts, vlt's detached children included, so +# waiting for (or stopping) vlt's background work only reaches this run's +# processes, not another checkout's on the same machine. See vlt-background.sh. +export BENCH_RUN_ID="${BENCH_RUN_ID:-$(date +%s)-$$-$RANDOM}" # Add --force for large fixture to bypass peer dependency errors FORCE_FLAG="" diff --git a/suite/scripts/variations/common.sh b/suite/scripts/variations/common.sh index 26ed22914d..ca13a0a07c 100644 --- a/suite/scripts/variations/common.sh +++ b/suite/scripts/variations/common.sh @@ -58,6 +58,10 @@ for pm in npm yarn berry zpm pnpm vlt bun deno aube upm nx turbo vp node; do fi done BENCH_OUTPUT_FOLDER="$BENCH_RESULTS/$BENCH_FIXTURE/$BENCH_VARIATION" +# Marks every process this run starts, vlt's detached children included, so +# waiting for (or stopping) vlt's background work only reaches this run's +# processes, not another checkout's on the same machine. See vlt-background.sh. +export BENCH_RUN_ID="${BENCH_RUN_ID:-$(date +%s)-$$-$RANDOM}" # Where yarn classic, berry and zpm live (BENCH_YARN_BIN, BENCH_BERRY_BIN, ...) source "$BENCH_SCRIPTS/pm-bins.sh" if [ -n "$BENCH_INCLUDE_BERRY" ] && [ -z "$BENCH_BERRY_VERSION" ]; then diff --git a/suite/scripts/vlt-background.sh b/suite/scripts/vlt-background.sh index 2b173f9f4c..c2067d2818 100644 --- a/suite/scripts/vlt-background.sh +++ b/suite/scripts/vlt-background.sh @@ -18,7 +18,42 @@ # before a child renames itself. Matching the name, not the command line, for # the first check means a shell whose arguments mention these names can never # match itself; the [.] does the same for the second. +# +# Those two checks match every vlt child of the current user. On a shared +# machine that includes other benchmark checkouts and the user's own vlt +# commands, which a run would then wait on, or stop. So each run exports a +# unique BENCH_RUN_ID (variations/common.sh, registry/common.sh); every +# process the run starts inherits it, and vlt passes `process.env` on to its +# detached children, so they carry it too. When BENCH_RUN_ID is set, only +# processes whose environment holds this run's id count. When it is unset (a +# script run by hand), every vlt child of the user counts, as before. + +# vlt_background_pids: the pids of vlt's detached children of this run, one +# per line (none: empty output). Never fails. +vlt_background_pids() { + local pids pid + pids=$({ + pgrep -u "$(id -u)" '^vlt-' + pgrep -u "$(id -u)" -f '/vlt/[a-z-]+-src-[a-z-]+[.]js' + } | sort -un) || true + if [ -z "${BENCH_RUN_ID:-}" ]; then + [ -z "$pids" ] || printf '%s\n' "$pids" + return 0 + fi + for pid in $pids; do + # The process may have exited since pgrep, and environ may be unreadable + # (not our process after all): a process whose id can't be read isn't + # treated as ours. + if tr '\0' '\n' < "/proc/$pid/environ" 2>/dev/null | + grep -qxF -- "BENCH_RUN_ID=$BENCH_RUN_ID"; then + printf '%s\n' "$pid" + fi + done + return 0 +} + +# vlt_background_running: whether any of vlt's detached children of this run +# is running. vlt_background_running() { - pgrep -u "$(id -u)" '^vlt-' >/dev/null || - pgrep -u "$(id -u)" -f '/vlt/[a-z-]+-src-[a-z-]+[.]js' >/dev/null + [ -n "$(vlt_background_pids)" ] }