Skip to content

Commit

Permalink
worker_threads: fix spawning from preload scripts
Browse files Browse the repository at this point in the history
Fix spawning nested worker threads from preload scripts and
warn about doing so.

Signed-off-by: James M Snell <jasnell@gmail.com>
Fixes: #36531
  • Loading branch information
jasnell committed Feb 22, 2021
1 parent f3eb224 commit a2243e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
11 changes: 11 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,17 @@ Calling `unref()` on a worker allows the thread to exit if this is the only
active handle in the event system. If the worker is already `unref()`ed calling
`unref()` again has no effect.

## Notes

### Launching worker threads from preload scripts

Take care when launching worker threads from preload scripts (scripts loaded
and run using the `-r` command line flag). Worker threads automatically inherit
the command line flags from the running process and will preload the same
preload scripts as the main thread. If the preload script unconditionally
launches a worker thread, every thread spawned will spawn another until the
application crashes.

[Addons worker support]: addons.md#addons_worker_support
[ECMAScript module loader]: esm.md#esm_data_imports
[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ port.on('message', (message) => {
initializeCJSLoader();
initializeESMLoader();

const CJSLoader = require('internal/modules/cjs/loader');
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
loadPreloadModules();
initializeFrozenIntrinsics();
if (argv !== undefined) {
process.argv = ArrayPrototypeConcat(process.argv, argv);
}
Expand All @@ -147,6 +143,11 @@ port.on('message', (message) => {
};
workerIo.sharedCwdCounter = cwdCounter;

const CJSLoader = require('internal/modules/cjs/loader');
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
loadPreloadModules();
initializeFrozenIntrinsics();

if (!hasStdin)
process.stdin.push(null);

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/worker-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Worker, isMainThread, workerData } = require('worker_threads');

let count = isMainThread ? 0 : workerData + 1;
if (workerData < 2) {
new Worker('1 + 1', { eval: true, count });
}
10 changes: 10 additions & 0 deletions test/parallel/test-preload-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');
const worker = fixtures.path('worker-preload.js');
const { exec } = require('child_process');
const kNodeBinary = process.argv[0];


exec(`"${kNodeBinary}" -r "${worker}" -pe "1+1"`, common.mustSucceed());

0 comments on commit a2243e1

Please sign in to comment.