fix(persist): clearStorage() should invalidate concurrent async rehydration - #3555
Merged
Conversation
…le async hydration Addresses #3554 When clearStorage() is called while an async rehydrate() is still pending, the storage item gets removed but the older read can finish afterward and apply the value that was just cleared. Fix: increment hydrationVersion in clearStorage() so any in-flight hydration is treated as stale and discarded, consistent with how concurrent rehydrate() calls are already handled. Co-authored-by: dai-shi <490574+dai-shi@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
dai-shi
August 10, 2026 13:46
View session
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
dai-shi
marked this pull request as ready for review
August 10, 2026 14:39
|
Size Change: +6 B (+0.05%) Total Size: 13.2 kB 📦 View Changed
ℹ️ View Unchanged
|
Co-authored-by: dai-shi <490574+dai-shi@users.noreply.github.com>
Co-authored-by: dai-shi <490574+dai-shi@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With async storage,
clearStorage()can race with a pendingrehydrate(): the item is removed, but the older async read completes afterward and writes the cleared value back into state.rehydrate()already useshydrationVersionto discard stale concurrent hydrations —clearStorage()simply wasn't advancing that counter.Summary
src/middleware/persist.ts: IncrementhydrationVersionat the start ofclearStorage(). Any in-flightrehydrate()then fails the existingcurrentVersion !== hydrationVersionstaleness checks and exits without applying state.tests/persistAsync.test.tsx: Five new tests covering:getItemresult discarded afterclearStorage()migrateresult discarded afterclearStorage()removeItemthrowsclearStorage()after a completed hydration does not reset live stateonRehydrateStorage/onFinishHydrationcallbacks suppressed for stale hydration; subsequentrehydrate()works correctlyClose #3554