Skip to content
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

fix(vanilla): deal with promise resolving race condition #2199

Merged
merged 6 commits into from
Oct 23, 2023

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Oct 19, 2023

Related Issues or Discussions

#2192

Related Issue

#2191

Summary

Adds tests for the issue linked above.

Description of Issue

A primitive atom (atomP) exists and has a value (valueP)
An async atom (atomA) gets the value (valueAP) from atomP and then has a delay (delayA) before returning the value from atomP.
A derived async atom (atomB) gets the value (valueBP) from atomP and after a microtask or greater delay (delayB) it gets the value (valueBA) from atomA.
The bug requires delayB < delayA.
So,

  • atomP is a synchronous dependency of atomA and atomB
  • atomA is an asynchronous dependency of atomB

After all atoms mount,
atomP is changed twice with a delay (delayC) less than delayA.
This first multi-change proceeds nominally. ✅

atomP is then changed twice again with a delay (delayD) less than delayA.
This second multi-change does NOT proceed nominally.
The first value of this multi-change is used for ALL subsequent values of this multi-change.
Therefore, subsequent values use a stale value. ❌

Timing

See below for this timing in depth:

init valueP to 0
inside atomB.read: before await valueBA, valueBP is 0
change valueP to 1
inside atomB.read: before await valueBA, valueBP is 1
change valueP to 2
inside atomB.read: before await valueBA, valueBP is 2

inside atomA.read: after delayA, valueAP is 0
inside atomA.read: after delayA, valueAP is 1
inside atomA.read: after delayA, valueAP is 2

inside atomB.read: before await valueBA, valueBP is 2, valueBA is 2
inside atomB.read: before await valueBA, valueBP is 1, valueBA is 2
inside atomB.read: before await valueBA, valueBP is 0, valueBA is 2
===== above is nominal ✅ =====

change valueP to 3
inside atomB.read: before await valueBA, valueBP is 3
change valueP to 4
inside atomB.read: before await valueBA, valueBP is 4
inside atomA.read: after delayA, valueAP is 3
inside atomA.read: after delayA, valueAP is 4
inside atomB.read: before await valueBA, valueBP is 3, valueBA is 3 // Error: should be 4
inside atomB.read: before await valueBA, valueBP is 4, valueBA is 3 // Error: should be 4
===== above is anomalous ❌ =====

Theory

Likely, as a performance optimization there is logic in store.ts to reuse the existing value. The bug is probably due to caching the incorrect value.

The microsecond delay (delayB) in atomB is significant for some reason. Not sure why yet.

The store.sub is also significant for some reason.

Unit Test

I'm having trouble getting a unit test to work with just the store. Subsequent fires of setCount(increment) do not cause asyncAtom.read to refire unless I use a React hook. Not sure why.

It would be preferable to write a unit test and not depend on the React library for testing core Jotai functionality.

Check List

  • yarn run prettier for formatting code and docs

https://codesandbox.io/s/jovial-kepler-d9v27h

Screen.Recording.2023-10-17.at.7.46.48.PM.mov

@vercel
Copy link

vercel bot commented Oct 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2023 1:10pm

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 19, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 10ea181:

Sandbox Source
React Configuration
React TypeScript Configuration
React Browserify Configuration
React Snowpack Configuration
Next.js Configuration
Next.js with custom Babel config Configuration
React with custom Babel config Configuration
boring-shape-x4d8sr PR

@dai-shi
Copy link
Member

dai-shi commented Oct 19, 2023

Thanks. It would be really nice to have store api tests.

Also, could you avoid renderHook somehow? All of our tests never depend on it.

tests/react/async.test.tsx Outdated Show resolved Hide resolved
tests/react/async.test.tsx Outdated Show resolved Hide resolved
tests/react/async.test.tsx Outdated Show resolved Hide resolved
tests/react/async.test.tsx Outdated Show resolved Hide resolved
Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for minor requests. I will do the rest afterwards.

tests/react/async.test.tsx Outdated Show resolved Hide resolved
tests/react/async.test.tsx Outdated Show resolved Hide resolved
@dai-shi dai-shi changed the title bug: add tests for derived async gets stale value fix(vanilla): deal with promise resolving race condition Oct 23, 2023
@dai-shi dai-shi added this to the v2.5.0 milestone Oct 23, 2023
Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for your help. This must be a huge bug fix.

@dmaskasky
Copy link
Collaborator Author

Thanks so much for your help. This must be a huge bug fix.

Thanks for your help. Merge is blocked for me, kindly merge at the next opportunity.

@dai-shi dai-shi merged commit ac038ac into pmndrs:main Oct 23, 2023
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants