From 8bd7909d0089b42f513f4cabd67430d35a467a0a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 22 Feb 2019 20:11:19 +0100 Subject: [PATCH] worker: use copy of process.env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Vse Mozhet Byt Reviewed-By: Yongsheng Zhang Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Joyee Cheung Signed-off-by: Beth Griggs --- doc/api/process.md | 17 ++++-- doc/api/worker_threads.md | 51 ++++++++++++++---- lib/internal/worker.js | 24 +++++++++ lib/worker_threads.js | 2 + src/env-inl.h | 8 +-- src/env.cc | 2 +- src/env.h | 10 ++-- src/node_credentials.cc | 2 +- src/node_env_var.cc | 49 +++++++++-------- src/node_worker.cc | 25 ++++++++- src/node_worker.h | 4 ++ .../test-worker-process-env-shared.js | 32 +++++++++++ test/parallel/test-worker-process-env.js | 54 +++++++++++++++++++ 13 files changed, 229 insertions(+), 51 deletions(-) create mode 100644 test/parallel/test-worker-process-env-shared.js create mode 100644 test/parallel/test-worker-process-env.js diff --git a/doc/api/process.md b/doc/api/process.md index 7c29ceefbcbe73..5f866e3139aa4d 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -954,6 +954,11 @@ emitMyWarning(); + +* {symbol} + +A special value that can be passed as the `env` option of the [`Worker`][] +constructor, to indicate that the current thread and the Worker thread should +share read and write access to the same set of environment variables. + +```js +const { Worker, SHARE_ENV } = require('worker_threads'); +new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV }) + .on('exit', () => { + console.log(process.env.SET_IN_WORKER); // Prints 'foo'. + }); +``` + ## worker.threadId