Skip to content

Commit f27b5e4

Browse files
committedSep 21, 2017
src: prepare platform for upstream V8 changes
V8 platform tasks may schedule other tasks (both background and foreground), and may perform asynchronous operations like resolving Promises. To address that: - Run the task queue drain call inside a callback scope. This makes sure asynchronous operations inside it, like resolving promises, lead to the microtask queue and any subsequent operations not being silently forgotten. - Move the task queue drain call before `EmitBeforeExit()` and only run `EmitBeforeExit()` if there is no new event loop work. - Account for possible new foreground tasks scheduled by background tasks in `DrainBackgroundTasks()`. PR-URL: #15428 Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Matthew Loring <[email protected]>
1 parent 01c680b commit f27b5e4

File tree

4 files changed

+69
-37
lines changed

4 files changed

+69
-37
lines changed
 

‎src/node.cc

+16-29
Original file line numberDiff line numberDiff line change
@@ -1339,30 +1339,6 @@ void AddPromiseHook(v8::Isolate* isolate, promise_hook_func fn, void* arg) {
13391339
env->AddPromiseHook(fn, arg);
13401340
}
13411341

1342-
class InternalCallbackScope {
1343-
public:
1344-
InternalCallbackScope(Environment* env,
1345-
Local<Object> object,
1346-
const async_context& asyncContext);
1347-
~InternalCallbackScope();
1348-
void Close();
1349-
1350-
inline bool Failed() const { return failed_; }
1351-
inline void MarkAsFailed() { failed_ = true; }
1352-
inline bool IsInnerMakeCallback() const {
1353-
return callback_scope_.in_makecallback();
1354-
}
1355-
1356-
private:
1357-
Environment* env_;
1358-
async_context async_context_;
1359-
v8::Local<v8::Object> object_;
1360-
Environment::AsyncCallbackScope callback_scope_;
1361-
bool failed_ = false;
1362-
bool pushed_ids_ = false;
1363-
bool closed_ = false;
1364-
};
1365-
13661342
CallbackScope::CallbackScope(Isolate* isolate,
13671343
Local<Object> object,
13681344
async_context asyncContext)
@@ -1381,17 +1357,21 @@ CallbackScope::~CallbackScope() {
13811357

13821358
InternalCallbackScope::InternalCallbackScope(Environment* env,
13831359
Local<Object> object,
1384-
const async_context& asyncContext)
1360+
const async_context& asyncContext,
1361+
ResourceExpectation expect)
13851362
: env_(env),
13861363
async_context_(asyncContext),
13871364
object_(object),
13881365
callback_scope_(env) {
1389-
CHECK(!object.IsEmpty());
1366+
if (expect == kRequireResource) {
1367+
CHECK(!object.IsEmpty());
1368+
}
13901369

1370+
HandleScope handle_scope(env->isolate());
13911371
// If you hit this assertion, you forgot to enter the v8::Context first.
13921372
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
13931373

1394-
if (env->using_domains()) {
1374+
if (env->using_domains() && !object_.IsEmpty()) {
13951375
DomainEnter(env, object_);
13961376
}
13971377

@@ -1413,6 +1393,7 @@ InternalCallbackScope::~InternalCallbackScope() {
14131393
void InternalCallbackScope::Close() {
14141394
if (closed_) return;
14151395
closed_ = true;
1396+
HandleScope handle_scope(env_->isolate());
14161397

14171398
if (pushed_ids_)
14181399
env_->async_hooks()->pop_ids(async_context_.async_id);
@@ -1423,7 +1404,7 @@ void InternalCallbackScope::Close() {
14231404
AsyncWrap::EmitAfter(env_, async_context_.async_id);
14241405
}
14251406

1426-
if (env_->using_domains()) {
1407+
if (env_->using_domains() && !object_.IsEmpty()) {
14271408
DomainExit(env_, object_);
14281409
}
14291410

@@ -1463,6 +1444,7 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
14631444
int argc,
14641445
Local<Value> argv[],
14651446
async_context asyncContext) {
1447+
CHECK(!recv.IsEmpty());
14661448
InternalCallbackScope scope(env, recv, asyncContext);
14671449
if (scope.Failed()) {
14681450
return Undefined(env->isolate());
@@ -4726,9 +4708,14 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
47264708
do {
47274709
uv_run(env.event_loop(), UV_RUN_DEFAULT);
47284710

4711+
v8_platform.DrainVMTasks();
4712+
4713+
more = uv_loop_alive(env.event_loop());
4714+
if (more)
4715+
continue;
4716+
47294717
EmitBeforeExit(&env);
47304718

4731-
v8_platform.DrainVMTasks();
47324719
// Emit `beforeExit` if the loop became alive either after emitting
47334720
// event, or after running some callbacks.
47344721
more = uv_loop_alive(env.event_loop());

‎src/node_internals.h

+28
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,36 @@ v8::MaybeLocal<v8::Value> InternalMakeCallback(
294294
v8::Local<v8::Value> argv[],
295295
async_context asyncContext);
296296

297+
class InternalCallbackScope {
298+
public:
299+
// Tell the constructor whether its `object` parameter may be empty or not.
300+
enum ResourceExpectation { kRequireResource, kAllowEmptyResource };
301+
InternalCallbackScope(Environment* env,
302+
v8::Local<v8::Object> object,
303+
const async_context& asyncContext,
304+
ResourceExpectation expect = kRequireResource);
305+
~InternalCallbackScope();
306+
void Close();
307+
308+
inline bool Failed() const { return failed_; }
309+
inline void MarkAsFailed() { failed_ = true; }
310+
inline bool IsInnerMakeCallback() const {
311+
return callback_scope_.in_makecallback();
312+
}
313+
314+
private:
315+
Environment* env_;
316+
async_context async_context_;
317+
v8::Local<v8::Object> object_;
318+
Environment::AsyncCallbackScope callback_scope_;
319+
bool failed_ = false;
320+
bool pushed_ids_ = false;
321+
bool closed_ = false;
322+
};
323+
297324
} // namespace node
298325

326+
299327
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
300328

301329
#endif // SRC_NODE_INTERNALS_H_

‎src/node_platform.cc

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include "node_platform.h"
2+
#include "node_internals.h"
23

34
#include "util.h"
45

56
namespace node {
67

8+
using v8::HandleScope;
79
using v8::Isolate;
10+
using v8::Local;
11+
using v8::Object;
812
using v8::Platform;
913
using v8::Task;
1014
using v8::TracingController;
@@ -63,22 +67,33 @@ size_t NodePlatform::NumberOfAvailableBackgroundThreads() {
6367
return threads_.size();
6468
}
6569

66-
static void RunForegroundTask(uv_timer_t* handle) {
67-
Task* task = static_cast<Task*>(handle->data);
70+
static void RunForegroundTask(Task* task) {
71+
Isolate* isolate = Isolate::GetCurrent();
72+
HandleScope scope(isolate);
73+
Environment* env = Environment::GetCurrent(isolate);
74+
InternalCallbackScope cb_scope(env, Local<Object>(), { 0, 0 },
75+
InternalCallbackScope::kAllowEmptyResource);
6876
task->Run();
6977
delete task;
78+
}
79+
80+
static void RunForegroundTask(uv_timer_t* handle) {
81+
Task* task = static_cast<Task*>(handle->data);
82+
RunForegroundTask(task);
7083
uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
7184
delete reinterpret_cast<uv_timer_t*>(handle);
7285
});
7386
}
7487

7588
void NodePlatform::DrainBackgroundTasks() {
76-
FlushForegroundTasksInternal();
77-
background_tasks_.BlockingDrain();
89+
while (FlushForegroundTasksInternal())
90+
background_tasks_.BlockingDrain();
7891
}
7992

80-
void NodePlatform::FlushForegroundTasksInternal() {
93+
bool NodePlatform::FlushForegroundTasksInternal() {
94+
bool did_work = false;
8195
while (auto delayed = foreground_delayed_tasks_.Pop()) {
96+
did_work = true;
8297
uint64_t delay_millis =
8398
static_cast<uint64_t>(delayed->second + 0.5) * 1000;
8499
uv_timer_t* handle = new uv_timer_t();
@@ -91,9 +106,10 @@ void NodePlatform::FlushForegroundTasksInternal() {
91106
delete delayed;
92107
}
93108
while (Task* task = foreground_tasks_.Pop()) {
94-
task->Run();
95-
delete task;
109+
did_work = true;
110+
RunForegroundTask(task);
96111
}
112+
return did_work;
97113
}
98114

99115
void NodePlatform::CallOnBackgroundThread(Task* task,

‎src/node_platform.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class NodePlatform : public v8::Platform {
3939
virtual ~NodePlatform() {}
4040

4141
void DrainBackgroundTasks();
42-
void FlushForegroundTasksInternal();
42+
// Returns true iff work was dispatched or executed.
43+
bool FlushForegroundTasksInternal();
4344
void Shutdown();
4445

4546
// v8::Platform implementation.

0 commit comments

Comments
 (0)
Please sign in to comment.