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,