From ec14ab5cc6b5b589394dabe51b263fec01dd225c Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sun, 12 May 2024 15:46:28 +0800 Subject: [PATCH] src: make sure pass the `argv` to worker threads PR-URL: https://github.com/nodejs/node/pull/52827 Fixes: https://github.com/nodejs/node/issues/52825 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- src/node_worker.cc | 39 +++++++++++++----------- test/parallel/test-worker-cli-options.js | 16 ++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 test/parallel/test-worker-cli-options.js diff --git a/src/node_worker.cc b/src/node_worker.cc index f744c6a1a97ff1..14fcf72c1ec3e5 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -569,26 +569,30 @@ void Worker::New(const FunctionCallbackInfo& args) { } } #endif // NODE_WITHOUT_NODE_OPTIONS - } - if (args[2]->IsArray()) { - Local array = args[2].As(); // The first argument is reserved for program name, but we don't need it // in workers. std::vector exec_argv = {""}; - uint32_t length = array->Length(); - for (uint32_t i = 0; i < length; i++) { - Local arg; - if (!array->Get(env->context(), i).ToLocal(&arg)) { - return; - } - Local arg_v8; - if (!arg->ToString(env->context()).ToLocal(&arg_v8)) { - return; + if (args[2]->IsArray()) { + Local array = args[2].As(); + uint32_t length = array->Length(); + for (uint32_t i = 0; i < length; i++) { + Local arg; + if (!array->Get(env->context(), i).ToLocal(&arg)) { + return; + } + Local arg_v8; + if (!arg->ToString(env->context()).ToLocal(&arg_v8)) { + return; + } + Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8); + std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length()); + exec_argv.push_back(arg_string); } - Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8); - std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length()); - exec_argv.push_back(arg_string); + } else { + exec_argv_out = env->exec_argv(); + exec_argv.insert( + exec_argv.end(), exec_argv_out.begin(), exec_argv_out.end()); } std::vector invalid_args{}; @@ -606,9 +610,8 @@ void Worker::New(const FunctionCallbackInfo& args) { invalid_args.erase(invalid_args.begin()); if (errors.size() > 0 || invalid_args.size() > 0) { Local error; - if (!ToV8Value(env->context(), - errors.size() > 0 ? errors : invalid_args) - .ToLocal(&error)) { + if (!ToV8Value(env->context(), errors.size() > 0 ? errors : invalid_args) + .ToLocal(&error)) { return; } Local key = diff --git a/test/parallel/test-worker-cli-options.js b/test/parallel/test-worker-cli-options.js new file mode 100644 index 00000000000000..98682243f2d16c --- /dev/null +++ b/test/parallel/test-worker-cli-options.js @@ -0,0 +1,16 @@ +// Flags: --expose-internals +'use strict'; +require('../common'); +const { Worker } = require('worker_threads'); + +const CODE = ` +// If the --expose-internals flag does not pass to worker +// require function will throw an error +require('internal/options'); +`; +// Test if the flags is passed to worker threads +// See https://github.com/nodejs/node/issues/52825 +new Worker(CODE, { eval: true }); +new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals'] }); +new Worker(CODE, { eval: true, env: process.env }); +new Worker(CODE, { eval: true, execArgv: ['--expose-internals'] });