KITFORMA · EDITORIAL ANSWER
Why does starting one Node worker from a preload script create more workers repeatedly?
KitForma editorial guide. Worker threads inherit command-line execution arguments unless execArgv is explicitly configured. A script loaded with `node -r` can therefore run again inside its own worker and launch another worker recursively.
Step-by-step answer
node -r can therefore run again inside its own worker and launch another worker recursively.
Guard main-thread startup with isMainThread, and separate the worker entry point from application bootstrap. When the worker needs no inherited preload, pass an explicit empty execArgv array; otherwise select the required flags deliberately instead of dropping loaders or instrumentation blindly. Verify this with a bounded fixture that reports its thread identity and execution arguments and exits after one worker. Do not reproduce an unrestricted spawn loop on a shared machine. This is startup recursion, not excessive concurrency from a large job list, and increasing a worker-pool limit will not correct it.
Minimal example:
import assert from 'node:assert/strict';
import { Worker } from 'node:worker_threads';
import { once } from 'node:events';
const worker = new Worker(
`const { parentPort, isMainThread } = require('node:worker_threads');
parentPort.postMessage({ main: isMainThread, args: process.execArgv });`,
{ eval: true, execArgv: [] }
);
try {
const [result] = await once(worker, 'message');
assert.equal(result.main, false);
assert.deepEqual(result.args, []);
} finally {
await worker.terminate();
}
// One bounded worker verifies explicit argument isolation; no recursive preload.Sources and verification
Sources checked:
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.