Skip to content

Commit

Permalink
fix: renderer crash on setImmediate
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Nov 6, 2020
1 parent 2daca0f commit 7a64f7a
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions shell/common/node_bindings.cc
Expand Up @@ -103,6 +103,8 @@ ELECTRON_DESKTOP_CAPTURER_MODULE(V)

namespace {

using BrowserEnvironment = electron::NodeBindings::BrowserEnvironment;

void stop_and_close_uv_loop(uv_loop_t* loop) {
uv_stop(loop);

Expand All @@ -129,6 +131,8 @@ void stop_and_close_uv_loop(uv_loop_t* loop) {

bool g_is_initialized = false;

BrowserEnvironment g_browser_env;

bool IsPackagedApp() {
base::FilePath exe_path;
base::PathService::Get(base::FILE_EXE, &exe_path);
Expand Down Expand Up @@ -160,7 +164,7 @@ bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
// fall back to Blink's logic.
v8::Isolate* isolate = context->GetIsolate();
if (node::Environment::GetCurrent(isolate) == nullptr) {
if (gin_helper::Locker::IsBrowserProcess())
if (g_browser_env == BrowserEnvironment::kBrowser)
return false;
return blink::V8Initializer::WasmCodeGenerationCheckCallbackInMainThread(
context, v8::String::Empty(isolate));
Expand All @@ -170,6 +174,26 @@ bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
v8::String::Empty(isolate));
}

void ErrorMessageListener(v8::Local<v8::Message> message,
v8::Local<v8::Value> data) {
if (g_browser_env == BrowserEnvironment::kWorker) {
blink::V8Initializer::MessageHandlerInWorker(message, data);
} else {
blink::V8Initializer::MessageHandlerInMainThread(message, data);
}

v8::Isolate* isolate = v8::Isolate::GetCurrent();
node::Environment* env = node::Environment::GetCurrent(isolate);

// TODO(codebytere): properly emit the after() hooks now
// that the exception has been handled.
// See node/lib/internal/process/execution.js#L176-L180

// Ensure that the async id stack is properly cleared so the async
// hook stack does not becomes corrupted.
env->async_hooks()->clear_async_id_stack();
}

// Initialize Node.js cli options to pass to Node.js
// See https://nodejs.org/api/cli.html#cli_options
void SetNodeCliFlags() {
Expand Down Expand Up @@ -281,6 +305,7 @@ base::FilePath GetResourcesPath() {

NodeBindings::NodeBindings(BrowserEnvironment browser_env)
: browser_env_(browser_env), weak_factory_(this) {
g_browser_env = browser_env;
if (browser_env == BrowserEnvironment::kWorker) {
uv_loop_init(&worker_loop_);
uv_loop_ = &worker_loop_;
Expand Down Expand Up @@ -467,8 +492,14 @@ node::Environment* NodeBindings::CreateEnvironment(
is.policy = v8::MicrotasksPolicy::kScoped;

// We do not want to use Node.js' message listener as it interferes with
// Blink's.
// Blink's. Instead, we use Blink's slightly modified to work with async
// hooks.
is.flags &= ~node::IsolateSettingsFlags::MESSAGE_LISTENER_WITH_ERROR_LEVEL;
context->GetIsolate()->AddMessageListenerWithErrorLevel(
ErrorMessageListener,
v8::Isolate::kMessageError | v8::Isolate::kMessageWarning |
v8::Isolate::kMessageInfo | v8::Isolate::kMessageDebug |
v8::Isolate::kMessageLog);

// We do not want to use the promise rejection callback that Node.js uses,
// because it does not send PromiseRejectionEvents to the global script
Expand Down

0 comments on commit 7a64f7a

Please sign in to comment.