mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
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>
This commit is contained in:
1 parent
c6078f0ca1
commit
b304df97ff
2 files changed
+25
-12
No files matched your search
+8
-12
@@ -257,14 +257,11 @@ class AsyncLocalStorage {
|
||||
}
|
||||
|
||||
runSyncAndReturn(store, callback, ...args) {
|
||||
const resource = executionAsyncResource();
|
||||
const outerStore = resource[this.kResourceStore];
|
||||
this.enterWith(store);
|
||||
try {
|
||||
const resource = new AsyncResource('AsyncLocalStorage');
|
||||
return resource.runInAsyncScope(() => {
|
||||
this.enterWith(store);
|
||||
return callback(...args);
|
||||
} finally {
|
||||
resource[this.kResourceStore] = outerStore;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
exitSyncAndReturn(callback, ...args) {
|
||||
@@ -287,11 +284,10 @@ class AsyncLocalStorage {
|
||||
}
|
||||
|
||||
run(store, callback, ...args) {
|
||||
const resource = executionAsyncResource();
|
||||
const outerStore = resource[this.kResourceStore];
|
||||
this.enterWith(store);
|
||||
process.nextTick(callback, ...args);
|
||||
resource[this.kResourceStore] = outerStore;
|
||||
process.nextTick(() => {
|
||||
this.enterWith(store);
|
||||
return 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);
|
||||
Reference in new issue
Block a user