Skip to content

Commit b304df9

Browse files
QardBridgeAR
authored andcommitted
async_hooks: prevent sync methods of async storage exiting outer context
PR-URL: #31950 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent c6078f0 commit b304df9

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎lib/async_hooks.js‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,11 @@ class AsyncLocalStorage {
257257
}
258258

259259
runSyncAndReturn(store,callback, ...args){
260-
constresource=executionAsyncResource();
261-
constouterStore=resource[this.kResourceStore];
262-
this.enterWith(store);
263-
try{
260+
constresource=newAsyncResource('AsyncLocalStorage');
261+
returnresource.runInAsyncScope(()=>{
262+
this.enterWith(store);
264263
returncallback(...args);
265-
}finally{
266-
resource[this.kResourceStore]=outerStore;
267-
}
264+
});
268265
}
269266

270267
exitSyncAndReturn(callback, ...args){
@@ -287,11 +284,10 @@ class AsyncLocalStorage {
287284
}
288285

289286
run(store,callback, ...args){
290-
constresource=executionAsyncResource();
291-
constouterStore=resource[this.kResourceStore];
292-
this.enterWith(store);
293-
process.nextTick(callback, ...args);
294-
resource[this.kResourceStore]=outerStore;
287+
process.nextTick(()=>{
288+
this.enterWith(store);
289+
returncallback(...args);
290+
});
295291
}
296292

297293
exit(callback, ...args){
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{
5+
AsyncLocalStorage,
6+
executionAsyncResource
7+
}=require('async_hooks');
8+
9+
constasyncLocalStorage=newAsyncLocalStorage();
10+
11+
constouterResource=executionAsyncResource();
12+
13+
asyncLocalStorage.run(newMap(),()=>{
14+
assert.notStrictEqual(executionAsyncResource(),outerResource);
15+
});
16+
17+
assert.strictEqual(executionAsyncResource(),outerResource);

0 commit comments

Comments
 (0)