diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-22 20:11:19 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-03-30 22:25:35 +0100 |
commit | 9fbf0c60b583dae3d34598352c3c7614118cd035 (patch) | |
tree | d615114117264cce1c469f2ff80088dfddf6149b /src/node_worker.cc | |
parent | 1ee37aac09f263b00029561542cf3bea5db5113b (diff) | |
download | android-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 'src/node_worker.cc')
-rw-r--r-- | src/node_worker.cc | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/node_worker.cc b/src/node_worker.cc index d18d8023a2..8fabbffba4 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -68,7 +68,8 @@ Worker::Worker(Environment* env, exec_argv_(exec_argv), platform_(env->isolate_data()->platform()), profiler_idle_notifier_started_(env->profiler_idle_notifier_started()), - thread_id_(Environment::AllocateThreadId()) { + thread_id_(Environment::AllocateThreadId()), + env_vars_(env->env_vars()) { Debug(this, "Creating new worker instance with thread id %llu", thread_id_); // Set up everything that needs to be set up in the parent environment. @@ -254,6 +255,7 @@ void Worker::Run() { Environment::kNoFlags, thread_id_)); CHECK_NOT_NULL(env_); + env_->set_env_vars(std::move(env_vars_)); env_->set_abort_on_uncaught_exception(false); env_->set_worker_context(this); @@ -469,6 +471,25 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { new Worker(env, args.This(), url, per_isolate_opts, std::move(exec_argv_out)); } +void Worker::CloneParentEnvVars(const FunctionCallbackInfo<Value>& args) { + Worker* w; + ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); + CHECK(w->thread_joined_); // The Worker has not started yet. + + w->env_vars_ = w->env()->env_vars()->Clone(args.GetIsolate()); +} + +void Worker::SetEnvVars(const FunctionCallbackInfo<Value>& args) { + Worker* w; + ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); + CHECK(w->thread_joined_); // The Worker has not started yet. + + CHECK(args[0]->IsObject()); + w->env_vars_ = KVStore::CreateMapKVStore(); + w->env_vars_->AssignFromObject(args.GetIsolate()->GetCurrentContext(), + args[0].As<Object>()); +} + void Worker::StartThread(const FunctionCallbackInfo<Value>& args) { Worker* w; ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); @@ -566,6 +587,8 @@ void InitWorker(Local<Object> target, w->InstanceTemplate()->SetInternalFieldCount(1); w->Inherit(AsyncWrap::GetConstructorTemplate(env)); + env->SetProtoMethod(w, "setEnvVars", Worker::SetEnvVars); + env->SetProtoMethod(w, "cloneParentEnvVars", Worker::CloneParentEnvVars); env->SetProtoMethod(w, "startThread", Worker::StartThread); env->SetProtoMethod(w, "stopThread", Worker::StopThread); env->SetProtoMethod(w, "ref", Worker::Ref); |