Open nicolo-ribaudo opened 2 years ago
@nodejs/workers
The same happens with env
.
When running NODE_OPTIONS=--openssl-legacy-provider node test.js
with Node.js 17.2.0, this works:
const { Worker, isMainThread } = require("worker_threads");
if (isMainThread) {
new Worker(__filename);
} else {
console.log("From worker!", process.env.NODE_OPTIONS);
}
but this throws
const { Worker, isMainThread } = require("worker_threads");
if (isMainThread) {
new Worker(__filename, { env: process.env });
} else {
console.log("From worker!", process.env.NODE_OPTIONS);
}
You can see that the
gc
function is present. This means that the disallowed flags are not ignored (and not even "ignored but still used to populate theprocess.execArgv
array): they are used and affect the runtime behavior.
This may be not right, as:
const { isMainThread, Worker } = require("worker_threads");
if (isMainThread) {
new Worker(__filename, {
execArgv: []
});
} else {
console.log("Hello from the worker!", process.execArgv, globalThis.gc);
}
run node --expose_gc test.js
will log:
Hello from the worker! [] [Function: gc]
Seems that this behavior is not controlled by work's execArgv
.
If execArgv
is not specified, all parent's args indeed are used:
But since these Per-Process options and V8 flags are from parents, I guess it is ok?
this have for sure been bugging me too, some flags gets added or removed in different versions. one needs to be able to figure out what flags are supported or not before you can pass this execArgv commands
Just ran into this using ava
https://github.com/avajs/ava/issues/3207
What's the word on this? Just hit this on https://github.com/taskforcesh/bullmq/issues/2075 which is a pain because without a change to that package, worker_threads can't be used.
+1 for Ignore the unsupported options
Opened https://github.com/nodejs/node/pull/53596 for the simpler case of NODE_OPTIONS, when the worker spawning code does not intend to modify NODE_OPTIONS and only copies the one from the parent because they want to update other environment variables.
I came across this error while building angular 18 app. I solved it by disabling SSR "prerender": false, "ssr": false inside project.json file
Game[spiking @dance ]()
I ran into this when building an old project that uses mocha-parallel-test and node's --openssl-legacy-provider option.
mocha-parallel-tests spawns workers and specifies execArgv to remove some 'debug' flags from the process execArgv. But it doesn't remove openssl-legacy-provider so node throws Error [ERR_WORKER_INVALID_EXEC_ARGV]: Initiated Worker with invalid execArgv flags: --openssl-legacy-provider
https://github.com/mocha-parallel/mocha-parallel-tests/blob/master/src/main/thread/worker.ts#L35-L36
Either node should ignore these options by default, as it appears to when using the default execArgv, or node should provide a function to remove unsupported options, or return process.execArgv
without per-process arguments.
Version
v17.2.0
Platform
No response
Subsystem
worker_threads
What steps will reproduce the bug?
Consider this
test.js
file:Run these two commands:
They will log
Now, uncomment the
execArgv: process.execArgv
line and run the two commands again. The first command will still loghowever, the second one throws:
This mans that the default behavior of the Worker
execArgv
option is not to inheritexecArgv
from the parent thread (contrary to what the documentation says).Why would you want to explicitly pass
execArgv: process.execArgv
anyway?A few days ago I was trying to implement a require hook that internally uses a
Worker
, and in order to avoid registering it recursively in an infinite worker-spawn loop I tried to filter out the--require
flag:this seemed to work, until it crashed in a test that was using the
--expose_gc
option.Today, I found the exact same problem in
jest-worker
(a Jest subpackage that is often also used outside of Jest): https://github.com/facebook/jest/pull/12103 was opened becausejest-worker
didn't work when running Node.js with--max_old_space_size
.Since Node.js doesn't provide the list of flags supported by workers progrmmatically (and I didn't find it in the docs either :sweat_smile:) there is no way to filter the
execArgv
used by a worker.What is the current behavior?
I initially expected the real default behavior to be something like
execArgv: process.execArgv.filter(isSupportedByWorker)
, but it looks like it's not either. Consider for example the--expose_gc
(which is disallowed in workers) option with this example:If you run
node --expose_gc test.js
, it will logYou can see that the
gc
function is present. This means that the disallowed flags are not ignored (and not even "ignored but still used to populate theprocess.execArgv
array): they are used and affect the runtime behavior.How often does it reproduce? Is there a required condition?
Only for the options marked as "unsupported" in workers.
What is the expected behavior?
I see four possible solutions:
execArgv
).execArgv
of the parent thread.execArgv: process.execArgv
becomes the default behavior), and provide aWorker.filterExecArgv
utility that removes disallowed options, and that I would have used likeIf the current behavior is expected, then the docs should be updated to mention that the default value cannot be replicated by explicitly passing an
execArgv
option.What do you see instead?
No response
Additional information
No response