-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async_hooks: fix async/await context loss in AsyncLocalStorage #33189
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const { | ||
NumberIsSafeInteger, | ||
PromiseResolve, | ||
ReflectApply, | ||
Symbol, | ||
} = primordials; | ||
|
@@ -211,19 +212,73 @@ class AsyncResource { | |
} | ||
|
||
const storageList = []; | ||
const seenLayer = []; | ||
let trackerCount = 0; | ||
let depth = 0; | ||
|
||
function refreshStorageHooks() { | ||
if (storageList.length === 0) { | ||
storageHookWithTracking.disable(); | ||
storageHook.disable(); | ||
} else if (trackerCount > 0) { | ||
storageHookWithTracking.enable(); | ||
storageHook.disable(); | ||
} else { | ||
storageHookWithTracking.disable(); | ||
storageHook.enable(); | ||
} | ||
} | ||
|
||
function patchPromiseBarrier(currentResource) { | ||
PromiseResolve({ | ||
then(resolve) { | ||
const resource = executionAsyncResource(); | ||
propagateToStorageLists(resource, currentResource); | ||
resolve(); | ||
} | ||
}); | ||
} | ||
|
||
function propagateToStorageLists(resource, currentResource) { | ||
for (let i = 0; i < storageList.length; ++i) { | ||
storageList[i]._propagate(resource, currentResource); | ||
} | ||
} | ||
|
||
const storageHook = createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
const currentResource = executionAsyncResource(); | ||
// Value of currentResource is always a non null object | ||
for (let i = 0; i < storageList.length; ++i) { | ||
storageList[i]._propagate(resource, currentResource); | ||
propagateToStorageLists(resource, currentResource); | ||
} | ||
}); | ||
|
||
const storageHookWithTracking = createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
const currentResource = executionAsyncResource(); | ||
// Value of currentResource is always a non null object | ||
propagateToStorageLists(resource, currentResource); | ||
|
||
if (type === 'PROMISE' && !seenLayer[depth]) { | ||
seenLayer[depth] = true; | ||
patchPromiseBarrier(currentResource); | ||
} | ||
}, | ||
|
||
before(asyncId) { | ||
depth++; | ||
seenLayer[depth] = false; | ||
}, | ||
|
||
after(asyncId) { | ||
depth--; | ||
} | ||
}); | ||
|
||
class AsyncLocalStorage { | ||
constructor() { | ||
constructor({ trackAsyncAwait = false } = {}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not blocking comment: I'm not a big fan of user facing options which could be even named |
||
this.kResourceStore = Symbol('kResourceStore'); | ||
this.trackAsyncAwait = trackAsyncAwait; | ||
this.enabled = false; | ||
} | ||
|
||
|
@@ -232,9 +287,10 @@ class AsyncLocalStorage { | |
this.enabled = false; | ||
// If this.enabled, the instance must be in storageList | ||
storageList.splice(storageList.indexOf(this), 1); | ||
if (storageList.length === 0) { | ||
storageHook.disable(); | ||
if (this.trackAsyncAwait) { | ||
trackerCount--; | ||
} | ||
refreshStorageHooks(); | ||
} | ||
} | ||
|
||
|
@@ -250,7 +306,10 @@ class AsyncLocalStorage { | |
if (!this.enabled) { | ||
this.enabled = true; | ||
storageList.push(this); | ||
storageHook.enable(); | ||
if (this.trackAsyncAwait) { | ||
trackerCount++; | ||
} | ||
refreshStorageHooks(); | ||
} | ||
const resource = executionAsyncResource(); | ||
resource[this.kResourceStore] = store; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
|
||
const store = new AsyncLocalStorage({ trackAsyncAwait: true }); | ||
let checked = 0; | ||
|
||
function thenable(expected, count) { | ||
return { | ||
then: common.mustCall((cb) => { | ||
assert.strictEqual(expected, store.getStore()); | ||
checked++; | ||
cb(); | ||
}, count) | ||
}; | ||
} | ||
|
||
function main(n) { | ||
const firstData = Symbol('first-data'); | ||
const secondData = Symbol('second-data'); | ||
|
||
const first = thenable(firstData, 1); | ||
const second = thenable(secondData, 1); | ||
const third = thenable(firstData, 2); | ||
|
||
return store.run(firstData, common.mustCall(async () => { | ||
assert.strictEqual(firstData, store.getStore()); | ||
await first; | ||
|
||
await store.run(secondData, common.mustCall(async () => { | ||
assert.strictEqual(secondData, store.getStore()); | ||
await second; | ||
assert.strictEqual(secondData, store.getStore()); | ||
})); | ||
|
||
await Promise.all([ third, third ]); | ||
assert.strictEqual(firstData, store.getStore()); | ||
})); | ||
} | ||
|
||
const outerData = Symbol('outer-data'); | ||
|
||
Promise.all([ | ||
store.run(outerData, () => Promise.resolve(thenable(outerData))), | ||
Promise.resolve(3).then(common.mustCall(main)), | ||
main(1), | ||
main(2) | ||
]).then(common.mustCall(() => { | ||
assert.strictEqual(checked, 13); | ||
})); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m -1 to this fix. This is going to slow everything down because of the use use of before and after hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking a config option like
trackAsyncAwait
which would switch between two hook sets so it only does the before/after if a user ofAsyncLocalStorage
has explicitly requested it. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I think we should investigate if there is another possible fix first, this API is extremely nice and losing so much perf would not be good for the ecosystem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I'm trying to figure out a lower-level fix. I just made this as a possible higher-level solution for now, until we can come up with something better. Agreed it's not great though, needing the extra before/after hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that this problem is not new of AL, I don't think this should be rushed in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's fine. Just putting something up that I can iterate on. If the possible lower-level fix is what it needs to be to land, so be it. :)