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); });