Skip to content
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: allow copied NODE_OPTIONS in the env setting #53596

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,40 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
// [0] is expected to be the program name, add dummy string.
env_argv.insert(env_argv.begin(), "");
std::vector<std::string> invalid_args{};
options_parser::Parse(&env_argv,
nullptr,
&invalid_args,
per_isolate_opts.get(),
kAllowedInEnvvar,
&errors);

std::string parent_node_options;
USE(env->env_vars()->Get("NODE_OPTIONS").To(&parent_node_options));

// If the worker code passes { env: { ...process.env, ... } } or
// the NODE_OPTIONS is otherwise character-for-character equal to the
// original NODE_OPTIONS, allow per-process options inherited into
// the worker since worker spawning code is not usually in charge of
// how the NODE_OPTIONS is configured for the parent.
// TODO(joyeecheung): a more intelligent filter may be more desirable.
// but a string comparison is good enough(TM) for the case where the
// worker spawning code just wants to pass the parent configuration down
// and does not intend to modify NODE_OPTIONS.
if (parent_node_options == node_options) {
// Creates a wrapper per-process option over the per_isolate_opts
// to allow per-process options copied from the parent.
std::unique_ptr<PerProcessOptions> per_process_opts =
std::make_unique<PerProcessOptions>();
per_process_opts->per_isolate = per_isolate_opts;
options_parser::Parse(&env_argv,
nullptr,
&invalid_args,
per_process_opts.get(),
kAllowedInEnvvar,
&errors);
} else {
options_parser::Parse(&env_argv,
nullptr,
&invalid_args,
per_isolate_opts.get(),
kAllowedInEnvvar,
&errors);
}

if (!errors.empty() && args[1]->IsObject()) {
// Only fail for explicitly provided env, this protects from failures
// when NODE_OPTIONS from parent's env is used (which is the default).
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/spawn-worker-with-copied-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

// This test is meant to be spawned with NODE_OPTIONS=--title=foo
const assert = require('assert');
if (process.platform !== 'sunos') { // --title is unsupported on SmartOS.
assert.strictEqual(process.title, 'foo');
}

// Spawns a worker that may copy NODE_OPTIONS if it's set by the parent.
const { Worker } = require('worker_threads');
new Worker(`require('assert').strictEqual(process.env.TEST_VAR, 'bar')`, {
env: {
...process.env,
TEST_VAR: 'bar',
},
eval: true,
});
20 changes: 20 additions & 0 deletions test/fixtures/spawn-worker-with-trace-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const { Worker, isMainThread } = require('worker_threads')

// Tests that valid per-isolate/env NODE_OPTIONS are allowed and
// work in child workers.
if (isMainThread) {
new Worker(__filename, {
env: {
...process.env,
NODE_OPTIONS: '--trace-exit'
}
})
} else {
setImmediate(() => {
process.nextTick(() => {
process.exit(0);
});
});
}
35 changes: 35 additions & 0 deletions test/parallel/test-worker-node-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

require('../common');
const {
spawnSyncAndExitWithoutError,
spawnSyncAndAssert,
} = require('../common/child_process');
const fixtures = require('../common/fixtures');
spawnSyncAndExitWithoutError(
process.execPath,
[
fixtures.path('spawn-worker-with-copied-env'),
],
{
env: {
...process.env,
NODE_OPTIONS: '--title=foo'
}
}
);

spawnSyncAndAssert(
process.execPath,
[
fixtures.path('spawn-worker-with-trace-exit'),
],
{
env: {
...process.env,
}
},
{
stderr: /spawn-worker-with-trace-exit\.js:17/
}
);
Loading