Skip to content

Commit 148383f

Browse files
Flarnaaddaleax
authored andcommittedSep 22, 2020
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 <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2cf2316 commit 148383f

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
@@ -250,6 +250,7 @@ const storageHook = createHook({
250250
}
251251
});
252252

253+
const defaultAlsResourceOpts = { requireManualDestroy: true };
253254
class AsyncLocalStorage {
254255
constructor() {
255256
this.kResourceStore = Symbol('kResourceStore');
@@ -290,8 +291,11 @@ class AsyncLocalStorage {
290291
if (ObjectIs(store, this.getStore())) {
291292
return callback(...args);
292293
}
293-
const resource = new AsyncResource('AsyncLocalStorage');
294-
return resource.runInAsyncScope(() => {
294+
const resource = new AsyncResource('AsyncLocalStorage',
295+
defaultAlsResourceOpts);
296+
// Calling emitDestroy before runInAsyncScope avoids a try/finally
297+
// It is ok because emitDestroy only schedules calling the hook
298+
return resource.emitDestroy().runInAsyncScope(() => {
295299
this.enterWith(store);
296300
return callback(...args);
297301
});

0 commit comments

Comments
 (0)
Please sign in to comment.