summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-02-22 20:11:19 +0100
committerAnna Henningsen <anna@addaleax.net>2019-03-30 22:25:35 +0100
commit9fbf0c60b583dae3d34598352c3c7614118cd035 (patch)
treed615114117264cce1c469f2ff80088dfddf6149b /doc
parent1ee37aac09f263b00029561542cf3bea5db5113b (diff)
downloadandroid-node-v8-9fbf0c60b583dae3d34598352c3c7614118cd035.tar.gz
android-node-v8-9fbf0c60b583dae3d34598352c3c7614118cd035.tar.bz2
android-node-v8-9fbf0c60b583dae3d34598352c3c7614118cd035.zip
worker: use copy of process.env
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>
Diffstat (limited to 'doc')
-rw-r--r--doc/api/process.md17
-rw-r--r--doc/api/worker_threads.md51
2 files changed, 54 insertions, 14 deletions
diff --git a/doc/api/process.md b/doc/api/process.md
index fa41109662..3b428fb038 100644
--- a/doc/api/process.md
+++ b/doc/api/process.md
@@ -954,6 +954,11 @@ emitMyWarning();
<!-- YAML
added: v0.1.27
changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/26544
+ description: Worker threads will now use a copy of the parent thread’s
+ `process.env` by default, configurable through the `env`
+ option of the `Worker` constructor.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18990
description: Implicit conversion of variable value to string is deprecated.
@@ -983,8 +988,9 @@ An example of this object looks like:
```
It is possible to modify this object, but such modifications will not be
-reflected outside the Node.js process. In other words, the following example
-would not work:
+reflected outside the Node.js process, or (unless explicitly requested)
+to other [`Worker`][] threads.
+In other words, the following example would not work:
```console
$ node -e 'process.env.foo = "bar"' && echo $foo
@@ -1027,7 +1033,12 @@ console.log(process.env.test);
// => 1
```
-`process.env` is read-only in [`Worker`][] threads.
+Unless explicitly specified when creating a [`Worker`][] instance,
+each [`Worker`][] thread has its own copy of `process.env`, based on its
+parent thread’s `process.env`, or whatever was specified as the `env` option
+to the [`Worker`][] constructor. Changes to `process.env` will not be visible
+across [`Worker`][] threads, and only the main thread can make changes that
+are visible to the operating system or to native add-ons.
## process.execArgv
<!-- YAML
diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md
index e19181760e..5d81f3f7f5 100644
--- a/doc/api/worker_threads.md
+++ b/doc/api/worker_threads.md
@@ -125,6 +125,25 @@ if (isMainThread) {
}
```
+## worker.SHARE_ENV
+<!-- YAML
+added: REPLACEME
+-->
+
+* {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
<!-- YAML
added: v10.5.0
@@ -380,7 +399,11 @@ Notable differences inside a Worker environment are:
and [`process.abort()`][] is not available.
- [`process.chdir()`][] and `process` methods that set group or user ids
are not available.
-- [`process.env`][] is a read-only reference to the environment variables.
+- [`process.env`][] is a copy of the parent thread's environment variables,
+ unless otherwise specified. Changes to one copy will not be visible in other
+ threads, and will not be visible to native add-ons (unless
+ [`worker.SHARE_ENV`][] has been passed as the `env` option to the
+ [`Worker`][] constructor).
- [`process.title`][] cannot be modified.
- Signals will not be delivered through [`process.on('...')`][Signals events].
- Execution may stop at any point as a result of [`worker.terminate()`][]
@@ -439,13 +462,18 @@ if (isMainThread) {
If `options.eval` is `true`, this is a string containing JavaScript code
rather than a path.
* `options` {Object}
+ * `env` {Object} If set, specifies the initial value of `process.env` inside
+ the Worker thread. As a special value, [`worker.SHARE_ENV`][] may be used
+ to specify that the parent thread and the child thread should share their
+ environment variables; in that case, changes to one thread’s `process.env`
+ object will affect the other thread as well. **Default:** `process.env`.
* `eval` {boolean} If `true`, interpret the first argument to the constructor
as a script that is executed once the worker is online.
- * `workerData` {any} Any JavaScript value that will be cloned and made
- available as [`require('worker_threads').workerData`][]. The cloning will
- occur as described in the [HTML structured clone algorithm][], and an error
- will be thrown if the object cannot be cloned (e.g. because it contains
- `function`s).
+ * `execArgv` {string[]} List of node CLI options passed to the worker.
+ V8 options (such as `--max-old-space-size`) and options that affect the
+ process (such as `--title`) are not supported. If set, this will be provided
+ as [`process.execArgv`][] inside the worker. By default, options will be
+ inherited from the parent thread.
* `stdin` {boolean} If this is set to `true`, then `worker.stdin` will
provide a writable stream whose contents will appear as `process.stdin`
inside the Worker. By default, no data is provided.
@@ -453,11 +481,11 @@ if (isMainThread) {
not automatically be piped through to `process.stdout` in the parent.
* `stderr` {boolean} If this is set to `true`, then `worker.stderr` will
not automatically be piped through to `process.stderr` in the parent.
- * `execArgv` {string[]} List of node CLI options passed to the worker.
- V8 options (such as `--max-old-space-size`) and options that affect the
- process (such as `--title`) are not supported. If set, this will be provided
- as [`process.execArgv`][] inside the worker. By default, options will be
- inherited from the parent thread.
+ * `workerData` {any} Any JavaScript value that will be cloned and made
+ available as [`require('worker_threads').workerData`][]. The cloning will
+ occur as described in the [HTML structured clone algorithm][], and an error
+ will be thrown if the object cannot be cloned (e.g. because it contains
+ `function`s).
### Event: 'error'
<!-- YAML
@@ -628,6 +656,7 @@ active handle in the event system. If the worker is already `unref()`ed calling
[`vm`]: vm.html
[`worker.on('message')`]: #worker_threads_event_message_1
[`worker.postMessage()`]: #worker_threads_worker_postmessage_value_transferlist
+[`worker.SHARE_ENV`]: #worker_threads_worker_share_env
[`worker.terminate()`]: #worker_threads_worker_terminate_callback
[`worker.threadId`]: #worker_threads_worker_threadid_1
[Addons worker support]: addons.html#addons_worker_support