From ece9faf23900d56fb2e1e9487cc51db17ffc288d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 26 Nov 2019 17:05:25 +0100 Subject: [PATCH] src: add more `can_call_into_js()` guards This is in preparation for running native `SetImmediate()` callbacks during shutdown. PR-URL: https://github.com/nodejs/node/pull/30666 Fixes: https://github.com/nodejs/node/issues/30643 Refs: https://github.com/nodejs/node/pull/30374 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- src/env.cc | 5 ++++- src/node_errors.cc | 5 ++++- src/node_process_events.cc | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/env.cc b/src/env.cc index f6c26e3af5de83..161c6b04f2ec55 100644 --- a/src/env.cc +++ b/src/env.cc @@ -540,6 +540,9 @@ void Environment::RegisterHandleCleanups() { } void Environment::CleanupHandles() { + Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(), + Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); + for (ReqWrapBase* request : req_wrap_queue_) request->Cancel(); @@ -674,7 +677,7 @@ void Environment::RunAndClearNativeImmediates() { head->Call(this); if (UNLIKELY(try_catch.HasCaught())) { - if (!try_catch.HasTerminated()) + if (!try_catch.HasTerminated() && can_call_into_js()) errors::TriggerUncaughtException(isolate(), try_catch); // We are done with the current callback. Move one iteration along, diff --git a/src/node_errors.cc b/src/node_errors.cc index 7161a2d1bb7167..9d038e3d1683e7 100644 --- a/src/node_errors.cc +++ b/src/node_errors.cc @@ -270,6 +270,9 @@ static void ReportFatalException(Environment* env, Local error, Local message, EnhanceFatalException enhance_stack) { + if (!env->can_call_into_js()) + enhance_stack = EnhanceFatalException::kDontEnhance; + Isolate* isolate = env->isolate(); CHECK(!error.IsEmpty()); CHECK(!message.IsEmpty()); @@ -914,7 +917,7 @@ void TriggerUncaughtException(Isolate* isolate, } MaybeLocal handled; - { + if (env->can_call_into_js()) { // We do not expect the global uncaught exception itself to throw any more // exceptions. If it does, exit the current Node.js instance. errors::TryCatchScope try_catch(env, diff --git a/src/node_process_events.cc b/src/node_process_events.cc index b090b6c24b3383..d192ef19b7abad 100644 --- a/src/node_process_events.cc +++ b/src/node_process_events.cc @@ -35,6 +35,8 @@ Maybe ProcessEmitWarningGeneric(Environment* env, const char* warning, const char* type, const char* code) { + if (!env->can_call_into_js()) return Just(false); + HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context());