Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker_threads: fix spawning from preload scripts #37481

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
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
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 });
}
jasnell marked this conversation as resolved.
Show resolved Hide resolved
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());