Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: prevent EBUSY cleanup masking real assertion in detachedSpawn Windows test (#8138)

[._.]/ Adam Eivy9a1f1efa6ae4
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.js
index 4c6f0f051c..a093379cd6 100644
--- a/server/lib/detachedSpawn.js
+++ b/server/lib/detachedSpawn.js
@@ -752,13 +752,33 @@ const splitWindowsCommand = (command) => {
// One PowerShell SPAWN per call. Fine for the one-per-launch probes that reach
// it today; a fan-out or polled caller must instead take a single
// `Get-CimInstance Win32_Process` snapshot and match every PID against it.
+//
+// A loaded CI runner can leave this CIM query slower than a snappy timeout
+// tolerates (#8138) — a caller reads a thrown/timed-out query the same as any
+// other probe failure (fail-closed, rethrown — see processMatches below), so
+// a merely-slow host must not be treated as "job identity unknown" on the
+// first hiccup. Give it real headroom and one retry before letting the error
+// propagate.
+const CIM_QUERY_TIMEOUT_MS = 15000;
+const queryWin32Command = async (pid, attempt = 0) => {
+ try {
+ const { stdout } = await execFileAsync('powershell', [
+ '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command',
+ // `pid` is already parsed by Number.parseInt and range-checked by every
+ // caller, so it cannot carry anything but digits into this filter.
+ `$p = Get-CimInstance Win32_Process -Filter 'ProcessId=${pid}' -ErrorAction SilentlyContinue; if ($p) { [Console]::Out.WriteLine($p.CommandLine) }`,
+ ], safeChildProcessOptions({ timeout: CIM_QUERY_TIMEOUT_MS }));
+ return stdout;
+ } catch (err) {
+ if (attempt === 0) {
+ console.warn(`⚠️ CIM process query for pid ${pid} failed, retrying once: ${err.message}`);
+ return queryWin32Command(pid, 1);
+ }
+ throw err;
+ }
+};
const processMatchesWin32 = async (pid, { executable, args }) => {
- const { stdout } = await execFileAsync('powershell', [
- '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command',
- // `pid` is already parsed by Number.parseInt and range-checked by every
- // caller, so it cannot carry anything but digits into this filter.
- `$p = Get-CimInstance Win32_Process -Filter 'ProcessId=${pid}' -ErrorAction SilentlyContinue; if ($p) { [Console]::Out.WriteLine($p.CommandLine) }`,
- ], safeChildProcessOptions({ timeout: 5000 }));
+ const stdout = await queryWin32Command(pid);
const command = stdout.trim();
// The process exited between the liveness probe and here, or Windows refused
// the query — either way this is not our job.
diff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.js
index a4c451944b..3b252c83a8 100644
--- a/server/lib/detachedSpawn.test.js
+++ b/server/lib/detachedSpawn.test.js
@@ -35,9 +35,6 @@ const tmpControlDir = async () => {
dirs.push(d);
return d;
};
-afterEach(async () => {
- await Promise.all(dirs.splice(0).map((d) => rm(d, { recursive: true, force: true })));
-});
// Collect a stream's 'data' chunks into a single string.
const collect = (emitter) => {
@@ -77,6 +74,53 @@ const isAliveForTest = (pid) => {
try { process.kill(pid, 0); return true; } catch { return false; }
};
+// Best-effort: kill whatever detached child a test left running under this
+// control dir, and WAIT for it to actually exit, before the control dir gets
+// removed. Skipping this races the rm against a still-open stderr.log handle:
+// the OS refuses the unlink with EBUSY (Windows) or ENOTEMPTY, and that
+// secondary cleanup failure was masking whatever assertion the test actually
+// failed on (#8138). Read the pid straight off disk and kill it directly with
+// `taskkill`/SIGKILL rather than going through `killProcessTree` — several
+// tests in this file mock that import, and a leftover process must still die
+// for real in cleanup regardless of what a given test stubbed.
+const killLeftoverChild = async (controlDir) => {
+ const pidRaw = await readFile(join(controlDir, 'pid'), 'utf8').catch(() => '');
+ const pid = Number.parseInt(pidRaw, 10);
+ if (!Number.isFinite(pid) || pid <= 0) return;
+ // A handful of fixtures write `process.pid` (this very worker) into a
+ // synthetic control dir to stand in for "some live process" — killing it
+ // would take the test runner down with it, not the job under test.
+ if (pid === process.pid) return;
+ // The supervisor's `exit` sentinel means the job already finished and this
+ // control dir's pid is stale — the OS is free to have handed that number to
+ // an unrelated process by the time cleanup runs (this suite spawns/reaps
+ // dozens of short-lived children per file). Only signal a pid whose own job
+ // is still marked running, exactly like reapDetached's own guard, so a
+ // best-effort cleanup can never SIGKILL a process this test never spawned.
+ const exitWritten = (await readFile(join(controlDir, 'exit'), 'utf8').catch(() => '')).length > 0;
+ if (exitWritten) return;
+ if (IS_POSIX) {
+ try { process.kill(pid, 'SIGKILL'); } catch { /* already gone */ }
+ } else {
+ // `/T` takes the job's own descendants with it too, so a leftover
+ // ffmpeg/python fixture doesn't linger past the test that spawned it.
+ await execFileAsync('taskkill', ['/pid', String(pid), '/T', '/F']).catch(() => {});
+ }
+ await waitUntil(() => !isAliveForTest(pid), { timeoutMs: 5000 });
+};
+
+afterEach(async () => {
+ const pending = dirs.splice(0);
+ await Promise.all(pending.map(async (d) => {
+ await killLeftoverChild(d).catch(() => {});
+ // Retry on EBUSY/ENOTEMPTY (Windows can hold stderr.log open a beat after
+ // the process exits, and the wait above is best-effort) rather than
+ // letting cleanup itself throw and mask the real test failure alongside
+ // it.
+ await rm(d, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
+ }));
+});
+
const ppidOf = async (pid) => {
const { stdout } = await execFileAsync('ps', ['-o', 'ppid=', '-p', String(pid)]).catch(() => ({ stdout: '' }));
const n = Number.parseInt(stdout.trim(), 10);