@@ -176,7 +176,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
176176class PromiseWrap : public AsyncWrap {
177177 public:
178178 PromiseWrap (Environment* env, Local<Object> object, bool silent)
179- : AsyncWrap(env, object, PROVIDER_PROMISE, - 1 , silent) {
179+ : AsyncWrap(env, object, PROVIDER_PROMISE, kInvalidAsyncId , silent) {
180180 MakeWeak ();
181181 }
182182
@@ -382,7 +382,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
382382
383383void AsyncWrap::GetAsyncId (const FunctionCallbackInfo<Value>& args) {
384384 AsyncWrap* wrap;
385- args.GetReturnValue ().Set (- 1 );
385+ args.GetReturnValue ().Set (kInvalidAsyncId );
386386 ASSIGN_OR_RETURN_UNWRAP (&wrap, args.Holder ());
387387 args.GetReturnValue ().Set (wrap->get_async_id ());
388388}
@@ -409,10 +409,15 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
409409 AsyncWrap* wrap;
410410 ASSIGN_OR_RETURN_UNWRAP (&wrap, args.Holder ());
411411 double execution_async_id =
412- args[0 ]->IsNumber () ? args[0 ].As <Number>()->Value () : - 1 ;
412+ args[0 ]->IsNumber () ? args[0 ].As <Number>()->Value () : kInvalidAsyncId ;
413413 wrap->AsyncReset (execution_async_id);
414414}
415415
416+ void AsyncWrap::EmitDestroy () {
417+ AsyncWrap::EmitDestroy (env (), async_id_);
418+ // Ensure no double destroy is emitted via AsyncReset().
419+ async_id_ = kInvalidAsyncId ;
420+ }
416421
417422void AsyncWrap::QueueDestroyAsyncId (const FunctionCallbackInfo<Value>& args) {
418423 CHECK (args[0 ]->IsNumber ());
@@ -474,7 +479,7 @@ void AsyncWrap::Initialize(Local<Object> target,
474479 // kDefaultTriggerAsyncId: Write the id of the resource responsible for a
475480 // handle's creation just before calling the new handle's constructor.
476481 // After the new handle is constructed kDefaultTriggerAsyncId is set back
477- // to -1 .
482+ // to kInvalidAsyncId .
478483 FORCE_SET_TARGET_FIELD (target,
479484 " async_id_fields" ,
480485 env->async_hooks ()->async_id_fields ().GetJSArray ());
@@ -558,15 +563,15 @@ AsyncWrap::AsyncWrap(Environment* env,
558563 CHECK_NE (provider, PROVIDER_NONE);
559564 CHECK_GE (object->InternalFieldCount (), 1 );
560565
561- async_id_ = - 1 ;
566+ async_id_ = kInvalidAsyncId ;
562567 // Use AsyncReset() call to execute the init() callbacks.
563568 AsyncReset (execution_async_id, silent);
564569}
565570
566571
567572AsyncWrap::~AsyncWrap () {
568573 EmitTraceEventDestroy ();
569- EmitDestroy (env (), get_async_id () );
574+ EmitDestroy ();
570575}
571576
572577void AsyncWrap::EmitTraceEventDestroy () {
@@ -602,16 +607,16 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
602607// and reused over their lifetime. This way a new uid can be assigned when
603608// the resource is pulled out of the pool and put back into use.
604609void AsyncWrap::AsyncReset (double execution_async_id, bool silent) {
605- if (async_id_ != - 1 ) {
610+ if (async_id_ != kInvalidAsyncId ) {
606611 // This instance was in use before, we have already emitted an init with
607612 // its previous async_id and need to emit a matching destroy for that
608613 // before generating a new async_id.
609- EmitDestroy (env (), async_id_ );
614+ EmitDestroy ();
610615 }
611616
612617 // Now we can assign a new async_id_ to this instance.
613- async_id_ =
614- execution_async_id == - 1 ? env ()-> new_async_id () : execution_async_id;
618+ async_id_ = execution_async_id == kInvalidAsyncId ? env ()-> new_async_id ()
619+ : execution_async_id;
615620 trigger_async_id_ = env ()->get_default_trigger_async_id ();
616621
617622 switch (provider_type ()) {
@@ -693,7 +698,7 @@ async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
693698 // Environment::GetCurrent() allocates a Local<> handle.
694699 HandleScope handle_scope (isolate);
695700 Environment* env = Environment::GetCurrent (isolate);
696- if (env == nullptr ) return - 1 ;
701+ if (env == nullptr ) return AsyncWrap:: kInvalidAsyncId ;
697702 return env->execution_async_id ();
698703}
699704
@@ -702,7 +707,7 @@ async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
702707 // Environment::GetCurrent() allocates a Local<> handle.
703708 HandleScope handle_scope (isolate);
704709 Environment* env = Environment::GetCurrent (isolate);
705- if (env == nullptr ) return - 1 ;
710+ if (env == nullptr ) return AsyncWrap:: kInvalidAsyncId ;
706711 return env->trigger_async_id ();
707712}
708713
@@ -727,7 +732,7 @@ async_context EmitAsyncInit(Isolate* isolate,
727732 CHECK_NOT_NULL (env);
728733
729734 // Initialize async context struct
730- if (trigger_async_id == - 1 )
735+ if (trigger_async_id == AsyncWrap:: kInvalidAsyncId )
731736 trigger_async_id = env->get_default_trigger_async_id ();
732737
733738 async_context context = {
0 commit comments