From ae2ed119a4e101f70de0c99bd1fcc1ab5670794a Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:58:48 -0800 Subject: [PATCH] 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. --- .../vitest-node-runtime-worker.mjs | 4 --- .../node-worker/vitest-worker-parent-exit.cjs | 25 +++++++++++++++++++ config/vitest.config.ts | 7 +++++- 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 config/scripts/test-runtime/node-worker/vitest-worker-parent-exit.cjs diff --git a/config/scripts/test-runtime/node-worker/vitest-node-runtime-worker.mjs b/config/scripts/test-runtime/node-worker/vitest-node-runtime-worker.mjs index 10af691cf6e..ef692d24ee0 100644 --- a/config/scripts/test-runtime/node-worker/vitest-node-runtime-worker.mjs +++ b/config/scripts/test-runtime/node-worker/vitest-node-runtime-worker.mjs @@ -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), diff --git a/config/scripts/test-runtime/node-worker/vitest-worker-parent-exit.cjs b/config/scripts/test-runtime/node-worker/vitest-worker-parent-exit.cjs new file mode 100644 index 00000000000..068053cc6bf --- /dev/null +++ b/config/scripts/test-runtime/node-worker/vitest-worker-parent-exit.cjs @@ -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() +} diff --git a/config/vitest.config.ts b/config/vitest.config.ts index ed0495975f7..608ad30fcd3 100644 --- a/config/vitest.config.ts +++ b/config/vitest.config.ts @@ -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,