Skip to content

Commit 014feec

Browse files
Flarnajasnell
authored andcommitted
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: #34653 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 33505e2 commit 014feec

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/async_hooks.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ const storageHook = createHook({
253253
}
254254
});
255255

256+
const defaultAlsResourceOpts = { requireManualDestroy: true };
256257
class AsyncLocalStorage {
257258
constructor() {
258259
this.kResourceStore = Symbol('kResourceStore');
@@ -293,8 +294,11 @@ class AsyncLocalStorage {
293294
if (ObjectIs(store, this.getStore())) {
294295
return callback(...args);
295296
}
296-
const resource = new AsyncResource('AsyncLocalStorage');
297-
return resource.runInAsyncScope(() => {
297+
const resource = new AsyncResource('AsyncLocalStorage',
298+
defaultAlsResourceOpts);
299+
// Calling emitDestroy before runInAsyncScope avoids a try/finally
300+
// It is ok because emitDestroy only schedules calling the hook
301+
return resource.emitDestroy().runInAsyncScope(() => {
298302
this.enterWith(store);
299303
return callback(...args);
300304
});

0 commit comments

Comments
 (0)