Skip to content

Keep values written while Onyx.clear is in progress - #881

Merged
carlosmiceli merged 2 commits into
Expensify:mainfrom
callstack-internal:eliran/3100-clear-keeps-writes-during-clear
Oct 28, 2026
Merged

carlosmiceli merged 2 commits into
Expensify:mainfrom
callstack-internal:eliran/3100-clear-keeps-writes-during-clear

Conversation

@elirangoshen

@elirangoshen elirangoshen commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Details

Onyx.clear resets the cache right away, but it writes the default values to storage and notifies subscribers only after storage has finished clearing. It uses the values it captured when it started. If something writes to a key while storage is still clearing, the end of clear overwrites that write: the key's default is written over it in storage, and subscribers are told the key was reset even though the cache holds the new value.

In E/App this happens when a user switches accounts in a browser that is already signed in (Supportal, OldDot transition, re-auth). The sign-out clear can take from 200ms to over 10s, and the new sign-in saves its session and queues OpenApp during that time. When the clear finishes, session goes back to its default {loading: false} in storage and in every subscriber, so every request after that gets a 407, and the queued OpenApp is dropped. The user is left on infinite skeletons. Production logs from the last few days show 34 of these overlaps across 16 accounts; in 32 of them the new login token was wiped.

This PR makes clear leave such writes alone:

  • While it runs, clear records the keys of every write issued through the public API (set, merge, update, ...), using the key list each write already registers in PendingWrites. Reads that refill the cache from storage never register there, so they are not mistaken for writes, even when a write stores the same value the key had before the clear. A recorded key counts as written during the clear only if its cache value is no longer the one clear reset it to, so a write issued before the reset still loses to it, as before.
  • Default values are written to storage only for keys that were not written during the clear. Written keys are saved to storage again after removeItems, in case it deleted them after they were stored.
  • Keys written during the clear (including collection members) do not get the end-of-clear notification. Their subscribers already received the newer value when it was written.

Keys nobody writes during the clear behave exactly as before.

App ships Onyx 3.0.117, and 3.0.118 to 3.0.123 need App changes that are not merged yet (async Onyx.get, react-native-nitro-sqlite 10). So the linked E/App PR pins this fix applied on top of 3.0.117 (callstack-internal/react-native-onyx#b5f84a4a), which isolates it in E/App CI.

Related Issues

Expensify/App#104458

Linked E/App PR

Expensify/App#105110

Automated Tests

Added tests/unit/onyxClearConcurrentWriteTest.ts (Write data while Onyx.clear is in progress). It holds Storage.removeItems so writes land while a clear is running:

  • should keep values written to keys after the clear reset them: a key with a default, a regular key and a collection member are written during the clear, and all three keep the new value in subscribers, the cache and storage. Without the fix this fails with default / undefined.
  • should keep a value written during the clear even when it equals the value from before the clear: writing the pre-clear value again during the clear (like a re-auth restoring unchanged session data) is kept, not wiped.
  • should still reset keys that were not written during the clear: a key that is not written still goes back to its default.

The full unit suite passes (24 suites, 710 tests).

Manual Tests

Tested through the linked E/App PR:

  1. On web, sign in to NewDot as account A.
  2. In the same browser, open a Supportal or OldDot transition link for a different account B.
  3. Verify account B loads (no infinite skeletons) and the console has no [SignInRedirect] Sign-out clear finished after a new sign-in started line with isNewSessionWiped: true.
  4. Sign out and sign in again as B. Verify sign-out still clears the previous account's data (Inbox, workspaces).

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I linked the corresponding Expensify/App PR in the ### Linked E/App PR section above, and verified this change against it (E/App CI passed and manual testing completed)
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari 3-account-b-inbox-loaded 3b-account-b-profile 4-after-signout-new-account-inbox

height="900" alt="1-account-a-inbox" src="https://github.com/user-attachments/assets/1863d00b-23b6-46f2-b243-a2f7ff6aef1f" />

1b-account-a-profile 2-transition-in-progress

Onyx.clear resets the cache right away but writes defaults to storage and
notifies subscribers only after storage finishes clearing, using the values
it captured at the start. A write that lands in between, like a new sign-in
saving its session while the previous sign-out is still clearing, was then
overwritten in storage and subscribers were told the key was reset.

Skip the default write and the stale notification for keys written during
the clear, and save those keys again after removeItems.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T12:13:14.005943Z 566009c Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@melvin-bot
melvin-bot Bot requested review from carlosmiceli and removed request for a team October 27, 2026 12:06

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 566009cb40

ℹ️ 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".

Comment thread lib/Onyx.ts Outdated
Comparing the cache value with the value from before the clear treated a
write that repeats that value (e.g. a re-auth restoring unchanged session
data) as a read refilling the cache, so clear still wiped it. Record the
keys of writes issued while clear runs through PendingWrites instead, since
reads never register there.
@elirangoshen

Copy link
Copy Markdown
Contributor Author

Hi @carlosmiceli , is there anything from my side to do to merge this one ?

@carlosmiceli
carlosmiceli merged commit 1b6535d into Expensify:main Oct 28, 2026
13 of 17 checks passed
@os-botify

os-botify Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

🚀 Published to npm in 3.0.139 🎉

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.

2 participants