-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
d7d9278
commit d60bc23
Showing
4 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
// This test is meant to be spawned with NODE_OPTIONS=--title=foo | ||
const assert = require('assert'); | ||
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
} | ||
); |