-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(persist): prevent race condition during concurrent rehydrate calls #3336
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(persist): prevent race condition during concurrent rehydrate calls #3336
Conversation
Added hydrationVersion counter to track and invalidate stale hydrations. When multiple rehydrate() calls happen concurrently, only the last one applies its state, preventing race conditions with async storage. - Added hydrationVersion counter incremented on each hydrate() call - Added version checks after each async step (getItem, migrate, set) - Added 3 tests for concurrent rehydration scenarios
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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. |
|
If needed, I can provide a minimal reproduction sandbox. |
dai-shi
left a comment
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.
Thanks for the suggestion, and working on it. I may need to take a second look.
I wonder if anyone else would like to give a review.
|
Thanks a lot for the review and for looping in another reviewer! |
dai-shi
left a comment
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.
simplify further?
…on' into fix/persist-hydration-race-condition
dai-shi
left a comment
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.
LGTM
Now, it's readable. Thanks for your contribution.
I'll merge this PR when I prepare a new release. Maybe next year. Anyone can feel free to give a review until then.
|
Thank you for the thorough review and patience! 🙏 Learned a lot from this process. Happy to contribute more in the future! 🚀 |
|
@Niyaz-Mazhitov @dai-shi LGTM |
Bumps [zustand](https://github.com/pmndrs/zustand) from 5.0.9 to 5.0.10. Release notes *Sourced from [zustand's releases](https://github.com/pmndrs/zustand/releases).* > v5.0.10 > ------- > > This version includes a fix to the `persist` middleware for an edge case. > > What's Changed > -------------- > > * fix(persist): prevent race condition during concurrent rehydrate calls by [`@Niyaz-Mazhitov`](https://github.com/Niyaz-Mazhitov) in [pmndrs/zustand#3336](https://redirect.github.com/pmndrs/zustand/pull/3336) > > New Contributors > ---------------- > > * [`@max-programming`](https://github.com/max-programming) made their first contribution in [pmndrs/zustand#3310](https://redirect.github.com/pmndrs/zustand/pull/3310) > * [`@oleksandr-danylchenko`](https://github.com/oleksandr-danylchenko) made their first contribution in [pmndrs/zustand#3319](https://redirect.github.com/pmndrs/zustand/pull/3319) > * [`@MateuszSobiech`](https://github.com/MateuszSobiech) made their first contribution in [pmndrs/zustand#3334](https://redirect.github.com/pmndrs/zustand/pull/3334) > * [`@EduardoRangelG`](https://github.com/EduardoRangelG) made their first contribution in [pmndrs/zustand#3326](https://redirect.github.com/pmndrs/zustand/pull/3326) > * [`@1mehdifaraji`](https://github.com/1mehdifaraji) made their first contribution in [pmndrs/zustand#3339](https://redirect.github.com/pmndrs/zustand/pull/3339) > * [`@kamja44`](https://github.com/kamja44) made their first contribution in [pmndrs/zustand#3349](https://redirect.github.com/pmndrs/zustand/pull/3349) > * [`@Niyaz-Mazhitov`](https://github.com/Niyaz-Mazhitov) made their first contribution in [pmndrs/zustand#3336](https://redirect.github.com/pmndrs/zustand/pull/3336) > > **Full Changelog**: <pmndrs/zustand@v5.0.9...v5.0.10> Commits * [`003b0a7`](pmndrs/zustand@003b0a7) 5.0.10 * [`9bb7f3b`](pmndrs/zustand@9bb7f3b) chore(deps): update dev dependencies ([#3353](https://redirect.github.com/pmndrs/zustand/issues/3353)) * [`d0cb549`](pmndrs/zustand@d0cb549) fix(persist): prevent race condition during concurrent rehydrate calls ([#3336](https://redirect.github.com/pmndrs/zustand/issues/3336)) * [`59e8783`](pmndrs/zustand@59e8783) test: remove eslint-disable for no-unused-expressions ([#3349](https://redirect.github.com/pmndrs/zustand/issues/3349)) * [`db6fe66`](pmndrs/zustand@db6fe66) docs(CONTRIBUTING): fix 'npm install' to 'pnpm install' ([#3350](https://redirect.github.com/pmndrs/zustand/issues/3350)) * [`8f1d45a`](pmndrs/zustand@8f1d45a) Fix formatting and punctuation in third-party libraries doc ([#3346](https://redirect.github.com/pmndrs/zustand/issues/3346)) * [`74d0aec`](pmndrs/zustand@74d0aec) Add zustand-mutable middleware link ([#3343](https://redirect.github.com/pmndrs/zustand/issues/3343)) * [`f912630`](pmndrs/zustand@f912630) Add zustand-mmkv-storage to third-party libraries ([#3339](https://redirect.github.com/pmndrs/zustand/issues/3339)) * [`4f1f963`](pmndrs/zustand@4f1f963) docs: add memoized selector usage with `useStore` ([#3320](https://redirect.github.com/pmndrs/zustand/issues/3320)) * [`aa2da00`](pmndrs/zustand@aa2da00) docs: fix navigation order for comparison and tutorial markdowns ([#3326](https://redirect.github.com/pmndrs/zustand/issues/3326)) * Additional commits viewable in [compare view](pmndrs/zustand@v5.0.9...v5.0.10) [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- Dependabot commands and options You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Summary
Fixes race condition in persist middleware when multiple
rehydrate()calls happen concurrently.Problem
When using async storage, if
rehydrate()is called multiple times before the first hydration completes, all hydrations run in parallel and results are applied in unpredictable order. This causes:onFinishHydrationcallbacks firing multiple timesSolution
Added a
hydrationVersioncounter that:hydrate()callThis ensures only the most recent
rehydrate()call applies its state.Test plan
rehydrate()calls (only last one wins)