Skip to content

Commit 60f2fa9

Browse files
author
Matt Loring
committed
src: Node implementation of v8::Platform
Node.js currently uses the V8 implementation of the DefaultPlatform which schedules VM tasks on a V8 managed thread pool. Since the Node.js event loop is not aware of these tasks, the Node.js process may exit while there are outstanding VM tasks. This will become problematic once asynchronous wasm compilation lands in V8. This PR introduces a Node.js specific implementation of the v8::Platform on top of libuv so that the event loop is aware of outstanding VM tasks. PR-URL: #14001 Fixes: #3665 Fixes: #8496 Fixes: #12980 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 821d624 commit 60f2fa9

12 files changed

+322
-45
lines changed

node.gyp

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
'src/node_http_parser.cc',
191191
'src/node_main.cc',
192192
'src/node_os.cc',
193+
'src/node_platform.cc',
193194
'src/node_revert.cc',
194195
'src/node_serdes.cc',
195196
'src/node_url.cc',
@@ -238,6 +239,7 @@
238239
'src/node_internals.h',
239240
'src/node_javascript.h',
240241
'src/node_mutex.h',
242+
'src/node_platform.h',
241243
'src/node_root_certs.h',
242244
'src/node_version.h',
243245
'src/node_watchdog.h',
@@ -656,6 +658,8 @@
656658
'defines': [ 'NODE_WANT_INTERNALS=1' ],
657659

