From 5fea320753b2a562c71f25f5bdddd4fc2bf9bc6e Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 26 Sep 2018 15:58:45 -0700 Subject: [PATCH] src: ready background workers before bootstrap Make sure background workers are ready before proceeding with the bootstrap or post-bootstrap execution of any code that may trigger `process.exit()`. Fixes: https://github.com/nodejs/node/issues/23065 PR-URL: https://github.com/nodejs/node/pull/23233 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- src/node_platform.cc | 36 ++++++++++++++++++++++++++++++++++-- src/node_platform.h | 5 +++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/node_platform.cc b/src/node_platform.cc index ab1f9157134ffa..ad0cd0db3ec684 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -16,10 +16,28 @@ using v8::Platform; using v8::Task; using v8::TracingController; +struct PlatformWorkerData { + TaskQueue* task_queue; + Mutex* platform_workers_mutex; + ConditionVariable* platform_workers_ready; + int* pending_platform_workers; + int id; +}; + static void BackgroundRunner(void* data) { + std::unique_ptr + worker_data(static_cast(data)); TRACE_EVENT_METADATA1("__metadata", "thread_name", "name", "BackgroundTaskRunner"); - TaskQueue *background_tasks = static_cast *>(data); + + // Notify the main thread that the platform worker is ready. + { + Mutex::ScopedLock lock(*worker_data->platform_workers_mutex); + (*worker_data->pending_platform_workers)--; + worker_data->platform_workers_ready->Signal(lock); + } + + TaskQueue* background_tasks = worker_data->task_queue; while (std::unique_ptr task = background_tasks->BlockingPop()) { task->Run(); background_tasks->NotifyOfCompletion(); @@ -144,15 +162,29 @@ class BackgroundTaskRunner::DelayedTaskScheduler { }; BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) { + Mutex::ScopedLock lock(platform_workers_mutex_); + pending_platform_workers_ = thread_pool_size; + delayed_task_scheduler_.reset( new DelayedTaskScheduler(&background_tasks_)); threads_.push_back(delayed_task_scheduler_->Start()); + for (int i = 0; i < thread_pool_size; i++) { + PlatformWorkerData* worker_data = new PlatformWorkerData{ + &background_tasks_, &platform_workers_mutex_, + &platform_workers_ready_, &pending_platform_workers_, i + }; std::unique_ptr t { new uv_thread_t() }; - if (uv_thread_create(t.get(), BackgroundRunner, &background_tasks_) != 0) + if (uv_thread_create(t.get(), BackgroundRunner, worker_data) != 0) break; threads_.push_back(std::move(t)); } + + // Wait for platform workers to initialize before continuing with the + // bootstrap. + while (pending_platform_workers_ > 0) { + platform_workers_ready_.Wait(lock); + } } void BackgroundTaskRunner::PostTask(std::unique_ptr task) { diff --git a/src/node_platform.h b/src/node_platform.h index e5c6d7b099acb6..c89c6a5ff6708e 100644 --- a/src/node_platform.h +++ b/src/node_platform.h @@ -110,6 +110,7 @@ class BackgroundTaskRunner : public v8::TaskRunner { void Shutdown(); size_t NumberOfAvailableBackgroundThreads() const; + private: TaskQueue background_tasks_; @@ -117,6 +118,10 @@ class BackgroundTaskRunner : public v8::TaskRunner { std::unique_ptr delayed_task_scheduler_; std::vector> threads_; + + Mutex platform_workers_mutex_; + ConditionVariable platform_workers_ready_; + int pending_platform_workers_; }; class NodePlatform : public MultiIsolatePlatform {