Skip to content

Commit a683e87

Browse files
QardBethGriggs
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 4e5ec41 commit a683e87

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

lib/async_hooks.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,11 @@ class AsyncLocalStorage {
257257
}
258258

259259
runSyncAndReturn(store, callback, ...args) {
260-
const resource = executionAsyncResource();
261-
const outerStore = resource[this.kResourceStore];
262-
this.enterWith(store);
263-
try {
260+
const resource = new AsyncResource('AsyncLocalStorage');
261+
return resource.runInAsyncScope(() => {
262+
this.enterWith(store);
264263
return callback(...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-
const resource = executionAsyncResource();
291-
const outerStore = 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+
return callback(...args);
290+
});
295291
}
296292

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

0 commit comments

Comments
 (0)