From 9836376fa82da54ddfc7e527b820f47d5d106f5e Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Sun, 14 Jun 2020 14:05:17 -0700 Subject: [PATCH] fix: let Node.js perform microtask checkpoint in the main process --- shell/browser/electron_browser_main_parts.h | 1 + shell/browser/javascript_environment.h | 2 ++ shell/browser/microtasks_runner.cc | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/shell/browser/electron_browser_main_parts.h b/shell/browser/electron_browser_main_parts.h index b2f36da53641f..446e3e1c2474a 100644 --- a/shell/browser/electron_browser_main_parts.h +++ b/shell/browser/electron_browser_main_parts.h @@ -90,6 +90,7 @@ class ElectronBrowserMainParts : public content::BrowserMainParts { Browser* browser() { return browser_.get(); } BrowserProcessImpl* browser_process() { return fake_browser_process_.get(); } + NodeEnvironment* node_env() { return node_env_.get(); } protected: // content::BrowserMainParts: diff --git a/shell/browser/javascript_environment.h b/shell/browser/javascript_environment.h index 17ce402d3a771..65fb113fd466a 100644 --- a/shell/browser/javascript_environment.h +++ b/shell/browser/javascript_environment.h @@ -57,6 +57,8 @@ class NodeEnvironment { explicit NodeEnvironment(node::Environment* env); ~NodeEnvironment(); + node::Environment* env() { return env_; } + private: node::Environment* env_; diff --git a/shell/browser/microtasks_runner.cc b/shell/browser/microtasks_runner.cc index 9c20bc827eb92..5090029ce7623 100644 --- a/shell/browser/microtasks_runner.cc +++ b/shell/browser/microtasks_runner.cc @@ -4,6 +4,10 @@ // found in the LICENSE file. #include "shell/browser/microtasks_runner.h" + +#include "shell/browser/electron_browser_main_parts.h" +#include "shell/browser/javascript_environment.h" +#include "shell/common/node_includes.h" #include "v8/include/v8.h" namespace electron { @@ -15,7 +19,19 @@ void MicrotasksRunner::WillProcessTask(const base::PendingTask& pending_task, void MicrotasksRunner::DidProcessTask(const base::PendingTask& pending_task) { v8::Isolate::Scope scope(isolate_); - v8::MicrotasksScope::PerformCheckpoint(isolate_); + // In the browser process we follow Node.js microtask policy of kExplicit + // and let the MicrotaskRunner which is a task observer for chromium UI thread + // scheduler run the micotask checkpoint. This worked fine because Node.js + // also runs microtasks through its task queue, but after + // https://github.com/electron/electron/issues/20013 Node.js now performs its + // own microtask checkpoint and it may happen is some situations that there is + // contention for performing checkpoint between Node.js and chromium, ending + // up Node.js dealying its callbacks. To fix this, now we always lets Node.js + // handle the checkpoint in the browser process. + auto* node_env = electron::ElectronBrowserMainParts::Get()->node_env(); + node::InternalCallbackScope microtask_scope( + node_env->env(), v8::Local(), {0, 0}, + node::InternalCallbackScope::kAllowEmptyResource); } } // namespace electron