Skip to content

Commit e73bf43

Browse files
Flarnajuanarbol
authored andcommitted
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: #42402 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent 9b0ac65 commit e73bf43

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

lib/internal/async_hooks.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,10 @@ function promiseInitHookWithDestroyTracking(promise, parent) {
326326
destroyTracking(promise, parent);
327327
}
328328

329-
const destroyedSymbol = Symbol('destroyed');
330-
331329
function destroyTracking(promise, parent) {
332330
trackPromise(promise, parent);
333331
const asyncId = promise[async_id_symbol];
334-
const destroyed = { destroyed: false };
335-
promise[destroyedSymbol] = destroyed;
336-
registerDestroyHook(promise, asyncId, destroyed);
332+
registerDestroyHook(promise, asyncId);
337333
}
338334

339335
function promiseBeforeHook(promise) {

src/async_wrap.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,13 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
214214

215215
p->env->RemoveCleanupHook(DestroyParamCleanupHook, p.get());
216216

217-
if (!prop_bag->Get(p->env->context(), p->env->destroyed_string())
217+
if (!prop_bag.IsEmpty() &&
218+
!prop_bag->Get(p->env->context(), p->env->destroyed_string())
218219
.ToLocal(&val)) {
219220
return;
220221
}
221222

222-
if (val->IsFalse()) {
223+
if (val.IsEmpty() || val->IsFalse()) {
223224
AsyncWrap::EmitDestroy(p->env, p->asyncId);
224225
}
225226
// unique_ptr goes out of scope here and pointer is deleted.
@@ -229,14 +230,16 @@ void AsyncWrap::WeakCallback(const WeakCallbackInfo<DestroyParam>& info) {
229230
static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
230231
CHECK(args[0]->IsObject());
231232
CHECK(args[1]->IsNumber());
232-
CHECK(args[2]->IsObject());
233+
CHECK(args.Length() == 2 || args[2]->IsObject());
233234

234235
Isolate* isolate = args.GetIsolate();
235236
DestroyParam* p = new DestroyParam();
236237
p->asyncId = args[1].As<Number>()->Value();
237238
p->env = Environment::GetCurrent(args);
238239
p->target.Reset(isolate, args[0].As<Object>());
239-
p->propBag.Reset(isolate, args[2].As<Object>());
240+
if (args.Length() > 2) {
241+
p->propBag.Reset(isolate, args[2].As<Object>());
242+
}
240243
p->target.SetWeak(p, AsyncWrap::WeakCallback, WeakCallbackType::kParameter);
241244
p->env->AddCleanupHook(DestroyParamCleanupHook, p);
242245
}

typings/internalBinding/async_wrap.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ declare function InternalBinding(binding: 'async_wrap'): {
107107
promiseAfterHook: InternalAsyncWrapBinding.PromiseHook | undefined,
108108
promiseResolveHook: InternalAsyncWrapBinding.PromiseHook | undefined
109109
): void;
110-
registerDestroyHook(promise: Promise<unknown>, asyncId: number, destroyed: { destroyed: boolean }): void;
110+
registerDestroyHook(resource: object, asyncId: number, destroyed?: { destroyed: boolean }): void;
111111
async_hook_fields: Uint32Array;
112112
async_id_fields: Float64Array;
113113
async_ids_stack: Float64Array;

0 commit comments

Comments
 (0)