Skip to content

Commit bd83a5c

Browse files
authored
child_process: build the default env block in one native pass
When spawn()/spawnSync() are called without options.env, normalizeSpawnArguments() copied process.env with a spread and then walked the copy to build the KEY=value array uv_spawn() takes. Spreading the process.env proxy costs one enumerator callback plus a query and a getter interceptor per variable, each doing a linear getenv() scan and allocating; with a couple of hundred variables that was the single largest JS-side cost of spawning a process. Add KVStore::Pairs() (Enumerate() + Get() by default, one uv_os_environ() pass for the real environment, skipping hidden variables on Windows exactly like Enumerate() does), expose it as process_wrap.getEnvPairs(), and use it for the default-environment case. A user supplied options.env and the permission model case keep the existing code. On Windows the same sort/first-wins-case-insensitive filter is applied to the pairs. The variables copyProcessEnvToEnv() propagates are part of the real environment by definition, and its entries cannot contain null bytes, so those steps only remain on the options.env path. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65325 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d782ae6 commit bd83a5c

5 files changed

Lines changed: 248 additions & 50 deletions

File tree

‎lib/child_process.js‎

Lines changed: 95 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
RegExpPrototypeExec,
4141
SafeSet,
4242
StringPrototypeIncludes,
43+
StringPrototypeIndexOf,
4344
StringPrototypeSlice,
4445
StringPrototypeToUpperCase,
4546
SymbolDispose,
@@ -93,6 +94,8 @@ const {
9394
stdioStringToArray,
9495
} = child_process;
9596

97+
const { getEnvPairs } = internalBinding('process_wrap');
98+
9699
const MAX_BUFFER = 1024 * 1024;
97100

98101
const permission = require('internal/process/permission');
@@ -709,60 +712,74 @@ function normalizeSpawnArguments(file, args, options) {
709712
ArrayPrototypeUnshift(args, file);
710713
}
711714

