-
Notifications
You must be signed in to change notification settings - Fork 326
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
π Bug Report β Runtime APIs: The AsyncContext of AsynLocalStorages gets lost in custom thenables #1513
Comments
Ah, yes, custom thenables do not currently automatically propagate the async context. You'll need to use const thenable = {
then: AsyncLocalStorage.bind(function (onfulfilled) {
als.getStore();
})
}; |
@jasnell I see but I assume that this can be improved/fixed? is propagating the async context for thenables something potentially problematic/complex? As you can see in cloudflare/next-on-pages#499 the problem is that libraries can use thenables so application authors could be unable use the |
It requires a fix in v8 to get working. We can patch v8 to get it working but I've been waiting on a larger change coming for the implementation of the standardized |
@jasnell yeah that would be awesome since I have seen a few developers hitting this issue π Hopefully it's not too too much work/complex to implement such temporary patch? π€ |
Any suggested workaround meanwhile? I am unable to use Prisma on CloudflareWorkers currently. |
The immediate workaround is to use For example... (this doesn't use a thenable but the idea should be clear)...
Essentially, your custom thenable would use |
@jasnell I assume that the V8 change hasn't landed yet? π Regarding the workaround, as I mentioned here: cloudflare/next-on-pages#499 (comment) I think that unfortunately the workaround is a bit impractical when thenables come from libraries. Since people keep hitting this issue I would be curious to know if there's an ETA for the V8 change, alternatively as I mentioned in my next-on-pages comment maybe we could there do some hacks and try to patch all thenables present in the code, but before we consider doing that I'd be curious about the effort required in workerd to patch this and the brittleness of the solution since, as I mentioned, a next-on-pages hack would be:
So I'm wondering, if we want to fix this, where it would make more sense to do so (I'm definitely leaning towards workerd but as I mentioned I don't really know what the fix there implies) |
No ETA yet on the v8 change. I'll look at it today. The change definitely needs to be made in v8. We don't have the necessary visibility in to the necessary bits within v8 to apply a reasonable workaround in workerd. |
To clarify, when I say this, I mean that the limitation here is within v8 itself and to fix it requires a patch to v8. We can float a patch that addresses this but ultimately, long term, it's something that v8 needs to address. My plan currently is to float a patch and I have one in testing internally. Assuming that looks good I'll be able to open a PR here. |
Oh I see. Thanks for looking so deeply into this. Do you think there is long-term merit in using JavascriptCore instead of V8? It would reduce resource utilisation on Cloudflare Edge Network right? |
Not necessarily. While we've always had a Side Quest in mind of making workers somewhat agnostic of the js runtime the reality is that v8 is a hard dependency. It's not clear at all that switching to something like JSC would yield enough of a realized benefit to justify the development effort it would take. This issue around the async context propagation, on its own, certainly would not be enough justification. |
Quick update... I've got the v8 patch put together and internal testing is showing that it's good. I want to get some review before I open the PR that adds the patch here. But, progress! @Qard ... just a heads up that I was able to use your older patch as a roadmap here. When I open the PR here, the patch should contain everything you need to move forward to upstream the updated patch in v8 itself. |
Nice! That was quick work. π |
waiting for the patch, thanks for your hard work! |
Waiting for the patch as well - Hope it gets fixed soon! |
Fixed by #1665 which was merged yesterday. Likely will go to production some time next week. |
Going to go ahead and close this issue now since the fix has landed. As Kenton mentioned, if all goes well that'll end up in production likely by next week. |
We @prisma would appreciate a further comment here when this has been rolled out to production, so we can confirm and then remove documentation for the current blocking problem. Thanks. |
This is now in production. |
If I define a custom thenable and try to access the store of an
AsyncLocalStorage
in thethen
method, the store seems to beundefined
although it shouldn't be.Basically:
minimal reproduction
The text was updated successfully, but these errors were encountered: