Skip to content
Permalink
Browse files

async_hooks: fix ctx loss after nested ALS calls

PR-URL: #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>
  • Loading branch information
puzpuzpuz authored and mhdawson committed Mar 4, 2020
1 parent 434d39d commit 86ab4ee6e49681fb4a9603128af4f6fa29806183
@@ -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;
@@ -12,8 +12,16 @@ asyncLocalStorage.runSyncAndReturn(new Map(), () => {
process.nextTick(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});

asyncLocalStorage.disable();
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

// Calls to exit() should not mess with enabled status
asyncLocalStorage.exit(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

process.nextTick(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
asyncLocalStorage.runSyncAndReturn(new Map(), () => {
@@ -4,19 +4,35 @@ const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();
const outer = {};
const inner = {};

setTimeout(() => {
asyncLocalStorage.run(new Map(), () => {
const asyncLocalStorage2 = new AsyncLocalStorage();
asyncLocalStorage2.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
const store2 = asyncLocalStorage2.getStore();
store.set('hello', 'world');
store2.set('hello', 'foo');
setTimeout(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
}, 200);
});
function testInner() {
assert.strictEqual(asyncLocalStorage.getStore(), outer);

asyncLocalStorage.run(inner, () => {
assert.strictEqual(asyncLocalStorage.getStore(), inner);
});
assert.strictEqual(asyncLocalStorage.getStore(), outer);

asyncLocalStorage.exit(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});
assert.strictEqual(asyncLocalStorage.getStore(), outer);

asyncLocalStorage.runSyncAndReturn(inner, () => {
assert.strictEqual(asyncLocalStorage.getStore(), inner);
});
}, 100);
assert.strictEqual(asyncLocalStorage.getStore(), outer);

asyncLocalStorage.exitSyncAndReturn(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});
assert.strictEqual(asyncLocalStorage.getStore(), outer);
}

asyncLocalStorage.run(outer, testInner);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

asyncLocalStorage.runSyncAndReturn(outer, testInner);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

0 comments on commit 86ab4ee

Please sign in to comment.
You can’t perform that action at this time.