From 014feecc445c7dfc754378f2626cd43ee30a448d Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Mon, 3 Aug 2020 23:46:49 +0200 Subject: [PATCH] async_hooks: avoid GC tracking of AsyncResource in ALS Manually destroy the AsyncResource created by AsyncLocalStore.run() to avoid unneeded GC tracking in case a destroy hooks is present. PR-URL: https://github.com/nodejs/node/pull/34653 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- lib/async_hooks.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 758c4eab10fe83..7dd888b61f79f9 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -253,6 +253,7 @@ const storageHook = createHook({ } }); +const defaultAlsResourceOpts = { requireManualDestroy: true }; class AsyncLocalStorage { constructor() { this.kResourceStore = Symbol('kResourceStore'); @@ -293,8 +294,11 @@ class AsyncLocalStorage { if (ObjectIs(store, this.getStore())) { return callback(...args); } - const resource = new AsyncResource('AsyncLocalStorage'); - return resource.runInAsyncScope(() => { + const resource = new AsyncResource('AsyncLocalStorage', + defaultAlsResourceOpts); + // Calling emitDestroy before runInAsyncScope avoids a try/finally + // It is ok because emitDestroy only schedules calling the hook + return resource.emitDestroy().runInAsyncScope(() => { this.enterWith(store); return callback(...args); });