From b7586e459ba53f4da36405863f485eb611fdce5d Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 1 May 2021 14:18:00 -0700 Subject: [PATCH 1/2] fix: allow Node.js to manage microtasks queue When `uv_run()` resulted in invocation of JS functions the microtask queue checkpoint in Node's CallbackScope was a no-op because the expected microtask queue policy was `kExplicit` and Electron ran under `kScoped` policy. This change switches policy to `kExplicit` right before `uv_run()` and reverts it back to original value after `uv_run()` completes to provide better compatibility with Node. --- shell/common/node_bindings.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index e8f942a7fa00d..ecd84d2a7d5f3 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -575,8 +575,9 @@ void NodeBindings::UvRunOnce() { // Enter node context while dealing with uv events. v8::Context::Scope context_scope(env->context()); - // Perform microtask checkpoint after running JavaScript. - gin_helper::MicrotasksScope microtasks_scope(env->isolate()); + auto old_policy = env->isolate()->GetMicrotasksPolicy(); + DCHECK_EQ(v8::MicrotasksScope::GetCurrentDepth(env->isolate()), 0); + env->isolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit); if (browser_env_ != BrowserEnvironment::kBrowser) TRACE_EVENT_BEGIN0("devtools.timeline", "FunctionCall"); @@ -587,6 +588,8 @@ void NodeBindings::UvRunOnce() { if (browser_env_ != BrowserEnvironment::kBrowser) TRACE_EVENT_END0("devtools.timeline", "FunctionCall"); + env->isolate()->SetMicrotasksPolicy(old_policy); + if (r == 0) base::RunLoop().QuitWhenIdle(); // Quit from uv. From bd3906df59568e7a16f08a1d65162eee6dfa00e0 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Mon, 3 May 2021 09:22:42 -0700 Subject: [PATCH 2/2] add comment --- shell/common/node_bindings.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index ecd84d2a7d5f3..a6fb17e343c0e 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -575,6 +575,10 @@ void NodeBindings::UvRunOnce() { // Enter node context while dealing with uv events. v8::Context::Scope context_scope(env->context()); + // Node.js expects `kExplicit` microtasks policy and will run microtasks + // checkpoints after every call into JavaScript. Since we use a different + // policy in the renderer - switch to `kExplicit` and then drop back to the + // previous policy value. auto old_policy = env->isolate()->GetMicrotasksPolicy(); DCHECK_EQ(v8::MicrotasksScope::GetCurrentDepth(env->isolate()), 0); env->isolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);