nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.41k stars 29.52k forks source link

Stop throwing for unsupported options in `Worker`'s `execArgv` and `env.NODE_OPTIONS`. #41103

Open nicolo-ribaudo opened 2 years ago

nicolo-ribaudo commented 2 years ago

Version

v17.2.0

Platform

No response

Subsystem

worker_threads

What steps will reproduce the bug?

Consider this test.js file:

const { isMainThread, Worker } = require("worker_threads");

if (isMainThread) {
  new Worker(__filename, {
    // execArgv: process.execArgv
  });
} else {
  console.log("Hello from the worker!", process.execArgv);
}

NOTE: --experimental-json-modules and --title are just examples

Run these two commands:

node --experimental-json-modules test.js
node --experimental-json-modules --title foo test.js

They will log

Hello from the worker! [ '--experimental-json-modules' ]
Hello from the worker! [ '--experimental-json-modules', '--title', 'foo' ]

Now, uncomment the execArgv: process.execArgv line and run the two commands again. The first command will still log

Hello from the worker! [ '--experimental-json-modules' ]

however, the second one throws:

Error [ERR_WORKER_INVALID_EXEC_ARGV]: Initiated Worker with invalid execArgv flags: --title
    at new NodeError (node:internal/errors:371:5)
    at new Worker (node:internal/worker:191:13)
    at Object.<anonymous> (/home/nicolo/Documenti/dev/babel/babel/test.js:4:3)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_WORKER_INVALID_EXEC_ARGV'
}

This mans that the default behavior of the Worker execArgv option is not to inherit execArgv 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:

new Worker(workerFile, {
  execArgv: process.execArgv.filter((val, i, argv) => val === "--requrie" || argv[i-1] === "--requrie"),
});

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 because jest-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:

const { isMainThread, Worker } = require("worker_threads");

if (isMainThread) {
  new Worker(__filename);
} else {
  console.log("Hello from the worker!", process.execArgv, globalThis.gc);
}

If you run node --expose_gc test.js, it will log

Hello from the worker! [ '--expose_gc' ] [Function: gc]

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 the process.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:

If 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

targos commented 2 years ago

@nodejs/workers

nicolo-ribaudo commented 2 years ago

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);
}
meixg commented 2 years ago

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 the process.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.

meixg commented 2 years ago

If execArgv is not specified, all parent's args indeed are used:

https://github.com/nodejs/node/blob/4cb2a4786711e304314313aabb24ed2dc46134ef/src/node_worker.cc#L551-L553

https://github.com/nodejs/node/blob/4cb2a4786711e304314313aabb24ed2dc46134ef/src/node_worker.cc#L308-L315

But since these Per-Process options and V8 flags are from parents, I guess it is ok?

https://github.com/nodejs/node/pull/25467

jimmywarting commented 1 year ago

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

shellscape commented 1 year ago

Just ran into this using ava https://github.com/avajs/ava/issues/3207

aaronovz1 commented 1 year ago

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.

bcomnes commented 5 months ago

+1 for Ignore the unsupported options

joyeecheung commented 4 months ago

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.

kirklimbu commented 3 months ago

I came across this error while building angular 18 app. I solved it by disabling SSR "prerender": false, "ssr": false inside project.json file

alirezaraha3 commented 2 months ago

Game[spiking @dance ]()

everett1992 commented 1 month ago

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.