Skip to content

[Due for payment 2026-05-12] [$250] Fix doubled OpenApp on delegate (copilot) account switch #89265

Closed

Description

@MelvinBot

When a user switches into a copilot/delegate account via the account switcher, OpenApp fires twice — the first call resolves with skeletons visible, then a second OpenApp runs while skeletons are still visible, causing a noticeable double-loading experience on large accounts.

Two independent root causes:

  1. NetworkState reconfigure path: The CONNECT_AS_DELEGATE response updates SESSION.accountID. The SESSION subscriber in NetworkState.ts calls configureAndSubscribe() to rebuild the NetInfo subscription. Re-subscribing emits a synthetic null → true transition that the listener treats as a network recovery event → reconnect() → openApp().

  2. DelegateAccessHandler recovery effect path: Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS) preserves HAS_LOADED_APP=true but not IS_LOADING_APP. After the clear, the state is briefly hasLoadedApp=true && isLoadingApp=undefined — exactly the trigger for the recovery effect, which fires its own openApp().

Related PR: #89254

Issue OwnerCurrent Issue Owner: @abzokhattab
Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~022049837260593240936
  • Upwork Job ID: 2049837260593240936
  • Last Price Increase: 2026-04-30

Activity

  1. added
    ReviewingHas a PR in review
    BugSomething is broken. Auto assigns a BugZero manager.
    on Apr 30, 2026
  2. MelvinBot commented on Apr 30, 2026

    @MelvinBot
    ContributorAuthor

    Proposal

    What is the root cause of that problem?

    When switching to a copilot/delegate account, OpenApp fires twice because two independent code paths each trigger their own openApp() call:

    Root Cause 1 — NetworkState reconfigure path: The CONNECT_AS_DELEGATE response updates SESSION.accountID. The SESSION subscriber in NetworkState.ts:332-342 detects the accountID change and calls configureAndSubscribe(), which tears down and recreates the NetInfo subscription (NetworkState.ts:270-323). The new subscription emits a synthetic state event. If prevIsInternetReachable is null (set by setForceOffline at line 159), the listener at line 317 treats this as a network recovery → onReachabilityRestored() → notifyReconnectListeners() → Reconnect.ts:58-60 → openApp().

    Root Cause 2 — DelegateAccessHandler recovery effect: Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS) preserves HAS_LOADED_APP (Delegate.ts:30) but does not preserve IS_LOADING_APP (Delegate.ts:23-42). After the clear, the state is hasLoadedApp=true && isLoadingApp=undefined — exactly the trigger for the recovery effect in DelegateAccessHandler.tsx:61-73, which fires its own openApp().

    What changes do you think we should make in order to solve the problem?

    Both root causes need to be addressed:

    1. For Root Cause 2: Introduce a clearOnyxForDelegateTransition() helper in Delegate.ts that merges IS_LOADING_APP=true before calling Onyx.clear(), and adds IS_LOADING_APP to the preserve list. This ensures post-clear state is always isLoadingApp=true, preventing the recovery effect from firing. Replace all 4 existing Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS) call sites (3 in Delegate.ts, 1 in Session/index.ts) with this helper.

    2. For Root Cause 1: Add a suppression flag in NetworkState.ts that skips the next onReachabilityRestored() call when configureAndSubscribe() is invoked due to a SESSION accountID change. This prevents the synthetic NetInfo transition from being misinterpreted as a network recovery event.

    PR #89254 already implements both of these fixes.


    Next Steps for Contributor+ team: Reply with @MelvinBot implement this to create a draft PR, @MelvinBot <your feedback> to refine this analysis, or explain why you are rejecting Melvin's proposal.

  3. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Apr 30, 2026
  4. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Apr 30, 2026
  5. melvin-bot commented on Apr 30, 2026

    @melvin-bot

    Current assignee @abzokhattab is eligible for the External assigner, not assigning anyone new.

  6. changed the title [-]Fix doubled OpenApp on delegate (copilot) account switch[/-] [+][$250] Fix doubled OpenApp on delegate (copilot) account switch[/+] on Apr 30, 2026
  7. melvin-bot commented on Apr 30, 2026

    @melvin-bot
  8. Yatin-Bhatti commented on Apr 30, 2026

    @Yatin-Bhatti

    Proposal

    What is the root cause of that problem?

    The redundant OpenApp execution is caused by a Sessional Lifecycle Desynchronization during the account-switching phase.
    Currently, the application treats the "Delegate Switch" as two fragmented operations: a data-clear and a network-reconfiguration. This creates two distinct "False Positive" triggers:
    The Incomplete State Trigger: Onyx.clear preserves the "App Loaded" status but resets the "Is Loading" status. This creates an illegal state where the app thinks it is fully initialized but lacks the "Loading" gate, causing the DelegateAccessHandler to "panic" and trigger a recovery OpenApp.
    The Synthetic Network Event: Re-configuring the NetworkState due to an account change forces a NetInfo re-subscription. This re-subscription emits a "synthetic" connection event that the system misinterprets as a transition from "Offline" to "Online," triggering a second, redundant openApp.
    The system lacks a Transition Lock to signal that these state changes are part of a deliberate account handover rather than a system failure or network recovery.

    What changes do you think we should make in order to solve the problem?

    I propose an Atomic Sessional Handover approach to silence redundant triggers at the source:

    1. Implement a Transition Middleware: I will create a unified transitionToDelegateAccount utility. This middleware will ensure that IS_LOADING_APP is programmatically locked to true before the Onyx.clear operation begins. By ensuring IS_LOADING_APP is in the "Preserve List," we prevent the DelegateAccessHandler from ever seeing the indeterminate state that triggers the first redundant call.

    2. Context-Aware Network Suppression: Instead of adding flags to the network listener, I will update NetworkState.ts to implement a "Quiescent Subscription" model. When the account ID changes, the logic will perform a "Silent Re-subscription" that explicitly ignores the first emitted event if the network status has not physically changed.

    3. Synchronize the Reset Lifecycle: I will refactor the transition flow to ensure that the navigation to the new account's home screen and the final data hydration are part of a single, orchestrated sequence. This ensures the "Skeletons" are only replaced once the final OpenApp call (the legitimate one) has successfully returned.

    4. Logic Centralization: This fix removes the need for "Suppression Flags" inside UI components, moving the responsibility for sessional stability to the logic layer where the switch is initiated.

    What alternative solutions did you explore? (Optional)

    I considered the approach of simply adding IS_LOADING_APP to the existing clear list. However, I rejected this as an incomplete solution because it doesn't address the Synthetic Network Recovery bug. I also explored Proposal 1's idea of a suppression flag in NetworkState, but determined that a "Quiescent Subscription" is more robust because it relies on data-identity (checking if the network state actually changed) rather than a manually managed boolean flag, which is less prone to race conditions.

  9. CyberMechAV commented on Apr 30, 2026

    @CyberMechAV

    Proposal

    I can fix this double OpenApp loading issue.

    I understand the two separate triggers:

    NetworkState.ts rebuilds the NetInfo subscription after CONNECT_AS_DELEGATE updates SESSION.accountID. The new subscription emits a synthetic connectivity transition, which is incorrectly treated as recovery and calls reconnect() -> openApp().

    DelegateAccessHandler clears Onyx while preserving HAS_LOADED_APP, but IS_LOADING_APP is briefly undefined. That creates hasLoadedApp=true && isLoadingApp=undefined, which triggers the recovery effect and fires another openApp().

    My fix would be focused and minimal:

    Guard the NetInfo reconfigure path so synthetic initial emissions after resubscribe do not trigger reconnect/openApp.

    Preserve or explicitly stabilize IS_LOADING_APP during delegate Onyx.clear(), or adjust the recovery effect so it does not run during intentional delegate account switching.

    Add/update tests around delegate account switching to prove OpenApp only fires once.

    Verify against the behavior from related PR #89254 and make sure the skeleton state does not restart for large accounts.

    I can inspect the existing implementation, patch both paths, and provide a clean PR with the exact reasoning and test coverage.

  10. melvin-bot commented on Apr 30, 2026

    @melvin-bot

    📣 @CyberMechAV! 📣
    Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork.
    Please follow these steps:

    1. Make sure you've read and understood the contributing guidelines.
    2. Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
    3. Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
    4. Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details.
      Screen Shot 2022-11-16 at 4 42 54 PM
      Format:
    Contributor details
    Your Expensify account email: <REPLACE EMAIL HERE>
    Upwork Profile Link: <REPLACE LINK HERE>
    
  11. 29 remaining items

  12. melvin-bot commented on May 18, 2026

    @melvin-bot

    @mallenexpensify Huh... This is 4 days overdue. Who can take care of this?

  13. melvin-bot commented on May 20, 2026

    @melvin-bot

    @mallenexpensify 6 days overdue. This is scarier than being forced to listen to Vogon poetry!

  14. melvin-bot commented on May 22, 2026

    @melvin-bot

    @mallenexpensify Now this issue is 8 days overdue. Are you sure this should be a Daily? Feel free to change it!

  15. mallenexpensify commented on May 22, 2026

    @mallenexpensify
    Contributor

    @abzokhattab plz complete the BZ checklist and tag me in a post once you have. Thx

  16. abzokhattab commented on May 23, 2026

    @abzokhattab
    Contributor

    BugZero Checklist:

    • [Contributor] The offending PR and associated issue have been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake.

      Link to the comment: https://github.com/Expensify/App/pull/47338/changes#r3245096549

    • [Contributor] If the regression was CRITICAL (e.g. interrupts a core flow) A discussion in #expensify-open-source has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner.

      Link to discussion: N/A

    • [Contributor] If it was decided to create a regression test for the bug, please propose the regression test steps using the template below to ensure the same bug will not reach production again.

    • [BugZero Assignee] Create a GH issue for creating/updating the regression test once above steps have been agreed upon.

      Link to issue: https://github.com/Expensify/Expensify/issues/640779

    Regression Test Proposal

    Precondition:

    • Have an account with copilot/delegate access configured to another account with a lot of data

    Test:

    1. Sign in to the delegator account
    2. Open DevTools > Network tab and filter by OpenApp
    3. Open the account switcher and switch into the copilot/delegate account
    4. Verify exactly one OpenApp request fires (not two)
    5. Switch back to the original account
    6. Verify exactly one OpenApp request fires again
    7. Verify no double-loading/skeleton flashes occur

    Do we agree 👍 or 👎

    Zapier Logs Run ID: 00040eee-dfc5-aed2-e060-58f8a97d2104
  17. abzokhattab commented on May 23, 2026

    @abzokhattab
    Contributor
  18. quinthar commented on Jun 13, 2026

    @quinthar
    Contributor

    Approved $250 to @abzokhattab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions