@@ -165,6 +165,7 @@ static void DestroyIdsCb(uv_timer_t* handle) {
165165 if (ret.IsEmpty ()) {
166166 ClearFatalExceptionHandlers (env);
167167 FatalException (env->isolate (), try_catch);
168+ UNREACHABLE ();
168169 }
169170 }
170171 } while (!env->destroy_ids_list ()->empty ());
@@ -218,69 +219,43 @@ bool DomainExit(Environment* env, v8::Local<v8::Object> object) {
218219}
219220
220221
221- static bool PreCallbackExecution (AsyncWrap* wrap, bool run_domain_cbs) {
222- if (wrap->env ()->using_domains () && run_domain_cbs) {
223- bool is_disposed = DomainEnter (wrap->env (), wrap->object ());
224- if (is_disposed)
225- return false ;
226- }
227-
228- return AsyncWrap::EmitBefore (wrap->env (), wrap->get_id ());
229- }
230-
231-
232- bool AsyncWrap::EmitBefore (Environment* env, double async_id) {
222+ void AsyncWrap::EmitBefore (Environment* env, double async_id) {
233223 AsyncHooks* async_hooks = env->async_hooks ();
234224
235- if (async_hooks->fields ()[AsyncHooks::kBefore ] > 0 ) {
236- Local<Value> uid = Number::New (env->isolate (), async_id);
237- Local<Function> fn = env->async_hooks_before_function ();
238- TryCatch try_catch (env->isolate ());
239- MaybeLocal<Value> ar = fn->Call (
240- env->context (), Undefined (env->isolate ()), 1 , &uid);
241- if (ar.IsEmpty ()) {
242- ClearFatalExceptionHandlers (env);
243- FatalException (env->isolate (), try_catch);
244- return false ;
245- }
246- }
247-
248- return true ;
249- }
250-
251-
252- static bool PostCallbackExecution (AsyncWrap* wrap, bool run_domain_cbs) {
253- if (!AsyncWrap::EmitAfter (wrap->env (), wrap->get_id ()))
254- return false ;
225+ if (async_hooks->fields ()[AsyncHooks::kBefore ] == 0 )
226+ return ;
255227
256- if (wrap->env ()->using_domains () && run_domain_cbs) {
257- bool is_disposed = DomainExit (wrap->env (), wrap->object ());
258- if (is_disposed)
259- return false ;
228+ Local<Value> uid = Number::New (env->isolate (), async_id);
229+ Local<Function> fn = env->async_hooks_before_function ();
230+ TryCatch try_catch (env->isolate ());
231+ MaybeLocal<Value> ar = fn->Call (
232+ env->context (), Undefined (env->isolate ()), 1 , &uid);
233+ if (ar.IsEmpty ()) {
234+ ClearFatalExceptionHandlers (env);
235+ FatalException (env->isolate (), try_catch);
236+ UNREACHABLE ();
260237 }
261-
262- return true ;
263238}
264239
265- bool AsyncWrap::EmitAfter (Environment* env, double async_id) {
240+
241+ void AsyncWrap::EmitAfter (Environment* env, double async_id) {
266242 AsyncHooks* async_hooks = env->async_hooks ();
267243
268- // If the callback failed then the after() hooks will be called at the end
269- // of _fatalException().
270- if (async_hooks->fields ()[AsyncHooks::kAfter ] > 0 ) {
271- Local<Value> uid = Number::New (env->isolate (), async_id);
272- Local<Function> fn = env->async_hooks_after_function ();
273- TryCatch try_catch (env->isolate ());
274- MaybeLocal<Value> ar = fn->Call (
275- env->context (), Undefined (env->isolate ()), 1 , &uid);
276- if (ar.IsEmpty ()) {
277- ClearFatalExceptionHandlers (env);
278- FatalException (env->isolate (), try_catch);
279- return false ;
280- }
281- }
244+ if (async_hooks->fields ()[AsyncHooks::kAfter ] == 0 )
245+ return ;
282246
283- return true ;
247+ // If the user's callback failed then the after() hooks will be called at the
248+ // end of _fatalException().
249+ Local<Value> uid = Number::New (env->isolate (), async_id);
250+ Local<Function> fn = env->async_hooks_after_function ();
251+ TryCatch try_catch (env->isolate ());
252+ MaybeLocal<Value> ar = fn->Call (
253+ env->context (), Undefined (env->isolate ()), 1 , &uid);
254+ if (ar.IsEmpty ()) {
255+ ClearFatalExceptionHandlers (env);
256+ FatalException (env->isolate (), try_catch);
257+ UNREACHABLE ();
258+ }
284259}
285260
286261class PromiseWrap : public AsyncWrap {
@@ -373,9 +348,9 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
373348 CHECK_NE (wrap, nullptr );
374349 if (type == PromiseHookType::kBefore ) {
375350 env->async_hooks ()->push_ids (wrap->get_id (), wrap->get_trigger_id ());
376- PreCallbackExecution (wrap, false );
351+ AsyncWrap::EmitBefore (wrap-> env (), wrap-> get_id () );
377352 } else if (type == PromiseHookType::kAfter ) {
378- PostCallbackExecution (wrap, false );
353+ AsyncWrap::EmitAfter (wrap-> env (), wrap-> get_id () );
379354 if (env->current_async_id () == wrap->get_id ()) {
380355 // This condition might not be true if async_hooks was enabled during
381356 // the promise callback execution.
@@ -696,18 +671,27 @@ MaybeLocal<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
696671 get_id (),
697672 get_trigger_id ());
698673
699- if (!PreCallbackExecution (this , true )) {
674+ // Return v8::Undefined() because returning an empty handle will cause
675+ // ToLocalChecked() to abort.
676+ if (env ()->using_domains () && DomainEnter (env (), object ())) {
700677 return Undefined (env ()->isolate ());
701678 }
702679
703- // Finally... Get to running the user's callback.
680+ // No need to check a return value because the application will exit if an
681+ // exception occurs.
682+ AsyncWrap::EmitBefore (env (), get_id ());
683+
704684 MaybeLocal<Value> ret = cb->Call (env ()->context (), object (), argc, argv);
705685
706686 if (ret.IsEmpty ()) {
707687 return ret;
708688 }
709689
710- if (!PostCallbackExecution (this , true )) {
690+ AsyncWrap::EmitAfter (env (), get_id ());
691+
692+ // Return v8::Undefined() because returning an empty handle will cause
693+ // ToLocalChecked() to abort.
694+ if (env ()->using_domains () && DomainExit (env (), object ())) {
711695 return Undefined (env ()->isolate ());
712696 }
713697
0 commit comments