Repository navigation
Keep another tab's writes when Onyx.clear runs - #899
elirangoshen wants to merge 2 commits into
Conversation
When one tab signs out while another signs in, the sign-out clear wiped the new session for every tab. Three things caused it on web: - Storage applies writes in order, so the other tab's write waited behind the clear's removal and was then overwritten by the defaults the clear wrote afterwards. The defaults are now written before the removal. - Writes from another tab skip the write tracking added for writes made during a clear. InstanceSync now reports the changed keys as soon as the storage event arrives, and clear leaves those keys in storage and reads them back at the end. - The writing tab's own sync read, queued before its write, could return after it and overwrite the newer value in memory. A key this tab writes while it is being read is now read again.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cbb1c73f59
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| .then(() => { | ||
| // Keys reported while the storage step ran are read back too | ||
| keysWrittenByOtherInstance = collectKeysWrittenByOtherInstance(); | ||
| return keysWrittenByOtherInstance.size > 0 ? Storage.multiGet([...keysWrittenByOtherInstance]) : []; |
There was a problem hiding this comment.
Guard the final readback against newer local writes
When another tab has announced a key and a local Onyx.set() runs after this final multiGet is queued but before it resolves, the returned snapshot can predate that local write. The snapshot is still applied unconditionally, and wasWrittenOnlyByThisInstance remains false for the key, so cache and subscribers revert to the older value even though storage contains the newer local value. This can roll back a successful local sign-in. Track writes during this read and skip or re-read affected keys, as the new InstanceSync reader does.
Useful? React with 👍 / 👎.
| .then((pairsWrittenByOtherInstance) => { | ||
| // Storage holds the other tab's values, so the cache and subscribers take them instead of the reset values. | ||
| for (const [key, storedValue] of pairsWrittenByOtherInstance) { | ||
| const value = storedValue ?? OnyxUtils.getNullFreeDefaultKeyState(key) ?? undefined; |
There was a problem hiding this comment.
Normalize storage values before restoring them to the cache
For simultaneous clears with an initialKeyStates value such as {errors: null, loading: false}, the other tab writes that raw default using Storage.multiSet, and IDBKeyValProvider.multiGet returns it without normalization. This branch then caches and notifies {errors: null, loading: false} instead of {loading: false}, potentially undoing normalization already performed by the regular InstanceSync callback and violating the null-free cache assumption used by removeNestedNullValues. Apply the same normalization as the regular sync callback before caching and notifying these values.
Useful? React with 👍 / 👎.
| const resetDefaultsPromise = Storage.multiSet(defaultKeyValuePairs.filter(([key]) => !keysWrittenByOtherInstance.has(key))); | ||
|
|
||
| // Remove only the items that we want cleared from storage | ||
| for (const key of keysToBeClearedFromStorage) cache.drop(key); | ||
| return Storage.removeItems(keysToBeClearedFromStorage) | ||
| return Promise.all([resetDefaultsPromise, Storage.removeItems(keysToBeClearedFromStorage)]) |
There was a problem hiding this comment.
Preserve quota-safe clearing when writing defaults first
When IndexedDB is near its quota and restoring a missing or larger default requires additional space, this multiSet now runs before deletion can reclaim that space. Capacity errors propagate through the storage layer without retry here, so Promise.all rejects and skips the clear's subscriber notifications even though removeItems can subsequently succeed. The previous delete-then-default sequence succeeds in this scenario. Preserve a quota-safe completion path so clearing a full store does not reject and leave subscribers showing the pre-clear state.
Useful? React with 👍 / 👎.
- Ignore a read-back value for a key this tab wrote during the read, since the local write is newer - Remove nested nulls from read-back values, as regular cross-tab sync does - Retry the defaults write after the removal when it fails, so a full store still clears - Shorten comments
Details
#881 stopped
Onyx.clearfrom overwriting writes made during the clear, but only writes made in the same tab. Production logs after #881 shipped show no change: on Nov 24–25, 59 of 88 sign-out clears that overlapped a new sign-in (20 accounts) still wiped the new session, all on web builds that have the fix.The new session comes from another tab. When one tab signs out while another signs in, three things on web wipe it:
removeItems, and then the defaultsmultiSetthatclearissues afterwards overwrites it. The defaults are now written beforeremoveItems, at the same time.InstanceSync, which updates the cache and notifies subscribers without going throughPendingWrites, so the write listener from Keep values written while Onyx.clear is in progress #881 never sees it.InstanceSyncnow reports the changed keys as soon as the storage event arrives (before it reads their values), through a newPendingWrites.reportOtherInstanceWrite.clearleaves those keys out of the defaults it writes, and at the end reads them back from storage and puts the stored value (or the default, if the other tab removed it) in the cache and subscribers.InstanceSyncis reading it is now dropped from that result and read again.Keys that no tab writes during the clear behave as before. Native has no
InstanceSync, so only the write-order change applies there.Related Issues
Expensify/App#104458
Linked E/App PR
Expensify/App#108178
Automated Tests
Added
tests/unit/onyxClearCrossTabWriteTest.ts(Another tab writes while Onyx.clear is in progress). It holdsStorage.removeItemsand fires thekeepInstancesSynccallbacks the way another tab's write arrives:should keep a session another tab saved while this tab was clearingshould read back a session another tab saved even when its value has not reached this tab yet: only the key is announced, and the value is read from storage.should read back a collection member another tab saved while this tab was clearing: the key is announced late, while the defaults are being written.should end with the default session when the other tab was signing out tooAdded to
tests/unit/storage/instanceSyncWebTest.ts(storage event parsing):reports the changed keys as soon as the event arrives, before reading their valuesreads a key again when this tab writes it while its value is being readThe full unit suite passes (795 tests).
Manual Tests
Two-tab browser test of this fix (Onyx bundled for web with IndexedDB and
InstanceSync), compared with 3.0.149 from production:Onyx.clear(sign-out). Tab B saves a new session 5, 80, 300, 480 and 700 ms after the clear starts.On 3.0.149 the new session was wiped when it was saved 57–355 ms into the clear. With the fix every case was correct.
In the App, through the linked E/App PR:
[SignInRedirect] Sign-out clear finished after a new sign-in startedline withisNewSessionWiped: true.Author Checklist
### Related Issuessection above### Linked E/App PRsection above, and verified this change against it (E/App CI passed and manual testing completed)TestssectiontoggleReportand 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: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari