async_hooks: use resource stack for AsyncLocalStorage run

PR-URL: https://github.com/nodejs/node/pull/39890
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
This commit is contained in:
Stephen Belanger 2021-08-25 22:22:38 -07:00 committed by Node.js GitHub Bot
parent 508890d795
commit 926152a38c
3 changed files with 33 additions and 38 deletions

View file

@ -263,7 +263,6 @@ const storageHook = createHook({
}
});
const defaultAlsResourceOpts = { requireManualDestroy: true };
class AsyncLocalStorage {
constructor() {
this.kResourceStore = Symbol('kResourceStore');
@ -309,14 +308,19 @@ class AsyncLocalStorage {
if (ObjectIs(store, this.getStore())) {
return ReflectApply(callback, null, args);
}
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);
this._enable();
const resource = executionAsyncResource();
const oldStore = resource[this.kResourceStore];
resource[this.kResourceStore] = store;
try {
return ReflectApply(callback, null, args);
});
} finally {
resource[this.kResourceStore] = oldStore;
}
}
exit(callback, ...args) {