Skip to content

Skip keyless update entries in updateSnapshots instead of crashing - #818

Open
elirangoshen wants to merge 4 commits into
Expensify:mainfrom
callstack-internal:eliran/fix-updatesnapshots-keyless-updates
Open

Skip keyless update entries in updateSnapshots instead of crashing#818
elirangoshen wants to merge 4 commits into
Expensify:mainfrom
callstack-internal:eliran/fix-updatesnapshots-keyless-updates

Conversation

@elirangoshen

@elirangoshen elirangoshen commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Details

Came from this slack thread

Fixes a production crash in OnyxUtils.updateSnapshots: TypeError: Cannot read properties of undefined (reading 'startsWith') (Sentry APP-6NT, ~94 events / ~35 users since 2026-03-01, plus duplicate groupings APP-JT0, APP-961, APP-H8X, APP-ED8 — all on web, mostly 2FA pages).

Root cause: Onyx.update validates its entries and skips any with a non-string key (and clear/multiset entries legitimately carry no key at all), but then passes the unfiltered update array to OnyxUtils.updateSnapshots. There, OnyxKeys.isCollectionMemberKey(snapshotCollectionKey, key) calls key.startsWith(collectionKey) with no guard, so any keyless entry throws and rejects the whole Onyx.update promise. It only reproduces when the snapshot collection is non-empty (i.e. the user has cached search snapshots), which is why the volume is low.

In Expensify/App this surfaced as an unhandled rejection from QueuedOnyxUpdates.flushQueue (the deferred WRITE flush), and Sentry mis-attributed the newest grouping (APP-JT0) to Expensify/App PR #97098, which merely touched those lines.

Fix: in updateSnapshots, skip entries where typeof key !== 'string' before calling isCollectionMemberKey — mirroring the per-entry validation Onyx.update already performs.

Fixes APP-6NT

Related Issues

Expensify/App#98438

Linked E/App PR

Expensify/App#97857 (TESTING ONLY pin of this PR's HEAD, so the full E/App CI suite runs against it). The fix itself is self-contained in Onyx and requires no App-side change; for context, Expensify/App#97098 is the App PR that Sentry falsely attributed the newest crash grouping to.

Automated Tests

Added a regression test in tests/unit/onyxTest.ts — "should skip update entries without a key when updating Snapshots instead of rejecting": dispatches an Onyx.update containing a keyless merge entry alongside a valid one while a snapshot is cached. Without the fix it reproduces the exact production crash (TypeError: Cannot read properties of undefined (reading 'startsWith') rejecting Onyx.update); with the fix, Onyx.update resolves and the valid entry still lands in the snapshot.

Verification run: 173 unit tests pass (onyxTest + onyxUtilsTest), tsc --noEmit clean, ESLint clean, Prettier clean. Also verified the new test fails with the exact production stack trace when the guard is stashed.

Manual Tests

On Android / iOS (native or mWeb) against staging:

  1. Sign in with an account that has chats and expenses.
  2. Go to the Reports/Search tab and run a search (e.g. All expenses). Scroll through the results and open one or two of them. (This caches search snapshot data — the precondition for the code path this PR touches.)
  3. Go back to Inbox and, in a report that appeared in those search results: send a message, edit an expense amount, and mark the report as unread.
  4. Return to the Search tab and re-open the search. Verify the results reflect the changes (new amount, latest message) and the app does not crash or freeze at any point.
  5. Enable airplane mode. Make 2–3 more changes (add a comment, rename a report, edit an expense). Disable airplane mode and wait for sync to finish. Verify the app stays responsive, nothing crashes, and search results reflect the offline changes.
  6. Sign out and sign back in. Verify no crash during sign-out/sign-in.

Expected: no crash and no stuck/undelivered updates in any step. (Before this fix, a malformed keyless entry in any server update batch would crash the app with a startsWith TypeError whenever a search had previously been run — steps 2–5 keep that code path continuously active.)

Deterministic developer repro (web console)
  1. In Expensify/App (web), sign in and run a Search so at least one snapshot_ collection entry is cached.
  2. In the dev console, dispatch an update containing a keyless entry alongside a valid one, e.g. Onyx.update([{onyxMethod: 'merge', key: 'session', value: {}}, {onyxMethod: 'merge', value: {}}]).
  3. Without this fix: the promise rejects with TypeError: Cannot read properties of undefined (reading 'startsWith') from updateSnapshots.
  4. With this fix: the promise resolves, the keyless entry is skipped, and the valid entry is applied to the cached snapshot.

QA Steps

Same as Manual Tests.

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

N/A — non-UI library fix, covered by the unit regression test.

Android: mWeb Chrome

N/A — non-UI library fix, covered by the unit regression test.

iOS: Native
Simulator.Screen.Recording.-.iPhone.17.-.2026-08-13.at.14.49.42.mov
iOS: mWeb Safari

