fix: let Node.js perform microtask checkpoint in the main process (#24131)

* fix: let Node.js perform microtask checkpoint in the main process

* fix: don't specify v8::MicrotasksScope for explicit policy

* fix: remove checkpoint from some call-sites

We already perform checkpoint at the end of a task,
either through MicrotaskRunner or through NodeBindings.
There isn't a need to add them again when calling into JS
except when dealing with promises.

* fix: remove checkpoint from some call-sites

We already perform checkpoint at the end of a task,
either through MicrotaskRunner or through NodeBindings.
There isn't a need to add them again when calling into JS
except when dealing with promises.

* fix incorrect specs

* default constructor arguments are considered for explicit mark

* add regression spec
This commit is contained in:
Robo 2020-06-17 10:08:10 -07:00 committed by GitHub
parent 59f9d75324
commit 7cc780d077
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 145 additions and 39 deletions

View file

@ -135,8 +135,6 @@ class JSChunkedDataPipeGetter : public gin::Wrappable<JSChunkedDataPipeGetter>,
data_producer_ = std::make_unique<mojo::DataPipeProducer>(std::move(pipe));
v8::HandleScope handle_scope(isolate_);
v8::MicrotasksScope script_scope(isolate_,
v8::MicrotasksScope::kRunMicrotasks);
auto maybe_wrapper = GetWrapper(isolate_);
v8::Local<v8::Value> wrapper;
if (!maybe_wrapper.ToLocal(&wrapper)) {

View file

@ -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:

View file

@ -57,6 +57,8 @@ class NodeEnvironment {
explicit NodeEnvironment(node::Environment* env);
~NodeEnvironment();
node::Environment* env() { return env_; }
private:
node::Environment* env_;

View file

@ -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,21 @@ 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 microtasks_scope(
node_env->env(), v8::Local<v8::Object>(), {0, 0},
node::InternalCallbackScope::kAllowEmptyResource);
}
}
} // namespace electron