question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: promise handling can result in infinite loops

See original GitHub issue

Hi! 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 geting 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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Nov 1, 2022

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.)

1reaction
matthewhuang97commented, Oct 18, 2022

Our contract is when atomState.p finalizes, we should either put a new promise or .v or .e. I may revisit/refactor the code, but the logic should be the same.

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise cause to stackoverflow with infinity while loop
Yes, because you're still adding infinite amount of handlers. And they never execute because the infinite loop doesn't let the promise ever ...
Read more >
JavaScript Mistake — Loops, Promises, and More
A for loop with an ending condition that'll never be reached is probably buggy code. If we want to make an infinite loop,...
Read more >
Iteration and Flow Control - Practical Modern JavaScript [Book]
Promises offer a different way of attacking asynchronous code flows. Iterators dictate how an object is iterated, producing the sequence of values that...
Read more >
How to Solve the Infinite Loop of React.useEffect()
useEffect () hook because it can generate infinite loops. ... and the onChange event handler updates the value state when user types into...
Read more >
no-await-in-loop - ESLint - Pluggable JavaScript Linter
Usually, the code should be refactored to create all the promises at once, then get access to the results using Promise.all() .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found