worker: allow copied NODE_OPTIONS in the env setting · nodejs/node@d6d0427 · GitHub
Skip to content

Commit d6d0427

Browse files
joyeecheungmarco-ippolito
authored andcommitted
worker: allow copied NODE_OPTIONS in the env setting
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. PR-URL: #53596 Refs: #41103 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 60ee41d commit d6d0427

4 files changed

Lines changed: 106 additions & 6 deletions

File tree

src/node_worker.cc

Lines changed: 34 additions & 6 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
// This test is meant to be spawned with NODE_OPTIONS=--title=foo
4+
const assert = require('assert');
5+
if (process.platform !== 'sunos') { // --title is unsupported on SmartOS.
6+
assert.strictEqual(process.title, 'foo');
7+
}
8+
9+
// Spawns a worker that may copy NODE_OPTIONS if it's set by the parent.
10+
const { Worker } = require('worker_threads');
11+
new Worker(`require('assert').strictEqual(process.env.TEST_VAR, 'bar')`, {
12+
env: {
13+
...process.env,
14+
TEST_VAR: 'bar',
15+
},
16+
eval: true,
17+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const { Worker, isMainThread } = require('worker_threads')
4+
5+
// Tests that valid per-isolate/env NODE_OPTIONS are allowed and
6+
// work in child workers.
7+
if (isMainThread) {
8+
new Worker(__filename, {
9+
env: {
10+
...process.env,
11+
NODE_OPTIONS: '--trace-exit'
12+
}
13+
})
14+
} else {
15+
setImmediate(() => {
16+
process.nextTick(() => {
17+
process.exit(0);
18+
});
19+
});
20+
}
Lines changed: 35 additions & 0 deletions

0 commit comments

Comments
 (0)