Skip to content

Commit 0c91220

Browse files
committed
process,worker: ensure code after exit() effectless
ignore exception that indicaties a termination in napi call
1 parent ad562e1 commit 0c91220

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/js_native_api_v8.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ class CallbackWrapperBase : public CallbackWrapper {
308308
env->CallIntoModule([&](napi_env env) { result = cb(env, cbinfo_wrapper); },
309309
[&](napi_env env, v8::Local<v8::Value> value) {
310310
exceptionOccurred = true;
311+
if (env->terminatedOrTerminating()) {
312+
return;
313+
}
311314
env->isolate->ThrowException(value);
312315
});
313316

src/js_native_api_v8.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,20 @@ struct napi_env__ {
7272
}
7373

7474
static inline void HandleThrow(napi_env env, v8::Local<v8::Value> value) {
75+
if (env->terminatedOrTerminating()) {
76+
return;
77+
}
7578
env->isolate->ThrowException(value);
7679
}
7780

81+
// i.e. whether v8 exited or is about to exit
82+
inline bool terminatedOrTerminating() {
83+
return this->isolate->IsExecutionTerminating() || !can_call_into_js();
84+
}
85+
86+
// v8 uses a special exception to indicate termination, the
87+
// `handle_exception` callback should identify such case using
88+
// terminatedOrTerminating() before actually handle the exception
7889
template <typename T, typename U = decltype(HandleThrow)>
7990
inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
8091
int open_handle_scopes_before = open_handle_scopes;

src/node_api.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ template <bool enforceUncaughtExceptionPolicy, typename T>
8282
void node_napi_env__::CallbackIntoModule(T&& call) {
8383
CallIntoModule(call, [](napi_env env_, v8::Local<v8::Value> local_err) {
8484
node_napi_env__* env = static_cast<node_napi_env__*>(env_);
85+
if (env->terminatedOrTerminating()) {
86+
return;
87+
}
8588
node::Environment* node_env = env->node_env();
8689
if (!node_env->options()->force_node_api_uncaught_exceptions_policy &&
8790
!enforceUncaughtExceptionPolicy) {

0 commit comments

Comments
 (0)