mirror of https://github.com/nodejs/node.git
async_hooks: prevent sync methods of async storage exiting outer context
PR-URL: https://github.com/nodejs/node/pull/31950 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>pull/32744/merge
parent
26924faa54
commit
f7f0441997
|
@ -257,14 +257,11 @@ class AsyncLocalStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
runSyncAndReturn(store, callback, ...args) {
|
runSyncAndReturn(store, callback, ...args) {
|
||||||
const resource = executionAsyncResource();
|
const resource = new AsyncResource('AsyncLocalStorage');
|
||||||
const outerStore = resource[this.kResourceStore];
|
return resource.runInAsyncScope(() => {
|
||||||
this.enterWith(store);
|
this.enterWith(store);
|
||||||
try {
|
|
||||||
return callback(...args);
|
return callback(...args);
|
||||||
} finally {
|
});
|
||||||
resource[this.kResourceStore] = outerStore;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exitSyncAndReturn(callback, ...args) {
|
exitSyncAndReturn(callback, ...args) {
|
||||||
|
@ -287,11 +284,10 @@ class AsyncLocalStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
run(store, callback, ...args) {
|
run(store, callback, ...args) {
|
||||||
const resource = executionAsyncResource();
|
process.nextTick(() => {
|
||||||
const outerStore = resource[this.kResourceStore];
|
this.enterWith(store);
|
||||||
this.enterWith(store);
|
return callback(...args);
|
||||||
process.nextTick(callback, ...args);
|
});
|
||||||
resource[this.kResourceStore] = outerStore;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(callback, ...args) {
|
exit(callback, ...args) {
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
'use strict';
|
||||||
|
require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const {
|
||||||
|
AsyncLocalStorage,
|
||||||
|
executionAsyncResource
|
||||||
|
} = require('async_hooks');
|
||||||
|
|
||||||
|
const asyncLocalStorage = new AsyncLocalStorage();
|
||||||
|
|
||||||
|
const outerResource = executionAsyncResource();
|
||||||
|
|
||||||
|
asyncLocalStorage.run(new Map(), () => {
|
||||||
|
assert.notStrictEqual(executionAsyncResource(), outerResource);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(executionAsyncResource(), outerResource);
|
Loading…
Reference in New Issue