bug: promise handling can result in infinite loops
See original GitHub issueHi! Thanks for all the great work on Jotai.
I noticed some pretty severe breaking behavior in using async values in atoms. In particular, store.ts
has two usages of the same pattern that result in an infinite loop:
In writeGetter
: https://github.com/pmndrs/jotai/blob/0e610f920d65f18830e5769789ca9682add25c72/src/core/store.ts#L659-L666
In asyncGet
: https://github.com/pmndrs/jotai/blob/0e610f920d65f18830e5769789ca9682add25c72/src/core/store.ts#L986-L987
In each of these, what happens is that we call the parent function, check if atomState.p
exists, and then calls p.then(() => writeGetter(atom))
again to invoke the parent function. The next time around, atomState.p
still exists, so we end up in an infinite loop.
I’m not sure whether this is an oversight, or whether there is supposed to be code responsible for clearing out atomState.p
. I think the repro was pretty simple, in that I just tried get
ing an atom whose value was a pending promise. As far as I can tell from the code, this would result in an infinite loop any time this happens? Let me know how I can help!
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Thanks for the reproduction! That looks pretty basic… While this can be considered a bug, and anyone interested can challenge, I think it’s pretty tough. Meanwhile, I would suggest avoiding using infinite promise at all with loadable.
(btw, my recent focus is on a new API #1514, and with it, hopefully loadable will be more stable without hacks.)
Can you show me where this code lives? I might be able to dig in a bit and help out if I have spare bandwidth!