Skip to content

Commit a5961c7

Browse files
committed
src: fix execArgv in worker
1 parent 2693f09 commit a5961c7

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/node_worker.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
590590
exec_argv.push_back(arg_string);
591591
}
592592
} else {
593-
exec_argv_out = env->exec_argv();
594593
exec_argv.insert(
595-
exec_argv.end(), exec_argv_out.begin(), exec_argv_out.end());
594+
exec_argv.end(), env->exec_argv().begin(), env->exec_argv().end());
596595
}
597596

598597
std::vector<std::string> invalid_args{};
@@ -608,7 +607,9 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
608607

609608
// The first argument is program name.
610609
invalid_args.erase(invalid_args.begin());
611-
if (errors.size() > 0 || invalid_args.size() > 0) {
610+
// Only fail for explicitly provided execArgv, this protects from failures
611+
// when execArgv from parent's execArgv is used (which is the default).
612+
if (errors.size() > 0 || (invalid_args.size() > 0 && args[2]->IsArray())) {
612613
Local<Value> error;
613614
if (!ToV8Value(env->context(), errors.size() > 0 ? errors : invalid_args)
614615
.ToLocal(&error)) {
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
// Flags: --expose-internals
1+
// Flags: --expose-internals --expose-gc
22
'use strict';
33
require('../common');
44
const { Worker } = require('worker_threads');
5+
const common = require('../common');
6+
const assert = require('assert');
57

68
const CODE = `
79
// If the --expose-internals flag does not pass to worker
810
// require function will throw an error
911
require('internal/options');
1012
`;
11-
// Test if the flags is passed to worker threads
13+
// Test if the flags is passed to worker threads collectly
14+
// and do not throw an error with the invalid execArgv
15+
// when execArgv is inherited from parent
1216
// See https://github.com/nodejs/node/issues/52825
17+
// See https://github.com/nodejs/node/issues/53011
18+
// Inherited env, execArgv from the parent will be ok
1319
new Worker(CODE, { eval: true });
14-
new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals'] });
20+
// Pass process.env explicitly and inherited execArgv(Node.js options) from parent will be ok
1521
new Worker(CODE, { eval: true, env: process.env });
22+
// Inherited env from the parent and pass execArgv(Node.js options) explicitly will be ok
1623
new Worker(CODE, { eval: true, execArgv: ['--expose-internals'] });
24+
// Pass process.env and execArgv(Node.js options) explicitly will be ok
25+
new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals'] });
26+
// Pass execArgv(V8 options) explicitly will throw an error
27+
assert.throws(() => {
28+
new Worker(CODE, { eval: true, execArgv: ['--expose-gc'] })
29+
}, /ERR_WORKER_INVALID_EXEC_ARGV/);

0 commit comments

Comments
 (0)