Skip to content

Fix iOS multi-select OOM on high-traffic accounts: strip large policy collections from retryParams#95663

Closed
dilshodmackbook-sketch wants to merge 1 commit into
Expensify:mainfrom
dilshodmackbook-sketch:dilshod/fix-93846-retryparams-oom
Closed

Fix iOS multi-select OOM on high-traffic accounts: strip large policy collections from retryParams#95663
dilshodmackbook-sketch wants to merge 1 commit into
Expensify:mainfrom
dilshodmackbook-sketch:dilshod/fix-93846-retryparams-oom

Conversation

@dilshodmackbook-sketch

@dilshodmackbook-sketch dilshodmackbook-sketch commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

This is a follow-up to #94976, which fixed the receipt image decode/conversion memory spike during iOS multi-select. That fix resolved the crash for low-traffic accounts, but on high-traffic accounts the app still OOMs right after tapping Create expenses.

The remaining spike comes from retryParams. When a receipt upload fails, requestMoney builds a retryParams object from nearly the whole request payload and getReceiptError serializes it with JSON.stringify into every failed transaction's error (failureData). The heavy part is policyParams, which carries policyCategories and policyTagList — these scale with workspace size. On a large workspace, multi-selecting many receipts means the same large policy collections are stringified once per receipt, producing many big JSON strings at once and exhausting memory.

Notably, retryParams is written into the error but is never read back on the NewDot side — the receipt-error row in DotIndicatorMessage only offers "Save receipt" (fileDownload(source, filename)) and "Delete", with no retry path that consumes retryParams. So the large policy collections add memory cost without being used.

This PR strips the large policy collections (policyCategories, policyTagList, policyRecentlyUsedTags, policyRecentlyUsedCategories) from the requestMoney retryParams, mirroring the existing pattern in trackExpense where reportActionsList is stripped "to keep the serialized error JSON small". It's kept as a separate PR so it can be reverted independently of the decode fix if needed.

Fixed Issues

$ #93846
PROPOSAL:

Tests

  1. Sign in with a high-traffic account that has a large workspace (many categories and tags).
  2. On iOS, open an expense chat and start a scan/manual request, then multi-select a large batch of receipts (e.g. 20–30).
  3. Tap Create expenses.
  4. Verify the app creates the expenses without crashing / running out of memory.
  5. Force a receipt upload failure (e.g. go offline mid-upload) so a receipt error is shown, and verify:
    • The "Save receipt" and "Delete" actions on the failed expense still work.
    • No errors appear in the JS console.
  • Verify that no errors appear in the JS console

Offline tests

  1. Go offline.
  2. Multi-select receipts and tap Create expenses.
  3. Verify the expenses are created optimistically and, on a forced upload failure, the receipt-error row still shows "Save receipt" / "Delete" and behaves as before.

QA Steps

  1. Sign in with a high-traffic account with a large workspace on iOS.
  2. Multi-select a large batch of receipts and tap Create expenses.
  3. Verify the app does not crash and the expenses are created.
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover 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 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: mWeb Chrome
    • iOS: Native
    • iOS: mWeb 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 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 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)
  • 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 new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • 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.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user 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.

Screenshots/Videos

Android: Native
video_2026-07-09_18-58-01.mp4
Android: mWeb Chrome
Screen.Recording.2026-07-09.at.18.06.29.mov
iOS: Native
iOS: mWeb Safari
Screen.Recording.2026-07-09.at.18.10.59.mov
MacOS: Chrome / Safari
Screen.Recording.2026-07-09.at.17.23.09.mp4
Screen.Recording.2026-07-09.at.18.04.15.mp4
Details

retryParams is stringified into every failed transaction's receipt error
but is never read back on the NewDot retry path. policyCategories and
policyTagList scale with workspace size, so on high-traffic accounts a
multi-select receipt upload serializes many large JSON blobs and OOMs.
Strip these collections from retryParams, mirroring the existing
reportActionsList strip in trackExpense.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@melvin-bot

melvin-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

@QichenZhu Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@QichenZhu

QichenZhu commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

The heavy part is policyParams, which carries policyCategories and policyTagList — these scale with workspace size.

@dilshodmackbook-sketch I don't agree this is the case for high-traffic accounts, and this PR doesn't work in my testing. nvm, let me retest.

Update: #95663 (comment)

@QichenZhu

Copy link
Copy Markdown
Contributor

