-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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 thread throws error when started from --require
script
#36531
Comments
--require
script--require
script
/cc @nodejs/workers |
Just switching the initialization order here should be fine, I think: diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js
index 7734aaa2c201..6c7c4acb3759 100644
--- a/lib/internal/main/worker_thread.js
+++ b/lib/internal/main/worker_thread.js
@@ -120,10 +120,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);
}
@@ -146,6 +142,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);
Just note that your program still won’t work as it’s written right now, because |
When I catch the error with Are you saying that the fix you're proposing will cause it to start spawning workers recursively? |
Yes, that would be the expected (and documented) behavior for the code you shared here.
That’s because the Worker never gets around to spawning its main script, I assume. |
I can put What is the recommended approach to avoid recursive thread spawning? Read |
|
@cspotcode Maybe to clarify: It’s not the first worker, the one you want to spawn, that throws the exception. It’s the inner, second worker, that’s already being spawned by the first one as a preload module, that fails. So it’s not that Workers can’t be spawned from preload scripts in the main thread, it’s that Workers can’t be spawned from the preload scripts of other Worker threads. |
Thanks for the clarification, I see what you mean. This is tricky. If I'm writing a library that internally uses worker_threads to do useful things, and I want the library's implementation to be properly opaque, then I should always be passing an empty |
Is a preload script supposed to have access to |
I mean, that really depends … it’s hard to know what the correct behavior would be without knowing the concrete circumstances. Sometimes a user using
I think it should have that, yes.
🤷♀️ The patch that I suggested above would also resolve this problem as well. You can open a PR with it, if you like, it just needs a test to come with, I think. |
Ok thanks, I did not fully understand that One note on #36531 (comment), checking |
Fix spawning nested worker threads from preload scripts and warn about doing so. Signed-off-by: James M Snell <jasnell@gmail.com> Fixes: nodejs#36531
PR: #37481 |
Fix spawning nested worker threads from preload scripts and warn about doing so. Signed-off-by: James M Snell <jasnell@gmail.com> Fixes: nodejs#36531
Fix spawning nested worker threads from preload scripts and warn about doing so. Signed-off-by: James M Snell <jasnell@gmail.com> Fixes: nodejs#36531 PR-URL: nodejs#37481 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
What steps will reproduce the bug?
Run the following at a bash shell:
How often does it reproduce? Is there a required condition?
It reproduces every time. It happens when the worker is created by a
--require
script.What is the expected behavior?
No errors from the worker_thread.
What do you see instead?
Logs the following:
Additional information
The line number referenced in the error is here: https://github.com/nodejs/node/blob/master/lib/internal/main/worker_thread.js#L140
Based on a brief look at the code, I guess node is assuming that the worker receives a bootstrapping message with a bunch of values, one of them being
cwdCounter
. But when created via--require
, that message is not sent or has not arrived yet.The text was updated successfully, but these errors were encountered: