Skip to content

Extract authToken from gap-deferred sign-in while OpenApp is in-flight - #97097

Merged
inimaga merged 2 commits into
Expensify:mainfrom
callstack-internal:VickyStash/bugfix/96848-transition-407-loop-shortlived-authtoken
Jul 29, 2026
Merged

inimaga merged 2 commits into
Expensify:mainfrom
callstack-internal:VickyStash/bugfix/96848-transition-407-loop-shortlived-authtoken

Conversation

@VickyStash

@VickyStash VickyStash commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

When a stale, gap-behind client returns to NewDot via an OldDot→NewDot /transition?...&shortLivedAuthToken=... link, the app can get stuck on an endless loading spinner with every request returning 407, and the user is never logged out.

Root cause is a single branch in handleMissingOnyxUpdates (src/libs/actions/OnyxUpdateManager/index.ts):

If user:

  • has expired session locally AND
  • has gap in data (missing onyx updates) AND
  • has session.signedInWithShortLivedAuthToken set as true
  1. On transition the app calls OpenApp with the expired persisted authToken, which sets isLoadingApp: true.
  2. SignInWithShortLivedAuthToken returns 200 with the fresh authToken, but because the client is behind on updates, that response is gap-deferred into ONYX_UPDATES_FROM_SERVER instead of applied inline.
  3. The deferred response reaches handleMissingOnyxUpdates while OpenApp is still in-flight (isLoadingApp === true), so it hits the if (isLoadingApp) early-return before updateAuthTokenIfNecessary ever runs. The fresh authToken is skipped, and this pass runs only once — nothing retries it.
  4. OpenApp then 407s (expired token) and its isLoadingApp: false clear is discarded on the failed-reauth path, so the loader stays up. Every subsequent read / Pusher connection 407s and is aborted by the short-lived-token reauth guard → endless loading, no logout.

The deadlock: OpenApp needs a valid token to finish and clear isLoadingApp, but the fresh token can't be applied while isLoadingApp is true.

before1.mp4

Fix: extract the authToken even when the deferred update body is skipped. In the if (isLoadingApp) branch, call updateAuthTokenIfNecessary(onyxUpdatesFromServer) before the early-return. This still skips applying the update body while OpenApp is in-flight (base state isn't ready yet — the original reason for the branch), but it lets a gap-deferred SignInWithShortLivedAuthToken response hand over its fresh authToken.
Once the token lands, the app's OpenApp retry succeeds and loading completes normally.

Fixed Issues

$ #96848
PROPOSAL: N/A

Tests

  • Verify that no errors appear in the JS console

Pre-condition:
Login to the same account:

  • in Browser 1 - new dot
  • in Browser 2 - new dot
  • in Browser 1 - old dot
  1. Open the app from browser 1 (ND)
  2. Open troubleshoot menu
  3. In console, run Onyx.merge('session', {signedInWithShortLivedAuthToken: true})
  4. In troubleshoot menu Authentication status -> tap Invalidate and close the app right away
  5. Open app in Browser 2. Do some activity, send some messages.
  6. Go back to Browser 1, from OldDot, transition to NewDot via a /transition?...&shortLivedAuthToken=... link.
  7. Verify the app loads instead of getting stuck on an endless loading spinner. In logs you should see [OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.

Offline tests

Same, as in the Tests section

QA Steps

// TODO: These must be filled out, or the issue title must include "[No QA]."

  • Verify that no errors appear in the JS console

Same, as in the Tests section

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 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 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 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 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

MacOS: Chrome / Safari
after.mp4

@VickyStash

Copy link
Copy Markdown
Contributor Author

@codex review

@codecov

codecov Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/libs/actions/OnyxUpdateManager/index.ts 85.08% <0.00%> (-1.52%) ⬇️
... and 605 files with indirect coverage changes

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 8b2b6ffb6f

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

@VickyStash
VickyStash marked this pull request as ready for review July 27, 2026 13:59
@VickyStash
VickyStash requested review from a team as code owners July 27, 2026 13:59
@melvin-bot
melvin-bot Bot requested review from eVoloshchak and heyjennahay July 27, 2026 13:59
@melvin-bot

melvin-bot Bot commented Jul 27, 2026

Copy link
Copy Markdown

@eVoloshchak 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]

@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: 8b2b6ffb6f

ℹ️ 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 src/libs/actions/OnyxUpdateManager/index.ts
@eVoloshchak

eVoloshchak commented Jul 28, 2026 •

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
  • 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.
  • 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

MacOS: Chrome / Safari
Screen.Recording.2026-07-28.at.19.16.52.mov

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

LGTM!

@melvin-bot

melvin-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

We did not find an internal engineer to review this PR, trying to assign a random engineer to #96848 as well as to this PR... Please reach out for help on Slack if no one gets assigned!

@melvin-bot
melvin-bot Bot requested a review from inimaga July 28, 2026 17:20
@inimaga
inimaga merged commit 6f91b77 into Expensify:main Jul 29, 2026
35 of 36 checks passed
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions

Copy link
Copy Markdown
Contributor

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

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/inimaga in version: 9.4.46-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

🤖 No help site changes required.

This PR is a purely internal, client-side bug fix in src/libs/actions/OnyxUpdateManager/index.ts — it makes handleMissingOnyxUpdates extract a fresh authToken from a gap-deferred SignInWithShortLivedAuthToken response even while OpenApp is in-flight, fixing an endless-loading-spinner deadlock on the OldDot→NewDot transition.

There's no change to any user-facing feature, setting, tab label, button, or workflow — the only observable effect is that the app no longer gets stuck loading in this edge case. Nothing in App/docs/articles documents authToken handoff, Onyx update gaps, or the /transition sign-in flow, so there's nothing to update on the help site.

No draft docs PR was created.

@VickyStash, if you believe some user-facing behavior here should be documented that I've missed, let me know and I'll draft the help site changes.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/marcaaron in version: 9.4.46-10 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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.

7 participants