fix: re-check cache after storage read in get() to handle concurrent writes#769
Open
TaduJR wants to merge 1 commit intoExpensify:mainfrom
Open
Conversation
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
CLABotify
added a commit
to Expensify/CLA
that referenced
this pull request
Apr 11, 2026
Author
|
cc @hungvu193 |
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.
Details
mergeCollectionWithPatchesis the only Onyx write operation that defers its cache write to an async.then()chain. All other operations (setWithRetry,applyMerge,multiSetWithRetry) update cache synchronously. WhenOnyx.update()batches 2+ collection keys intomergeCollectionWithPatches, a concurrentOnyx.merge()on the same key can callget()before the cache write completes.get()falls back toStorage.getItem(), which returns null on Native (SQLite is slower than the deferred cache write). The merge then overwrites cache with the stale result, destroying fields set by the original update.This affects any optimistic data written via
Onyx.update()that shares a collection prefix with other keys in the same update batch. In the App, it manifests asisOptimisticReport: truebeing destroyed from report metadata whenupdateLastVisitTimefires concurrently.The fix adds a cache re-check in
get()after the async storage read resolves. If a concurrent operation populated the cache while storage was in-flight, the fresher cache value is used instead of the stale storage result.Web is unaffected because IndexedDB completes the storage write before the
getItemresolves, so storage returns the correct value. The race window exists on both platforms but only causes data loss on Native.Related Issues
$ Expensify/App#83779
Automated Tests
Added a test in
onyxTest.tsthat reproduces the race condition by mockingStorage.getItemwith a 50ms delay for the target key (simulating slow Native SQLite). The test callsOnyx.updatewith 2+ collection keys followed by a concurrentOnyx.mergeon the same key. Without the fix, the merged result loses the original fields. With the fix, both fields are preserved.Manual Tests
Prerequisite: Turn on "Simulate failing network requests"
Author Checklist
### Related Issuessection aboveTestssectiontoggleReportand notonIconClick)myBool && <MyComponent />.STYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)/** comment above it */thisproperly so there are no scoping issues (i.e. foronClick={this.submit}the methodthis.submitshould be bound tothisin the constructor)thisare necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);ifthis.submitis never passed to a component event handler likeonClick)Avataris modified, I verified thatAvataris working as expected in all cases)mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
Android-Native.mp4
Android: mWeb Chrome
Android-mWeb.mp4
iOS: Native
iOS-Native.mp4
iOS: mWeb Safari
iOS-Safari.mp4
MacOS: Chrome / Safari
Mac-Chrome.mp4