mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 01:31:27 +00:00

Instead of sharing the OS-backed store for all `process.env` instances, create a copy of `process.env` for every worker that is created. The copies do not interact. Native-addons do not see modifications to `process.env` from Worker threads, but child processes started from Workers do default to the Worker’s copy of `process.env`. This makes Workers behave like child processes as far as `process.env` is concerned, and an option corresponding to the `child_process` module’s `env` option is added to the constructor. Fixes: https://github.com/nodejs/node/issues/24947 PR-URL: https://github.com/nodejs/node/pull/26544 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
33 lines
1003 B
JavaScript
33 lines
1003 B
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { Worker, parentPort, SHARE_ENV, workerData } = require('worker_threads');
|
|
|
|
if (!workerData) {
|
|
process.env.SET_IN_PARENT = 'set';
|
|
assert.strictEqual(process.env.SET_IN_PARENT, 'set');
|
|
|
|
const w = new Worker(__filename, {
|
|
workerData: 'runInWorker',
|
|
env: SHARE_ENV
|
|
}).on('exit', common.mustCall(() => {
|
|
// Env vars from the child thread are not set globally.
|
|
assert.strictEqual(process.env.SET_IN_WORKER, 'set');
|
|
}));
|
|
|
|
process.env.SET_IN_PARENT_AFTER_CREATION = 'set';
|
|
w.postMessage({});
|
|
} else {
|
|
assert.strictEqual(workerData, 'runInWorker');
|
|
|
|
// Env vars from the parent thread are inherited.
|
|
assert.strictEqual(process.env.SET_IN_PARENT, 'set');
|
|
|
|
process.env.SET_IN_WORKER = 'set';
|
|
assert.strictEqual(process.env.SET_IN_WORKER, 'set');
|
|
|
|
parentPort.once('message', common.mustCall(() => {
|
|
assert.strictEqual(process.env.SET_IN_PARENT_AFTER_CREATION, 'set');
|
|
}));
|
|
}
|