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.
This commit is contained in:
Brennan Benson
2026-11-15 18:58:48 -08:00
committed by GitHub
parent 8d802a17f4
commit ae2ed119a4
3 changed files with 31 additions and 5 deletions
@@ -8,10 +8,6 @@ if (!process.send) {
throw new Error('Node Vitest worker requires an IPC channel')
}
const send = process.send.bind(process)
// Vitest replaces process.exit with a throwing guard once a file runs, and the error then spins on the dead channel.
const exit = process.exit.bind(process)
// The pool stops workers by signal, so a closed channel means Vitest died; don't outlive it mid-file.
process.once('disconnect', () => exit(1))
init({
post: (response) => send(response),
@@ -0,0 +1,25 @@
// 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()
}
+6 -1
View File
@@ -94,7 +94,12 @@ const testOptions = {
env: { ...gitEnv, ORCA_VITEST_RUNTIME: 'node' },
server: { deps: { inline: ['zod'] } },
// Node's storage globals and V8 retention checks require the existing child flags.
execArgv: ['--no-experimental-webstorage', '--expose-gc'],
execArgv: [
'--no-experimental-webstorage',
'--expose-gc',
'--require',
resolve('config/scripts/test-runtime/node-worker/vitest-worker-parent-exit.cjs')
],
setupFiles: UNIT_SETUP_FILES.map((file) => resolve(file)),
include: UNIT_INCLUDE,
exclude: balancedShards ? UNIT_EXCLUDE : defaultExclude,