From e73bf4355d4d2f934a607977109f2d64db5dce21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerhard=20St=C3=B6bich?= Date: Mon, 21 Mar 2022 21:35:05 +0100 Subject: [PATCH] async_hooks: remove destroyed symbol on Promises Promises are never destroyed manually therefore it's not needed to attach an object to track if destroy hook was called already. PR-URL: https://github.com/nodejs/node/pull/42402 Reviewed-By: Rich Trott Reviewed-By: Mohammed Keyvanzadeh --- lib/internal/async_hooks.js | 6 +----- src/async_wrap.cc | 11 +++++++---- typings/internalBinding/async_wrap.d.ts | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index f15fe5cc99b5c4..ebc9254d5c5109 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -326,14 +326,10 @@ function promiseInitHookWithDestroyTracking(promise, parent) { destroyTracking(promise, parent); } -const destroyedSymbol = Symbol('destroyed'); - function destroyTracking(promise, parent) { trackPromise(promise, parent); const asyncId = promise[async_id_symbol]; - const destroyed = { destroyed: false }; - promise[destroyedSymbol] = destroyed; - registerDestroyHook(promise, asyncId, destroyed); + registerDestroyHook(promise, asyncId); } function promiseBeforeHook(promise) { diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 77922bd04adac7..38f2eb421f487d 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -214,12 +214,13 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo& info) { p->env->RemoveCleanupHook(DestroyParamCleanupHook, p.get()); - if (!prop_bag->Get(p->env->context(), p->env->destroyed_string()) + if (!prop_bag.IsEmpty() && + !prop_bag->Get(p->env->context(), p->env->destroyed_string()) .ToLocal(&val)) { return; } - if (val->IsFalse()) { + if (val.IsEmpty() || val->IsFalse()) { AsyncWrap::EmitDestroy(p->env, p->asyncId); } // unique_ptr goes out of scope here and pointer is deleted. @@ -229,14 +230,16 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo& info) { static void RegisterDestroyHook(const FunctionCallbackInfo& args) { CHECK(args[0]->IsObject()); CHECK(args[1]->IsNumber()); - CHECK(args[2]->IsObject()); + CHECK(args.Length() == 2 || args[2]->IsObject()); Isolate* isolate = args.GetIsolate(); DestroyParam* p = new DestroyParam(); p->asyncId = args[1].As()->Value(); p->env = Environment::GetCurrent(args); p->target.Reset(isolate, args[0].As()); - p->propBag.Reset(isolate, args[2].As()); + if (args.Length() > 2) { + p->propBag.Reset(isolate, args[2].As()); + } p->target.SetWeak(p, AsyncWrap::WeakCallback, WeakCallbackType::kParameter); p->env->AddCleanupHook(DestroyParamCleanupHook, p); } diff --git a/typings/internalBinding/async_wrap.d.ts b/typings/internalBinding/async_wrap.d.ts index 9df451e23cb515..bfbda3d7ed890b 100644 --- a/typings/internalBinding/async_wrap.d.ts +++ b/typings/internalBinding/async_wrap.d.ts @@ -107,7 +107,7 @@ declare function InternalBinding(binding: 'async_wrap'): { promiseAfterHook: InternalAsyncWrapBinding.PromiseHook | undefined, promiseResolveHook: InternalAsyncWrapBinding.PromiseHook | undefined ): void; - registerDestroyHook(promise: Promise, asyncId: number, destroyed: { destroyed: boolean }): void; + registerDestroyHook(resource: object, asyncId: number, destroyed?: { destroyed: boolean }): void; async_hook_fields: Uint32Array; async_id_fields: Float64Array; async_ids_stack: Float64Array;