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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jun 26, 2024

When the worker spawning code copies NODE_OPTIONS from process.env, previously we do a check again on the copied NODE_OPTIONS and throw if it contains per-process settings. This can be problematic if the end user starts the process with a NODE_OPTIONS and the worker is spawned by a third-party that tries to extend process.env with some overrides before passing them into the worker. This patch adds another exception that skips the validation if the NODE_OPTIONS in the env setting is character-by-character equal to the parent NODE_OPTIONS. While some more intelligent filter can be useful too, this works good enough for the inheritance case, when the worker spawning code does not intend to modify NODE_OPTIONS.

Refs: #41103

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support. labels Jun 26, 2024
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 26, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 26, 2024
@nodejs-github-bot
Copy link
Collaborator

USE(env->env_vars()->Get("NODE_OPTIONS").To(&parent_node_options));
if (args[1]->IsObject() &&
env_vars->Get("NODE_OPTIONS").To(&node_options) &&
node_options != parent_node_options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worker will ignore valid per-env options in NODE_OPTIONS when the process was started with a same NODE_OPTIONS.

#!/usr/bin/env NODE_OPTIONS=--trace-exit ./out/Debug/node

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

if (isMainThread) {
  new Worker(__filename, {
    env: {
      NODE_OPTIONS: '--trace-exit'
    }
  })
} else {
  setTimeout(() => {
    process.exit(1)
  }, 1000)
}

Copy link
Member Author

@joyeecheung joyeecheung Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, it still needs to be parsed to mutate the per_isolate_opts. Maybe we should just check in the error branch though that do require a pretty dumb error.ends_with("is not allowed in NODE_OPTIONS") check for all the errors..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..or probably just switch to kDisallowedInEnvvar if the NODE_OPTIONS are character-by-character equal with the parent one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to just wrap it around a throw-away PerProcessOption and added a test.

@joyeecheung joyeecheung force-pushed the worker-option branch 2 times, most recently from fd49c14 to d60bc23 Compare June 27, 2024 21:28
When the worker spawning code copies NODE_OPTIONS from process.env,
previously we do a check again on the copied NODE_OPTIONS and throw
if it contains per-process settings. This can be problematic
if the end user starts the process with a NODE_OPTIONS and the
worker is spawned by a third-party that tries to extend process.env
with some overrides before passing them into the worker. This patch
adds another exception that allows the per-process options in the
NODE_OPTIONS passed to a worker if the NODE_OPTIONS is
character-by-character equal to the parent NODE_OPTIONS.
While some more intelligent filter can be useful too,
this works good enough for the inheritance case, when the worker
spawning code does not intend to modify NODE_OPTIONS.
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 27, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 27, 2024
@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants