Skip to content

Commit

Permalink
fix: let Node.js perform microtask checkpoint in the main process
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak1556 committed Jun 14, 2020
1 parent b9ac334 commit 9836376
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions shell/browser/electron_browser_main_parts.h
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/javascript_environment.h
Expand Up @@ -57,6 +57,8 @@ class NodeEnvironment {
explicit NodeEnvironment(node::Environment* env);
~NodeEnvironment();

node::Environment* env() { return env_; }

private:
node::Environment* env_;

Expand Down
18 changes: 17 additions & 1 deletion shell/browser/microtasks_runner.cc
Expand Up @@ -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 {
Expand All @@ -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<v8::Object>(), {0, 0},
node::InternalCallbackScope::kAllowEmptyResource);
}

} // namespace electron

0 comments on commit 9836376

Please sign in to comment.