diff --git a/lib/async_hooks.js b/lib/async_hooks.js index cb017222a9bce2..dadde6a0f2e7ad 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -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) { diff --git a/test/async-hooks/test-async-local-storage-run-resource.js b/test/async-hooks/test-async-local-storage-run-resource.js new file mode 100644 index 00000000000000..9a7479f699246c --- /dev/null +++ b/test/async-hooks/test-async-local-storage-run-resource.js @@ -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);