712-
// Shallow copy to guarantee changes won't impact process.env
713-
const env = options.env || { ...process.env };
714-
const envPairs = [];
715-
716-
// process.env.NODE_V8_COVERAGE always propagates, making it possible to
717-
// collect coverage for programs that spawn with white-listed environment.
718-
copyProcessEnvToEnv(env, 'NODE_V8_COVERAGE', options.env);
719-
720-
if (isZOS) {
721-
// The following environment variables must always propagate if set.
722-
copyProcessEnvToEnv(env, '_BPXK_AUTOCVT', options.env);
723-
copyProcessEnvToEnv(env, '_CEE_RUNOPTS', options.env);
724-
copyProcessEnvToEnv(env, '_TAG_REDIR_ERR', options.env);
725-
copyProcessEnvToEnv(env, '_TAG_REDIR_IN', options.env);
726-
copyProcessEnvToEnv(env, '_TAG_REDIR_OUT', options.env);
727-
copyProcessEnvToEnv(env, 'STEPLIB', options.env);
728-
copyProcessEnvToEnv(env, 'LIBPATH', options.env);
729-
copyProcessEnvToEnv(env, '_EDC_SIG_DFLT', options.env);
730-
copyProcessEnvToEnv(env, '_EDC_SUSV3', options.env);
731-
}
715+
let envPairs;
716+
if (!options.env && !permission.isEnabled()) {
717+
// Default: the child inherits this process's environment. Take it as
718+
// 'KEY=value' strings in one native pass over the environment block
719+
// instead of copying process.env (one interceptor round trip and one
720+
// getenv() scan per variable). Everything copyProcessEnvToEnv() would
721+
// propagate below is part of it by definition, and entries of the real
722+
// environment block cannot contain null bytes.
723+
envPairs = getEnvPairs();
724+
if (process.platform === 'win32') {
725+
envPairs = dedupeWindowsEnvPairs(envPairs);
726+
}
727+
} else {
728+
// Shallow copy to guarantee changes won't impact process.env
729+
const env = options.env || { ...process.env };
730+
envPairs = [];
731+
732+
// process.env.NODE_V8_COVERAGE always propagates, making it possible to
733+
// collect coverage for programs that spawn with white-listed environment.
734+
copyProcessEnvToEnv(env, 'NODE_V8_COVERAGE', options.env);
735+
736+
if (isZOS) {
737+
// The following environment variables must always propagate if set.
738+
copyProcessEnvToEnv(env, '_BPXK_AUTOCVT', options.env);
739+
copyProcessEnvToEnv(env, '_CEE_RUNOPTS', options.env);
740+
copyProcessEnvToEnv(env, '_TAG_REDIR_ERR', options.env);
741+
copyProcessEnvToEnv(env, '_TAG_REDIR_IN', options.env);
742+
copyProcessEnvToEnv(env, '_TAG_REDIR_OUT', options.env);
743+
copyProcessEnvToEnv(env, 'STEPLIB', options.env);
744+
copyProcessEnvToEnv(env, 'LIBPATH', options.env);
745+
copyProcessEnvToEnv(env, '_EDC_SIG_DFLT', options.env);
746+
copyProcessEnvToEnv(env, '_EDC_SUSV3', options.env);
747+
}
732748

733-
if (permission.isEnabled()) {
734-
copyPermissionModelFlagsToEnv(env, 'NODE_OPTIONS', args);
735-
}
749+
if (permission.isEnabled()) {
750+
copyPermissionModelFlagsToEnv(env, 'NODE_OPTIONS', args);
751+
}
736752

737-
let envKeys = [];
738-
// Prototype values are intentionally included.
739-
for (const key in env) {
740-
ArrayPrototypePush(envKeys, key);
741-
}
753+
let envKeys = [];
754+
// Prototype values are intentionally included.
755+
for (const key in env) {
756+
ArrayPrototypePush(envKeys, key);
757+
}
742758

743-
if (process.platform === 'win32') {
744-
// On Windows env keys are case insensitive. Filter out duplicates,
745-
// keeping only the first one (in lexicographic order)
746-
const sawKey = new SafeSet();
747-
envKeys = ArrayPrototypeFilter(
748-
ArrayPrototypeSort(envKeys),
749-
(key) => {
750-
const uppercaseKey = StringPrototypeToUpperCase(key);
751-
if (sawKey.has(uppercaseKey)) {
752-
return false;
753-
}
754-
sawKey.add(uppercaseKey);
755-
return true;
756-
},
757-
);
758-
}
759+
if (process.platform === 'win32') {
760+
// On Windows env keys are case insensitive. Filter out duplicates,
761+
// keeping only the first one (in lexicographic order)
762+
const sawKey = new SafeSet();
763+
envKeys = ArrayPrototypeFilter(
764+
ArrayPrototypeSort(envKeys),
765+
(key) => {
766+
const uppercaseKey = StringPrototypeToUpperCase(key);
767+
if (sawKey.has(uppercaseKey)) {
768+
return false;
769+
}
770+
sawKey.add(uppercaseKey);
771+
return true;
772+
},
773+
);
774+
}
759775

760-
for (const key of envKeys) {
761-
const value = env[key];
762-
if (value !== undefined) {
763-
validateArgumentNullCheck(key, `options.env['${key}']`);
764-
validateArgumentNullCheck(value, `options.env['${key}']`);
765-
ArrayPrototypePush(envPairs, `${key}=${value}`);
776+
for (const key of envKeys) {
777+
const value = env[key];
778+
if (value !== undefined) {
779+
validateArgumentNullCheck(key, `options.env['${key}']`);
780+
validateArgumentNullCheck(value, `options.env['${key}']`);
781+
ArrayPrototypePush(envPairs, `${key}=${value}`);
782+
}
766783
}
767784
}
768785

@@ -780,6 +797,34 @@ function normalizeSpawnArguments(file, args, options) {
780797
};
781798
}
782799

800+
/**
801+
* Windows environment variable names are case-insensitive: keep only the
802+
* first entry for each name in lexicographic order of the names, exactly like
803+
* the key filtering applied to a user-supplied `options.env`.
804+
* @param {string[]} envPairs 'KEY=value' strings
805+
* @returns {string[]}
806+
*/
807+
function dedupeWindowsEnvPairs(envPairs) {
808+
const keyed = [];
809+
for (let i = 0; i < envPairs.length; i++) {
810+
const pair = envPairs[i];
811+
// Names never start with '=' here (hidden variables are not enumerated),
812+
// so the first '=' ends the name.
813+
const eq = StringPrototypeIndexOf(pair, '=');
814+
ArrayPrototypePush(keyed, { key: eq === -1 ? pair : StringPrototypeSlice(pair, 0, eq), pair });
815+
}
816+
ArrayPrototypeSort(keyed, (a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
817+
const sawKey = new SafeSet();
818+
const result = [];
819+
for (let i = 0; i < keyed.length; i++) {
820+
const uppercaseKey = StringPrototypeToUpperCase(keyed[i].key);
821+
if (sawKey.has(uppercaseKey)) continue;
822+
sawKey.add(uppercaseKey);
823+
ArrayPrototypePush(result, keyed[i].pair);
824+
}
825+
return result;
826+
}
827+
783828
function abortChildProcess(child, killSignal, reason) {
784829
if (!child)
785830
return;

‎src/node_env_var.cc‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ using v8::Boolean;
1717
using v8::Context;
1818
using v8::DontDelete;
1919
using v8::DontEnum;
20+
using v8::EscapableHandleScope;
2021
using v8::FunctionTemplate;
2122
using v8::HandleScope;
2223
using v8::IndexedPropertyHandlerConfiguration;
@@ -49,6 +50,7 @@ class RealEnvStore final : public KVStore {
4950
int32_t Query(const char* key) const override;
5051
void Delete(Isolate* isolate, Local<String> key) override;
5152
MaybeLocal<Array> Enumerate(Isolate* isolate) const override;
53+
MaybeLocal<Array> Pairs(Isolate* isolate) const override;
5254
};
5355

5456
class MapKVStore final : public KVStore {
@@ -224,6 +226,64 @@ MaybeLocal<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
224226
return env_v.ToArray();
225227
}
226228

229+
MaybeLocal<Array> RealEnvStore::Pairs(Isolate* isolate) const {
230+
Mutex::ScopedLock lock(per_process::env_var_mutex);
231+
uv_env_item_t* items;
232+
int count;
233+
234+
auto cleanup = OnScopeLeave([&]() { uv_os_free_environ(items, count); });
235+
CHECK_EQ(uv_os_environ(&items, &count), 0);
236+
237+
MaybeStackBuffer<Local<Value>, 256> pairs_v(count);
238+
int pairs_v_index = 0;
239+
std::string pair;
240+
for (int i = 0; i < count; i++) {
241+
#ifdef _WIN32
242+
// If the key starts with '=' it is a hidden environment variable.
243+
// Enumerate() skips these, so a copy of process.env never had them.
244+
if (items[i].name[0] == '=') continue;
245+
#endif
246+
pair.assign(items[i].name);
247+
pair += '=';
248+
pair += items[i].value;
249+
Local<Value> str;
250+
if (!ToV8Value(isolate->GetCurrentContext(), pair, isolate).ToLocal(&str)) {
251+
return {};
252+
}
253+
pairs_v[pairs_v_index++] = str;
254+
}
255+
256+
return Array::New(isolate, pairs_v.out(), pairs_v_index);
257+
}
258+
259+
MaybeLocal<Array> KVStore::Pairs(Isolate* isolate) const {
260+
EscapableHandleScope scope(isolate);
261+
Local<Context> context = isolate->GetCurrentContext();
262+
Local<Array> keys;
263+
if (!Enumerate(isolate).ToLocal(&keys)) return {};
264+
uint32_t keys_length = keys->Length();
265+
MaybeStackBuffer<Local<Value>, 256> pairs(keys_length);
266+
uint32_t pairs_index = 0;
267+
std::string pair;
268+
for (uint32_t i = 0; i < keys_length; i++) {
269+
Local<Value> key;
270+
if (!keys->Get(context, i).ToLocal(&key)) return {};
271+
if (!key->IsString()) continue;
272+
Utf8Value key_utf8(isolate, key);
273+
// A key that disappeared between Enumerate() and Get() is skipped, like an
274+
// undefined value is when copying process.env in JS.
275+
std::optional<std::string> value = Get(*key_utf8);
276+
if (!value.has_value()) continue;
277+
pair.assign(*key_utf8, key_utf8.length());
278+
pair += '=';
279+
pair += *value;
280+
Local<Value> str;
281+
if (!ToV8Value(context, pair, isolate).ToLocal(&str)) return {};
282+
pairs[pairs_index++] = str;
283+
}
284+
return scope.Escape(Array::New(isolate, pairs.out(), pairs_index));
285+
}
286+
227287
std::shared_ptr<KVStore> KVStore::Clone(Isolate* isolate) const {
228288
HandleScope handle_scope(isolate);
229289
Local<Context> context = isolate->GetCurrentContext();

‎src/process_wrap.cc‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class ProcessWrap : public HandleWrap {
7979
SetProtoMethod(isolate, constructor, "kill", Kill);
8080

8181
SetConstructorFunction(context, target, "Process", constructor);
82+
SetMethodNoSideEffect(context, target, "getEnvPairs", GetEnvPairs);
8283

8384
Local<Object> constants = Object::New(isolate);
8485
NODE_DEFINE_CONSTANT(constants, kProcessFlagDetached);
@@ -91,6 +92,7 @@ class ProcessWrap : public HandleWrap {
9192
registry->Register(New);
9293
registry->Register(Spawn);
9394
registry->Register(Kill);
95+
registry->Register(GetEnvPairs);
9496
}
9597

9698
SET_NO_MEMORY_INFO()
@@ -342,6 +344,17 @@ class ProcessWrap : public HandleWrap {
342344
args.GetReturnValue().Set(err);
343345
}
344346

347+
// The current environment as ["KEY=value", ...], i.e. what a spawned
348+
// child inherits by default, produced in one pass over the environment
349+
// block instead of one interceptor round trip per variable.
350+
static void GetEnvPairs(const FunctionCallbackInfo<Value>& args) {
351+
Environment* env = Environment::GetCurrent(args);
352+
Local<Array> pairs;
353+
if (env->env_vars()->Pairs(env->isolate()).ToLocal(&pairs)) {
354+
args.GetReturnValue().Set(pairs);
355+
}
356+
}
357+
345358
static void Kill(const FunctionCallbackInfo<Value>& args) {
346359
Environment* env = Environment::GetCurrent(args);
347360
ProcessWrap* wrap;

‎src/util.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ class KVStore {
323323
virtual int32_t Query(const char* key) const = 0;
324324
virtual void Delete(v8::Isolate* isolate, v8::Local<v8::String> key) = 0;
325325
virtual v8::MaybeLocal<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;
326+
// All entries as an array of "KEY=value" strings, in enumeration order —
327+
// the form uv_spawn() consumes. The default implementation is
328+
// Enumerate() + Get(); stores that can produce it in one pass override it.
329+
virtual v8::MaybeLocal<v8::Array> Pairs(v8::Isolate* isolate) const;
326330

327331
virtual std::shared_ptr<KVStore> Clone(v8::Isolate* isolate) const;
328332
virtual v8::Maybe<void> AssignFromObject(v8::Local<v8::Context> context,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
// When no `env` option is given, a child process must inherit exactly the
3+
// parent's current environment: same variables and values, reflecting runtime
4+
// additions/deletions made through process.env.
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const { spawn, spawnSync, execFileSync } = require('child_process');
8+
9+
// Mutate the environment at runtime in a few ways first.
10+
process.env.TEST_DEFAULT_ENV_ADDED = 'added ünïcödé ✓';
11+
process.env.TEST_DEFAULT_ENV_EMPTY = '';
12+
process.env.TEST_DEFAULT_ENV_EQUALS = 'a=b=c';
13+
process.env.TEST_DEFAULT_ENV_DELETED = 'x';
14+
delete process.env.TEST_DEFAULT_ENV_DELETED;
15+
16+
function expectedEnv() {
17+
// What `{ ...process.env }` yields, minus keys whose value is undefined
18+
// (there are none for the real environment, but keep the definition exact).
19+
const copy = { ...process.env };
20+
for (const key of Object.keys(copy)) {
21+
if (copy[key] === undefined) delete copy[key];
22+
}
23+
return copy;
24+
}
25+
26+
const printEnv = ['-e', 'process.stdout.write(JSON.stringify(process.env))'];
27+
28+
function run(options) {
29+
const child = spawnSync(process.execPath, printEnv, { encoding: 'utf8', ...options });
30+
assert.strictEqual(child.status, 0, child.stderr || String(child.error));
31+
return child.stdout;
32+
}
33+
34+
function check(output, label, expected = expectedEnv()) {
35+
const childEnv = JSON.parse(output);
36+
assert.deepStrictEqual(childEnv, expected, `${label}: contents`);
37+
assert.strictEqual(childEnv.TEST_DEFAULT_ENV_ADDED, 'added ünïcödé ✓');
38+
assert.strictEqual(childEnv.TEST_DEFAULT_ENV_EMPTY, '');
39+
assert.strictEqual(childEnv.TEST_DEFAULT_ENV_EQUALS, 'a=b=c');
40+
assert.ok(!('TEST_DEFAULT_ENV_DELETED' in childEnv));
41+
}
42+
43+
// spawnSync, options omitted entirely.
44+
check(run(), 'spawnSync no options');
45+
// Explicitly undefined / null env behave like the default.
46+
check(run({ env: undefined }), 'spawnSync env undefined');
47+
check(run({ env: null }), 'spawnSync env null');
48+
// execFileSync goes through the same normalization.
49+
check(execFileSync(process.execPath, printEnv, { encoding: 'utf8' }), 'execFileSync');
50+
// A user-supplied env is still passed through as given (not merged).
51+
{
52+
const env = { ...process.env, ONLY: 'this' };
53+
delete env.TEST_DEFAULT_ENV_ADDED;
54+
const childEnv = JSON.parse(run({ env }));
55+
assert.strictEqual(childEnv.ONLY, 'this');
56+
assert.ok(!('TEST_DEFAULT_ENV_ADDED' in childEnv));
57+
}
58+
// Async spawn (the environment is captured at spawn() time).
59+
{
60+
const expectedAtSpawn = expectedEnv();
61+
const child = spawn(process.execPath, printEnv);
62+
let out = '';
63+
child.stdout.setEncoding('utf8').on('data', (d) => { out += d; });
64+
let err = '';
65+
child.stderr.setEncoding('utf8').on('data', (d) => { err += d; });
66+
child.on('close', common.mustCall((code) => {
67+
assert.strictEqual(code, 0, err);
68+
check(out, 'spawn', expectedAtSpawn);
69+
}));
70+
}
71+
// A variable added after an earlier spawn is seen by a later one (no caching).
72+
process.env.TEST_DEFAULT_ENV_LATE = 'late';
73+
{
74+
const childEnv = JSON.parse(run());
75+
assert.strictEqual(childEnv.TEST_DEFAULT_ENV_LATE, 'late');
76+
}

0 commit comments

Comments
 (0)