diff --git a/lib/async_hooks.js b/lib/async_hooks.js index cb08993c2da22b..f558a6dcb234b1 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -250,6 +250,7 @@ const storageHook = createHook({ } }); +const defaultAlsResourceOpts = { requireManualDestroy: true }; class AsyncLocalStorage { constructor() { this.kResourceStore = Symbol('kResourceStore'); @@ -290,8 +291,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); });