N/A — non-UI library fix, covered by the unit regression test.

MacOS: Chrome / Safari

N/A — non-UI library fix, covered by the unit regression test.

Onyx.update already skips entries without a string key (and clear/multiset
entries legitimately have none), but passed the unfiltered array to
updateSnapshots, which crashed on key.startsWith when a snapshot was cached.

Fixes APP-6NT

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Comment thread lib/OnyxUtils.ts Outdated
Comment on lines +1214 to +1215
// `clear`/`multiset` updates legitimately carry no key (and `Onyx.update` skips other keyless
// entries), so ignore them here too instead of crashing on `key.startsWith` below.

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.

Unnecessary comment imo

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.

removed

Comment thread tests/unit/onyxTest.ts Outdated
Comment on lines +1557 to +1558
// A keyless entry (e.g. a malformed server update) used to crash updateSnapshots with
// "can't access property 'startsWith', key is undefined" and reject the whole update.

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.

Same

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.

removed

Comment thread lib/OnyxUtils.ts
Comment on lines +1214 to +1215
if (typeof key !== 'string') {
continue;

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.

Please, add Log message, same as it's done in Onyx.update

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.

Done

Comment thread lib/OnyxUtils.ts Outdated

for (const {key, value} of data) {
if (typeof key !== 'string') {
Logger.logInfo(`Invalid ${typeof key} key provided in Onyx update. Key must be of type string. Skipping snapshot update for this entry.`);

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.

NAB: Maybe its worth to log something extra here instead of only typeof key? onyxMethod? Something to help with debugging later? Trimmed String(key) maybe?

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.

good idea, done

@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: 758d11fc5a

ℹ️ 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/OnyxUtils.ts
Comment on lines +1215 to +1219
// clear/multiset entries legitimately carry no key; snapshots have nothing to update for them
if (onyxMethod !== METHOD.CLEAR && onyxMethod !== METHOD.MULTI_SET) {
Logger.logInfo(`Invalid ${typeof key} key (method: ${onyxMethod}, key: ${String(key).slice(0, 50)}) provided in Onyx update. Skipping snapshot update for this entry.`);
}
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Expand keyless multiset updates for snapshots

When an Onyx.update batch uses a keyless METHOD.MULTI_SET, the update handler in lib/Onyx.ts still applies every key from the entry's value, but this new guard drops the whole entry before updateSnapshots can inspect those keys. If any of the multiset keys is present in a cached snapshot, the real Onyx value is updated while the snapshot keeps the old value, so search/snapshot consumers can show stale data after a successful update; handle MULTI_SET by expanding its payload into per-key snapshot updates instead of continuing here.

Useful? React with 👍 / 👎.

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.

ok, so this is not a regression with this code, so we could choose to handle it in a follow-up. What we have here is strictly better than what we have on main, which errors in this case.

That said, we probably should update updateSnapshots to work with multiSet and clear, which would be strictly better than just avoiding a crash. Right @elirangoshen ?

@Valforte

Copy link
Copy Markdown
Contributor

@roryabraham could you review this PR? I'm not familiar with this code or what it's trying to achieve

Comment thread lib/OnyxUtils.ts
if (typeof key !== 'string') {
// clear/multiset entries legitimately carry no key; snapshots have nothing to update for them
if (onyxMethod !== METHOD.CLEAR && onyxMethod !== METHOD.MULTI_SET) {
Logger.logInfo(`Invalid ${typeof key} key (method: ${onyxMethod}, key: ${String(key).slice(0, 50)}) provided in Onyx update. Skipping snapshot update for this entry.`);

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.

Suggested change
Logger.logInfo(`Invalid ${typeof key} key (method: ${onyxMethod}, key: ${String(key).slice(0, 50)}) provided in Onyx update. Skipping snapshot update for this entry.`);
Logger.logWarn(`Invalid ${typeof key} key (method: ${onyxMethod}, key: ${String(key).slice(0, 50)}) provided in Onyx update. Skipping snapshot update for this entry.`);

Comment thread lib/OnyxUtils.ts
Comment on lines +1215 to +1219
// clear/multiset entries legitimately carry no key; snapshots have nothing to update for them
if (onyxMethod !== METHOD.CLEAR && onyxMethod !== METHOD.MULTI_SET) {
Logger.logInfo(`Invalid ${typeof key} key (method: ${onyxMethod}, key: ${String(key).slice(0, 50)}) provided in Onyx update. Skipping snapshot update for this entry.`);
}
continue;

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.

ok, so this is not a regression with this code, so we could choose to handle it in a follow-up. What we have here is strictly better than what we have on main, which errors in this case.

That said, we probably should update updateSnapshots to work with multiSet and clear, which would be strictly better than just avoiding a crash. Right @elirangoshen ?

@Valforte
Valforte removed their request for review August 13, 2026 21:36
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.

6 participants