async_hooks: fix ctx loss after nested ALS calls

PR-URL: https://github.com/nodejs/node/pull/32085
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
Andrey Pechkurov 2020-03-04 14:57:56 +03:00 committed by Michael Dawson
parent 434d39dd67
commit 86ab4ee6e4
3 changed files with 50 additions and 23 deletions

View file

@ -255,23 +255,21 @@ class AsyncLocalStorage {
resource[this.kResourceStore] = store;
}
_exit() {
const resource = executionAsyncResource();
if (resource) {
resource[this.kResourceStore] = undefined;
}
}
runSyncAndReturn(store, callback, ...args) {
const resource = executionAsyncResource();
const outerStore = resource[this.kResourceStore];
this._enter(store);
try {
return callback(...args);
} finally {
this._exit();
resource[this.kResourceStore] = outerStore;
}
}
exitSyncAndReturn(callback, ...args) {
if (!this.enabled) {
return callback(...args);
}
this.enabled = false;
try {
return callback(...args);
@ -288,12 +286,17 @@ class AsyncLocalStorage {
}
run(store, callback, ...args) {
const resource = executionAsyncResource();
const outerStore = resource[this.kResourceStore];
this._enter(store);
process.nextTick(callback, ...args);
this._exit();
resource[this.kResourceStore] = outerStore;
}
exit(callback, ...args) {
if (!this.enabled) {
return process.nextTick(callback, ...args);
}
this.enabled = false;
process.nextTick(callback, ...args);
this.enabled = true;