Please only mark a PR ready for review after you go through the checklist and attach screenshots/videos for all platforms. They are not optional. Please make sure you complete them for all your PRs.

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

You'''re right, sorry about that — I jumped the gun marking it ready. Moving it back to draft while I complete the checklist and gather the platform evidence.

One thing on the platforms: I can'''t build HybridApp locally, so I'''ll cover web/mWeb myself. Would it be okay to use the AdHoc test build for the iOS/Android native evidence? Happy to do whatever works best here.

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

No product review required.

@QichenZhu

Copy link
Copy Markdown
Contributor

One thing on the platforms: I can'''t build HybridApp locally

@dilshodmackbook-sketch you need to build and test on Android: Native and iOS: Native, which are standalone apps, not hybrid. Alternatively, you can test with ad-hoc builds, though you'll lose the convenience of live JS reload.

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

Thanks @QichenZhu, that clears it up — I was overcomplicating the native side.

@mountiny when you have a moment, could you trigger ad-hoc builds for this PR and #94976? I already recorded iOS: Native for #94976, so there I just need Android: Native, and for this one I'll need both iOS + Android. I'll wrap up the web/mWeb recordings in the meantime.

This one's still in draft while I finish gathering evidence — happy to mark it ready first if that's needed for the build to trigger, just let me know.

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here.

@QichenZhu

Copy link
Copy Markdown
Contributor

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
    https://expensify.slack.com/archives/C049HHMV9SM/p1778663561704459
  • I verified proper code patterns were followed (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 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 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
    • 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 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.
  • 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

@QichenZhu

QichenZhu commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

@dilshodmackbook-sketch unfortunately, this PR does not fix the issue. As shown in the video, there are two memory peaks. The first will be fixed by #94976. The second is what this PR is supposed to fix, but it doesn't.

ios-native.mov

Tested with the adhoc build on iPhone 17 iOS 26.5.

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

Thanks for testing @QichenZhu, and sorry the retryParams angle missed. I dug into the submit path, and my leading suspect for the second peak is the multi-receipt submission itself: the confirmation loop fires all ~30 trackExpense/requestMoney writes synchronously, so all the receipt files plus the per-transaction optimistic/failure Onyx data get built and queued at once — analogous to the decode peak that #94976 sequences, but on the submit side.

Does that match what your profiler shows for the second peak — is the heavy allocation in the submit/queue phase (receipt files + onyxData) rather than the error serialization? If so, I'd fix it by sequencing/throttling the submissions the same way #94976 sequences the conversions. Want to make sure I target the real allocation this time rather than guess again.

@QichenZhu

Copy link
Copy Markdown
Contributor

@dilshodmackbook-sketch I'm afraid I can't quite agree with this theory. There are no comparison videos showing the reproduction before your fix and the result after applying it, and my own testing doesn't support it either. To work on this issue, the only way is to experiment, not by language analysis, so using language models doesn't work.

@QichenZhu

QichenZhu commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Want to make sure I target the real allocation this time rather than guess again.

You are right. I've noticed a pattern: you implement a fix, mark it as ready without testing videos, then ask the internal engineer to build adhoc builds and have the reviewer test it.

That's not the right process.

You should validate it yourself first:

  1. Make sure you can reproduce the original issue without your fix, and
  2. Make sure the issue is resolved after applying your fix.

That way you are working from ground truth, not a guess.

@QichenZhu

Copy link
Copy Markdown
Contributor

@dilshodmackbook-sketch I offered to guide you on reproduction via Slack or Google Meet but haven't received a response. I assume you sorted it out yourself, but in case you are still having trouble reproducing the issue, please let us know as soon as possible so we can help.

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

You're right, and I appreciate the honest feedback — I got ahead of myself marking things ready and leaning on analysis instead of validating on device first. That's on me.

I'd like to take you up on the reproduction help. If it's alright, I'd prefer to keep it async over Slack (or here on GitHub) so I can share screen recordings as I go — I'll get access to #expensify-open-source now. I'll hold off on further PR updates until I can reproduce the crash on a native build without the fix and show a proper before/after. Thanks for your patience.

@dilshodmackbook-sketch

Copy link
Copy Markdown
Contributor Author

Closing this one — the retryParams approach was the wrong root cause for the second memory peak (confirmed via QichenZhu's ad-hoc testing). Stepping back from this issue; leaving #94976 open as discussed. Thanks all.

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.

3 participants