Files
orca/config/scripts/test-runtime/node-worker/vitest-worker-parent-exit.cjs
Brennan Benson ae2ed119a4 fix(test): exit vitest workers when their runner dies (#42928)
* fix(test): exit vitest workers when their runner dies

Bun fork workers outlived a killed Vitest main process for hours at ~1GB each.
A preload in every worker polls the parent and exits once it is gone; the
node-runtime worker's disconnect listener moves into it because Bun stops
delivering Vitest's IPC messages once a 'disconnect' listener exists.

* fix(test): probe the worker's parent with the real process.kill

Tests that mock process.kill made the parent probe exit the worker.
2026-11-15 18:58:48 -08:00

26 lines
797 B
JavaScript

// Pools stop workers by signal, so a dead parent means Vitest died; don't outlive it mid-file.
// Polls because Bun stops delivering Vitest's IPC messages once a 'disconnect' listener exists.
if (process.send) {
// Bound before Vitest swaps process.exit for a throwing guard and tests mock process.kill.
const exit = process.exit.bind(process)
const kill = process.kill.bind(process)
const parent = process.ppid
const parentAlive = () => {
// POSIX reparents orphans; Windows keeps the stale ppid, so probe it too.
if (process.ppid !== parent) {
return false
}
try {
kill(parent, 0)
return true
} catch (error) {
return error.code === 'EPERM'
}
}
setInterval(() => {
if (!parentAlive()) {
exit(1)
}
}, 1000).unref()
}