Skip to content

Commit

Permalink
async_hooks: prevent sync methods of async storage exiting outer context
Browse files Browse the repository at this point in the history
PR-URL: #31950
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
Qard authored and BridgeAR committed Apr 28, 2020
1 parent c6078f0 commit b304df9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions test/async-hooks/test-async-local-storage-run-resource.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit b304df9

Please sign in to comment.