658660
'sources': [
661+
'src/node_platform.cc',
662+
'src/node_platform.h',
659663
'test/cctest/test_base64.cc',
660664
'test/cctest/test_environment.cc',
661665
'test/cctest/test_util.cc',

src/inspector_agent.cc

+6-9
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,9 @@ class InspectorTimerHandle {
502502

503503
class NodeInspectorClient : public V8InspectorClient {
504504
public:
505-
NodeInspectorClient(node::Environment* env,
506-
v8::Platform* platform) : env_(env),
507-
platform_(platform),
508-
terminated_(false),
509-
running_nested_loop_(false) {
505+
NodeInspectorClient(node::Environment* env, node::NodePlatform* platform)
506+
: env_(env), platform_(platform), terminated_(false),
507+
running_nested_loop_(false) {
510508
client_ = V8Inspector::create(env->isolate(), this);
511509
contextCreated(env->context(), "Node.js Main Context");
512510
}
@@ -518,8 +516,7 @@ class NodeInspectorClient : public V8InspectorClient {
518516
terminated_ = false;
519517
running_nested_loop_ = true;
520518
while (!terminated_ && channel_->waitForFrontendMessage()) {
521-
while (v8::platform::PumpMessageLoop(platform_, env_->isolate()))
522-
{}
519+
platform_->FlushForegroundTasksInternal();
523520
}
524521
terminated_ = false;
525522
running_nested_loop_ = false;
@@ -647,7 +644,7 @@ class NodeInspectorClient : public V8InspectorClient {
647644

648645
private:
649646
node::Environment* env_;
650-
v8::Platform* platform_;
647+
node::NodePlatform* platform_;
651648
bool terminated_;
652649
bool running_nested_loop_;
653650
std::unique_ptr<V8Inspector> client_;
@@ -666,7 +663,7 @@ Agent::Agent(Environment* env) : parent_env_(env),
666663
Agent::~Agent() {
667664
}
668665

669-
bool Agent::Start(v8::Platform* platform, const char* path,
666+
bool Agent::Start(node::NodePlatform* platform, const char* path,
670667
const DebugOptions& options) {
671668
path_ = path == nullptr ? "" : path;
672669
debug_options_ = options;

src/inspector_agent.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// Forward declaration to break recursive dependency chain with src/env.h.
1515
namespace node {
1616
class Environment;
17+
class NodePlatform;
1718
} // namespace node
1819

1920
#include "v8.h"
@@ -42,7 +43,7 @@ class Agent {
4243
~Agent();
4344

4445
// Create client_, may create io_ if option enabled
45-
bool Start(v8::Platform* platform, const char* path,
46+
bool Start(node::NodePlatform* platform, const char* path,
4647
const DebugOptions& options);
4748
// Stop and destroy io_
4849
void Stop();

src/node.cc

+32-28
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "node_buffer.h"
2424
#include "node_constants.h"
2525
#include "node_javascript.h"
26+
#include "node_platform.h"
2627
#include "node_version.h"
2728
#include "node_internals.h"
2829
#include "node_revert.h"
@@ -250,25 +251,26 @@ node::DebugOptions debug_options;
250251

251252
static struct {
252253
#if NODE_USE_V8_PLATFORM
253-
void Initialize(int thread_pool_size) {
254+
void Initialize(int thread_pool_size, uv_loop_t* loop) {
254255
tracing_agent_ =
255-
trace_enabled ? new tracing::Agent() : nullptr;
256-
platform_ = v8::platform::CreateDefaultPlatform(
257-
thread_pool_size, v8::platform::IdleTaskSupport::kDisabled,
258-
v8::platform::InProcessStackDumping::kDisabled,
259-
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
256+
trace_enabled ? new tracing::Agent() : nullptr;
257+
platform_ = new NodePlatform(thread_pool_size, loop,
258+
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
260259
V8::InitializePlatform(platform_);
261260
tracing::TraceEventHelper::SetTracingController(
262-
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
263-
}
264-
265-
void PumpMessageLoop(Isolate* isolate) {
266-
v8::platform::PumpMessageLoop(platform_, isolate);
261+
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
267262
}
268263

269264
void Dispose() {
265+
platform_->Shutdown();
270266
delete platform_;
271267
platform_ = nullptr;
268+
delete tracing_agent_;
269+
tracing_agent_ = nullptr;
270+
}
271+
272+
void DrainVMTasks() {
273+
platform_->DrainBackgroundTasks();
272274
}
273275

274276
#if HAVE_INSPECTOR
@@ -293,12 +295,12 @@ static struct {
293295
tracing_agent_->Stop();
294296
}
295297

296-
v8::Platform* platform_;
297298
tracing::Agent* tracing_agent_;
299+
NodePlatform* platform_;
298300
#else // !NODE_USE_V8_PLATFORM
299-
void Initialize(int thread_pool_size) {}
300-
void PumpMessageLoop(Isolate* isolate) {}
301+
void Initialize(int thread_pool_size, uv_loop_t* loop) {}
301302
void Dispose() {}
303+
void DrainVMTasks() {}
302304
bool StartInspector(Environment *env, const char* script_path,
303305
const node::DebugOptions& options) {
304306
env->ThrowError("Node compiled with NODE_USE_V8_PLATFORM=0");
@@ -4555,19 +4557,14 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
45554557
SealHandleScope seal(isolate);
45564558
bool more;
45574559
do {
4558-
v8_platform.PumpMessageLoop(isolate);
4559-
more = uv_run(env.event_loop(), UV_RUN_ONCE);
4560-
4561-
if (more == false) {
4562-
v8_platform.PumpMessageLoop(isolate);
4563-
EmitBeforeExit(&env);
4564-
4565-
// Emit `beforeExit` if the loop became alive either after emitting
4566-
// event, or after running some callbacks.
4567-
more = uv_loop_alive(env.event_loop());
4568-
if (uv_run(env.event_loop(), UV_RUN_NOWAIT) != 0)
4569-
more = true;
4570-
}
4560+
uv_run(env.event_loop(), UV_RUN_DEFAULT);
4561+
4562+
EmitBeforeExit(&env);
4563+
4564+
v8_platform.DrainVMTasks();
4565+
// Emit `beforeExit` if the loop became alive either after emitting
4566+
// event, or after running some callbacks.
4567+
more = uv_loop_alive(env.event_loop());
45714568
} while (more == true);
45724569
}
45734570

@@ -4577,6 +4574,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
45774574
RunAtExit(&env);
45784575
uv_key_delete(&thread_local_env);
45794576

4577+
v8_platform.DrainVMTasks();
45804578
WaitForInspectorDisconnect(&env);
45814579
#if defined(LEAK_SANITIZER)
45824580
__lsan_do_leak_check();
@@ -4665,7 +4663,7 @@ int Start(int argc, char** argv) {
46654663
V8::SetEntropySource(crypto::EntropySource);
46664664
#endif // HAVE_OPENSSL
46674665

4668-
v8_platform.Initialize(v8_thread_pool_size);
4666+
v8_platform.Initialize(v8_thread_pool_size, uv_default_loop());
46694667
// Enable tracing when argv has --trace-events-enabled.
46704668
if (trace_enabled) {
46714669
fprintf(stderr, "Warning: Trace event is an experimental feature "
@@ -4682,6 +4680,12 @@ int Start(int argc, char** argv) {
46824680
v8_initialized = false;
46834681
V8::Dispose();
46844682

4683+
// uv_run cannot be called from the time before the beforeExit callback
4684+
// runs until the program exits unless the event loop has any referenced
4685+
// handles after beforeExit terminates. This prevents unrefed timers
4686+
// that happen to terminate during shutdown from being run unsafely.
4687+
// Since uv_run cannot be called, uv_async handles held by the platform
4688+
// will never be fully cleaned up.
46854689
v8_platform.Dispose();
46864690

46874691
delete[] exec_argv;

src/node_platform.cc

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#include "node_platform.h"
2+
3+
#include "util.h"
4+
5+
namespace node {
6+
7+
using v8::Isolate;
8+
using v8::Platform;
9+
using v8::Task;
10+
using v8::TracingController;
11+
12+
static void FlushTasks(uv_async_t* handle) {
13+
NodePlatform* platform = static_cast<NodePlatform*>(handle->data);
14+
platform->FlushForegroundTasksInternal();
15+
}
16+
17+
static void BackgroundRunner(void* data) {
18+
TaskQueue<Task>* background_tasks = static_cast<TaskQueue<Task>*>(data);
19+
while (Task* task = background_tasks->BlockingPop()) {
20+
task->Run();
21+
delete task;
22+
background_tasks->NotifyOfCompletion();
23+
}
24+
}
25+
26+
NodePlatform::NodePlatform(int thread_pool_size, uv_loop_t* loop,
27+
TracingController* tracing_controller)
28+
: loop_(loop) {
29+
CHECK_EQ(0, uv_async_init(loop, &flush_tasks_, FlushTasks));
30+
flush_tasks_.data = static_cast<void*>(this);
31+
uv_unref(reinterpret_cast<uv_handle_t*>(&flush_tasks_));
32+
if (tracing_controller) {
33+
tracing_controller_.reset(tracing_controller);
34+
} else {
35+
TracingController* controller = new TracingController();
36+
tracing_controller_.reset(controller);
37+
}
38+
for (int i = 0; i < thread_pool_size; i++) {
39+
uv_thread_t* t = new uv_thread_t();
40+
if (uv_thread_create(t, BackgroundRunner, &background_tasks_) != 0) {
41+
delete t;
42+
break;
43+
}
44+
threads_.push_back(std::unique_ptr<uv_thread_t>(t));
45+
}
46+
}
47+
48+
void NodePlatform::Shutdown() {
49+
background_tasks_.Stop();
50+
for (size_t i = 0; i < threads_.size(); i++) {
51+
CHECK_EQ(0, uv_thread_join(threads_[i].get()));
52+
}
53+
// uv_run cannot be called from the time before the beforeExit callback
54+
// runs until the program exits unless the event loop has any referenced
55+
// handles after beforeExit terminates. This prevents unrefed timers
56+
// that happen to terminate during shutdown from being run unsafely.
57+
// Since uv_run cannot be called, this handle will never be fully cleaned
58+
// up.
59+
uv_close(reinterpret_cast<uv_handle_t*>(&flush_tasks_), nullptr);
60+
}
61+
62+
size_t NodePlatform::NumberOfAvailableBackgroundThreads() {
63+
return threads_.size();
64+
}
65+
66+
static void RunForegroundTask(uv_timer_t* handle) {
67+
Task* task = static_cast<Task*>(handle->data);
68+
task->Run();
69+
delete task;
70+
uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
71+
delete reinterpret_cast<uv_timer_t*>(handle);
72+
});
73+
}
74+
75+
void NodePlatform::DrainBackgroundTasks() {
76+
FlushForegroundTasksInternal();
77+
background_tasks_.BlockingDrain();
78+
}
79+
80+
void NodePlatform::FlushForegroundTasksInternal() {
81+
while (auto delayed = foreground_delayed_tasks_.Pop()) {
82+
uint64_t delay_millis =
83+
static_cast<uint64_t>(delayed->second + 0.5) * 1000;
84+
uv_timer_t* handle = new uv_timer_t();
85+
handle->data = static_cast<void*>(delayed->first);
86+
uv_timer_init(loop_, handle);
87+
// Timers may not guarantee queue ordering of events with the same delay if
88+
// the delay is non-zero. This should not be a problem in practice.
89+
uv_timer_start(handle, RunForegroundTask, delay_millis, 0);
90+
uv_unref(reinterpret_cast<uv_handle_t*>(handle));
91+
delete delayed;
92+
}
93+
while (Task* task = foreground_tasks_.Pop()) {
94+
task->Run();
95+
delete task;
96+
}
97+
}
98+
99+
void NodePlatform::CallOnBackgroundThread(Task* task,
100+
ExpectedRuntime expected_runtime) {
101+
background_tasks_.Push(task);
102+
}
103+
104+
void NodePlatform::CallOnForegroundThread(Isolate* isolate, Task* task) {
105+
foreground_tasks_.Push(task);
106+
uv_async_send(&flush_tasks_);
107+
}
108+
109+
void NodePlatform::CallDelayedOnForegroundThread(Isolate* isolate,
110+
Task* task,
111+
double delay_in_seconds) {
112+
auto pair = new std::pair<Task*, double>(task, delay_in_seconds);
113+
foreground_delayed_tasks_.Push(pair);
114+
uv_async_send(&flush_tasks_);
115+
}
116+
117+
bool NodePlatform::IdleTasksEnabled(Isolate* isolate) { return false; }
118+
119+
double NodePlatform::MonotonicallyIncreasingTime() {
120+
// Convert nanos to seconds.
121+
return uv_hrtime() / 1e9;
122+
}
123+
124+
TracingController* NodePlatform::GetTracingController() {
125+
return tracing_controller_.get();
126+
}
127+
128+
template <class T>
129+
TaskQueue<T>::TaskQueue()
130+
: lock_(), tasks_available_(), tasks_drained_(),
131+
outstanding_tasks_(0), stopped_(false), task_queue_() { }
132+
133+
template <class T>
134+
void TaskQueue<T>::Push(T* task) {
135+
Mutex::ScopedLock scoped_lock(lock_);
136+
outstanding_tasks_++;
137+
task_queue_.push(task);
138+
tasks_available_.Signal(scoped_lock);
139+
}
140+
141+
template <class T>
142+
T* TaskQueue<T>::Pop() {
143+
Mutex::ScopedLock scoped_lock(lock_);
144+
T* result = nullptr;
145+
if (!task_queue_.empty()) {
146+
result = task_queue_.front();
147+
task_queue_.pop();
148+
}
149+
return result;
150+
}
151+
152+
template <class T>
153+
T* TaskQueue<T>::BlockingPop() {
154+
Mutex::ScopedLock scoped_lock(lock_);
155+
while (task_queue_.empty() && !stopped_) {
156+
tasks_available_.Wait(scoped_lock);
157+
}
158+
if (stopped_) {
159+
return nullptr;
160+
}
161+
T* result = task_queue_.front();
162+
task_queue_.pop();
163+
return result;
164+
}
165+
166+
template <class T>
167+
void TaskQueue<T>::NotifyOfCompletion() {
168+
Mutex::ScopedLock scoped_lock(lock_);
169+
if (--outstanding_tasks_ == 0) {
170+
tasks_drained_.Broadcast(scoped_lock);
171+
}
172+
}
173+
174+
template <class T>
175+
void TaskQueue<T>::BlockingDrain() {
176+
Mutex::ScopedLock scoped_lock(lock_);
177+
while (outstanding_tasks_ > 0) {
178+
tasks_drained_.Wait(scoped_lock);
179+
}
180+
}
181+
182+
template <class T>
183+
void TaskQueue<T>::Stop() {
184+
Mutex::ScopedLock scoped_lock(lock_);
185+
stopped_ = true;
186+
tasks_available_.Broadcast(scoped_lock);
187+
}
188+
189+
} // namespace node

0 commit comments

Comments
 (0)