Skip to content

Conversation

@Niyaz-Mazhitov
Copy link
Contributor

@Niyaz-Mazhitov Niyaz-Mazhitov commented Dec 27, 2025

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:

  • Stale state being applied over newer state
  • onFinishHydration callbacks firing multiple times
  • Inconsistent state after hydration

Solution

Added a hydrationVersion counter that:

  • Increments at the start of each hydrate() call
  • Is checked after each async step (getItem, migrate, before applying state)
  • Aborts the hydration if a newer one has started

This ensures only the most recent rehydrate() call applies its state.

Test plan

  • Added test: multiple concurrent rehydrate() calls (only last one wins)
  • Added test: async migration abort when newer hydration starts
  • Added test: state changes during hydration behavior
  • All existing persist tests pass (42 tests)

  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
@vercel
Copy link

vercel bot commented Dec 27, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
zustand-demo Ready Ready Preview, Comment Jan 12, 2026 5:48am

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 27, 2025

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.

@Niyaz-Mazhitov
Copy link
Contributor Author

If needed, I can provide a minimal reproduction sandbox.
Let me know if you'd like additional tests or adjustments, happy to iterate.

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

@dai-shi dai-shi requested a review from dbritto-dev December 29, 2025 10:18
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 29, 2025

commit: 627706d

@Niyaz-Mazhitov
Copy link
Contributor Author

Thanks a lot for the review and for looping in another reviewer!
I’ve updated the implementation to use the simpler version-check approach we discussed.
If you’d like me to adjust anything further or add more tests / comments, I’m happy to do so.

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.

simplify further?

…on' into fix/persist-hydration-race-condition
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.

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.

@Niyaz-Mazhitov
Copy link
Contributor Author

Thank you for the thorough review and patience! 🙏 Learned a lot from this process. Happy to contribute more in the future! 🚀

@dbritto-dev
Copy link
Collaborator

@Niyaz-Mazhitov @dai-shi LGTM

@dai-shi dai-shi added this to the v5.0.10 milestone Dec 30, 2025
@dai-shi dai-shi merged commit d0cb549 into pmndrs:main Jan 12, 2026
32 checks passed
mergify bot added a commit to robfrank/linklift that referenced this pull request Jan 18, 2026
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)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=zustand&package-manager=npm\_and\_yarn&previous-version=5.0.9&new-version=5.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)
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.

3 participants