Skip to content

Commit

Permalink
async_hooks: avoid GC tracking of AsyncResource in ALS
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Flarna authored and addaleax committed Aug 8, 2020
1 parent e89ec46 commit 6502489
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ const storageHook = createHook({
}
});

const defaultAlsResourceOpts = { requireManualDestroy: true };
class AsyncLocalStorage {
constructor() {
this.kResourceStore = Symbol('kResourceStore');
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 6502489

Please sign in to comment.