Skip to content

Commit

Permalink
workers: 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

PR-URL: #37481
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell authored and targos committed Mar 2, 2021
1 parent cbfc6b1 commit d93137b
Show file tree
Hide file tree
Showing 4 changed files with 35 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). Unless the `execArgv` option is
explicitly set, new 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) {
ArrayPrototypePushApply(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
9 changes: 9 additions & 0 deletions test/fixtures/worker-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {
Worker,
workerData,
threadId
} = require('worker_threads');

if (threadId < 2) {
new Worker('1 + 1', { eval: true });
}
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 d93137b

Please sign in to comment.