Skip to content

fix(storage): degrade to MemoryOnlyProvider once the IDB heal budget is exhausted - #854

Open
sosek108 wants to merge 13 commits into
Expensify:mainfrom
callstack-internal:fix/idb-fatal-memory-fallback
Open

sosek108 wants to merge 13 commits into
Expensify:mainfrom
callstack-internal:fix/idb-fatal-memory-fallback

Conversation

@sosek108

@sosek108 sosek108 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Details

Follow-up to #853. That PR made UnknownError: Internal error. go through the connection layer's FATAL heal. A heal reopens the connection, which is the same thing a reload does. For users whose database stays broken across connections (the user in the linked issue failed init on 5 reloads in a row), the heal budget runs out. After that, Onyx keeps sending every read and write to a dead IndexedDB for the rest of the session.

With this PR, once the budget is exhausted, createStore rethrows the error as IndexedDB heal budget exhausted: <original message>, with the original error as cause. classifyIDBError maps that wording to UNAVAILABLE. That class already does what we need:

  • the storage layer (lib/storage/index.ts) swaps in MemoryOnlyProvider, and
  • retryOperation skips the retry quietly.

The app keeps working for the session with in-memory storage, so data from OpenApp and later writes can be read back. Nothing persists across reloads, but persistence was already broken for these users.

The switch also happens earlier, at init. When Storage.getAll in initializeWithDefaultKeyStates fails FATAL, the connection layer has already reopened once and the read still failed. OnyxUtils boots with defaults as before, and now also calls the new Storage.degradeToMemoryOnly(error). Without this, the first writes after boot (the OpenApp burst) would each fail against IndexedDB until the budget ran out, and get dropped. The session already runs on defaults at that point, so writing it into a database we could not read gains nothing.

The budget-exhausted path still covers a database that breaks after a good init. It only fires after 3 failed reopen attempts with no successful operation in between. A one-off FATAL still heals as before and keeps IndexedDB.

The heal budget exhausted alert now carries errorMessage. Together with the Falling back to only using cache log, it shows in VictoriaLogs how many sessions end up on memory-only, and for which error.

Related Issues

Expensify/App#102272

Linked E/App PR

Expensify/App#103208

Automated Tests

  • classifyErrorTest: the heal-exhausted wording classifies as UNAVAILABLE.
  • createStoreTest: once the budget is spent, the rejection carries the new message and the original DOMException as cause, and classifies as UNAVAILABLE.
  • tryOrDegradePerformanceTest: a heal-exhausted rejection swaps the provider to MemoryOnlyProvider, and a later write and read round-trip through it.
  • tryOrDegradePerformanceTest: degradeToMemoryOnly swaps the provider to MemoryOnlyProvider.
  • onyxUtilsTest: an init read failing with UnknownError: Internal error. calls degradeToMemoryOnly, and one failing with an unclassified error does not.

Manual Tests

To be verified against the linked E/App PR.

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
Nagranie.z.ekranu.2026-10-8.o.10.33.15.mov
iOS: mWeb Safari
MacOS: Chrome / Safari
Nagranie.z.ekranu.2026-10-8.o.11.59.18.mov

Comment thread lib/storage/providers/IDBKeyValProvider/classifyError.ts Outdated
Comment thread lib/storage/providers/IDBKeyValProvider/classifyError.ts Outdated
@sosek108

sosek108 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@fabioh8010 I've aligned the code to your feedback. I've refactored the IDBKeyValProvider so there is no error string repetition

Comment thread lib/storage/providers/IDBKeyValProvider/errorMessages.ts Outdated
@@ -0,0 +1,7 @@
/** Messages of errors thrown by the IndexedDB provider itself, matched by `classifyIDBError` as UNAVAILABLE. */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"matched by classifyIDBError as UNAVAILABLE." is this right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, both messages are classified as StorageErrorClass.UNAVAILABLE (missing engine and spent heal budget both mean the store is gone for the session). I reworded the comment to name the class explicitly so it doesn't read as the key is unavailable

@sosek108
sosek108 marked this pull request as ready for review October 8, 2026 10:01
@sosek108
sosek108 requested a review from a team as a code owner October 8, 2026 10:01
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@melvin-bot
melvin-bot Bot requested review from amyevans and removed request for a team and fabioh8010 October 8, 2026 10:02
amyevans
amyevans previously approved these changes Oct 8, 2026
@amyevans
amyevans requested a review from mountiny October 8, 2026 14:07
@mountiny
mountiny requested a review from mkhutornyi October 9, 2026 15:47
mountiny
mountiny previously approved these changes Oct 9, 2026

@mountiny mountiny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes look good but waiting for testing and checklist from @mkhutornyi

@mkhutornyi

mkhutornyi commented Oct 9, 2026 •

Copy link
Copy Markdown

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • 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 any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • 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
    • 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 a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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 PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • 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 reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari
test1.mov
test2.mov

Comment thread lib/storage/index.ts Outdated
return provider;
},

degradeToMemoryOnly: (error) => degradePerformance(error),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

degradePerformance can run many times

Consider adding if (provider === MemoryOnlyProvider) return; at the start of this function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made the fix you suggested

Comment thread lib/OnyxUtils.ts
Logger.logAlert(`Failed to load data from storage during init. The app will boot with default key states only. Error: ${error}`);

if (Storage.classifyError(error) === StorageErrorClass.FATAL) {
Storage.degradeToMemoryOnly(error);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

degradeToMemoryOnly can receive something that isn't an Error

The value caught at startup is passed in as is.
A string rejection that is classified as FATAL from its text produces the log line Error: undefined Stack: undefined, which loses the only diagnostic for the switch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made a fix like you suggested

errorMessage,
});
} else if (errorClass === StorageErrorClass.UNKNOWN) {
throw new Error(`${IDBErrorMessage.HEAL_EXHAUSTED}: ${errorMessage}`, {cause: error});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

HEAL_EXHAUSTED is detected by matching text in the error message

Consider using dedicated name such as IDBHealExhaustedError, checked by name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed like you suggested

Comment thread lib/storage/providers/IDBKeyValProvider/errorMessages.ts
@mkhutornyi

Copy link
Copy Markdown

A tab on memory-only storage loses data when another tab writes

storage.multiGet(keys).then((pairs) => onStorageKeysChanged(pairs));

After a tab switches to memory-only storage, cross-tab sync still reads changed keys from that tab's own storage. That storage is now the empty in-memory store, so it returns undefined and Onyx.ts#L79 overwrites the cached value with it.

On main, this can't happen with a broken IndexedDB. Storage never switches to memory, so other tabs' writes fail before any sync event is sent. With this PR, writes succeed in memory and the sync events go out.

Example: two tabs both fall back to memory. Send a message in tab B, and in tab A that chat's messages vanish until reload.

Suggested fix: ignore sync events (or skip undefined values) once the provider is MemoryOnlyProvider.

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

No App issues found.
Worth checking #854 (comment) before merge

@mountiny

Copy link
Copy Markdown
Contributor

@fabioh8010 @sosek108 can you check the review comment, please?

@sosek108
sosek108 dismissed stale reviews from mountiny and amyevans via 57b9405 October 13, 2026 07:14
@sosek108

Copy link
Copy Markdown
Contributor Author

@mkhutornyi for I've applied your suggested fixes (alongside with #854 (comment)).

Ready for review

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.

5 participants