Skip to content

[Due for payment 2026-05-12] [$250] Don't fire duplicate ReconnectApp on delegate account switch #89271

Closed

Description

@MelvinBot

This issue tracks the fix for duplicate ReconnectApp requests being fired when switching accounts via delegate access.

When a user switches accounts via delegate access, the app sends OpenApp and then an unnecessary ReconnectApp right after. The OpenApp already performs a full sync, making the subsequent ReconnectApp wasted work.

PR: #89262


cc @mountiny @TMisiukiewicz @abzokhattab

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

Activity

  1. added
    Reviewing
    Daily
    Bug
    on Apr 30, 2026
  2. MelvinBot commented on Apr 30, 2026

    @MelvinBot
    ContributorAuthor

    Issue Analysis

    Root Cause: Race condition in Onyx update ordering during delegate account switching. Onyx.clear() wipes LAST_FULL_RECONNECT_TIME to empty, then when the OpenApp response arrives, applyHTTPSOnyxUpdates processes response.onyxData (containing server's NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE) before successData (which sets LAST_FULL_RECONNECT_TIME). This causes subscribeToFullReconnect to see '' < serverTimestamp → true, firing a spurious ReconnectApp.

    Recommended Fix: PR #89262 correctly addresses this by seeding LAST_FULL_RECONNECT_TIME with the current time before Onyx.clear() and preserving it through the clear, so the timestamp is already satisfied when the server NVP arrives.

    Investigation details

    Data flow:

    1. User initiates delegate switch → Delegate.connect() calls Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS) — LAST_FULL_RECONNECT_TIME is NOT in the preserve list, so it gets wiped to ''
    2. openApp() fires and server responds
    3. applyHTTPSOnyxUpdates processes response.onyxData first → NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE gets a real timestamp
    4. Onyx subscriber in subscribeToFullReconnect.ts fires: '' < serverTimestamp → triggers reconnectApp() (duplicate)
    5. Only then does successData set LAST_FULL_RECONNECT_TIME via App.ts:371-377 — too late

    Alternative explanations considered:

    • Network reconnect or visibility change triggering ReconnectApp independently — ruled out; the timing (immediately after OpenApp response) points to the Onyx ordering race
    • Multiple openApp() calls — no evidence of this in the delegate flow

    PR assessment: The fix in #89262 is correct and well-targeted. The new clearOnyxForDelegateTransition() helper sets LAST_FULL_RECONNECT_TIME to current time via Onyx.merge, then passes it as a preserved key to Onyx.clear(). Applied to all four relevant call sites: connect(), disconnect(), restoreDelegateSession(), and setupNewDotAfterTransitionFromOldDot().


    Next Steps: Reply with @MelvinBot implement this to create a draft PR, or @MelvinBot <your feedback> to refine this analysis.

  3. added
    External
    on Apr 30, 2026
  4. added
    Help Wanted
    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 [-]Don't fire duplicate ReconnectApp on delegate account switch[/-] [+][$250] Don't fire duplicate ReconnectApp on delegate account switch[/+] on Apr 30, 2026
  7. melvin-bot commented on Apr 30, 2026

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

    @oqildev
    Contributor

    Proposal

    Root cause?

    Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS) wipes both LAST_FULL_RECONNECT_TIME and NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE to ''. Then openApp() fires, and when the response arrives applyHTTPSOnyxUpdates applies onyxData before successData. This means NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE gets set to a server timestamp first — the subscribeToFullReconnect subscriber fires with '' < serverTimestamp → reconnectApp() fires. Only afterwards does successData set LAST_FULL_RECONNECT_TIME to current time — too late.

    The race isn't delegate-specific; it's structural: openApp() is already doing a full sync and will stamp LAST_FULL_RECONNECT_TIME via successData, but the NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE check fires before that stamp lands.

    What changes are needed?

    Guard doFullReconnectIfNecessary() in subscribeToFullReconnect.ts with an isLoadingApp check. While openApp() is in progress (IS_LOADING_APP = true), a ReconnectApp is by definition redundant — the full sync is already underway and successData will set LAST_FULL_RECONNECT_TIME. Defer the check until loading completes:

    // subscribeToFullReconnect.ts
    
    let isLoadingApp = false;
    Onyx.connectWithoutView({
        key: ONYXKEYS.IS_LOADING_APP,
        callback: (value) => {
            isLoadingApp = value ?? false;
            // Re-evaluate once openApp() finishes — catches the case where
            // successData never ran (e.g. network failure) and LAST_FULL_RECONNECT_TIME is still empty
            if (!isLoadingApp) {
                doFullReconnectIfNecessary();
            }
        },
    });
    
    function doFullReconnectIfNecessary() {
        if (lastFullReconnectTime >= reconnectAppIfFullReconnectBefore) {
            return;
        }
        if (isLoadingApp) {
            // openApp() is in progress and will set LAST_FULL_RECONNECT_TIME via successData
            return;
        }
        Log.info('Full reconnect triggered', false, {lastFullReconnectTime, reconnectAppIfFullReconnectBefore});
        reconnectApp();
    }
    

    Why this over alternatives?

    • Single file, no new helpers: Everything lives in subscribeToFullReconnect.ts. No need to touch KEYS_TO_PRESERVE_DELEGATE_ACCESS, no new wrapper function, no changes across 4 call sites in Delegate.ts.
    • Fixes the conceptual invariant, not the symptom: ReconnectApp should never fire while openApp() is running — that's the invariant. Encoding it here is cleaner than pre-seeding LAST_FULL_RECONNECT_TIME before every Onyx.clear() call.
    • Handles openApp() failure correctly: If openApp() fails and successData never runs, IS_LOADING_APP transitions to false via finallyData, which re-triggers doFullReconnectIfNecessary(). At that point LAST_FULL_RECONNECT_TIME is still empty and reconnect fires correctly — better behavior than the status quo.
    • Not delegate-specific: Works for any flow that calls openApp(), protecting against the same race in future contexts.

    Tests:

    • Unit test in subscribeToFullReconnect tests: set NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE while IS_LOADING_APP=true → verify reconnectApp() is NOT called.
    • Verify reconnectApp() IS called once IS_LOADING_APP becomes false with LAST_FULL_RECONNECT_TIME still empty (failure path).
    • Regression: normal network recovery (IS_LOADING_APP=false) still triggers reconnect correctly.
  9. ProjectNOVACo commented on Apr 30, 2026

    @ProjectNOVACo

    Proposal

    Please re-state the problem that we are trying to solve in this issue.
    The app fires duplicate ReconnectApp requests when switching accounts via delegate access, which is unnecessary since the OpenApp call already performs a full sync.

    What is the root cause of that problem?
    I dug into this and found that the issue starts in src/libs/actions/Delegate.ts. When we switch accounts using delegate access, Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS) wipes both LAST_FULL_RECONNECT_TIME and NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE to empty strings. Then when the OpenApp response arrives in src/libs/actions/OnyxUpdates.ts, the function applyHTTPSOnyxUpdates applies response.onyxData (which contains the server's timestamp for NVP_RECONNECT_APP_IF_FULL_RECONNECT_BEFORE) before it processes successData (which sets LAST_FULL_RECONNECT_TIME). This causes a race condition where subscribeToFullReconnect.ts triggers a reconnectApp() call unnecessarily.

    What changes do we need to make in order to solve the problem?
    We should modify src/libs/actions/Delegate.ts so when switching accounts, LAST_FULL_RECONNECT_TIME is set to a current timestamp right after clearing Onyx keys but before triggering the OpenApp. This will prevent subscribeToFullReconnect.ts from thinking we need a full reconnect.

    Here's how we can fix it:

    // In src/libs/actions/Delegate.ts, around line 196-203
    Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS).then(() => {
    // Set LAST_FULL_RECONNECT_TIME to current time before OpenApp is triggered
    return Onyx.merge(ONYXKEYS.LAST_FULL_RECONNECT_TIME, DateUtils.getDBTime());
    }).then(openApp);

    That the timestamp for LAST_FULL_RECONNECT_TIME is always up-to-date right after clearing and before any checks in subscribeToFullReconnect.ts, preventing unnecessary duplicate ReconnectApp calls.

  10. ProjectNOVACo commented on Apr 30, 2026

    @ProjectNOVACo

    📋 Contributor details

  11. 12 remaining items

  12. melvin-bot commented on May 5, 2026

    @melvin-bot

    @abzokhattab Eep! 4 days overdue now. Issues have feelings too...

  13. added
    Awaiting Payment
    on May 7, 2026
  14. melvin-bot commented on May 7, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  15. added
    Reviewing
    Weekly
    and removed
    Daily
    on May 7, 2026
  16. melvin-bot commented on May 12, 2026

    @melvin-bot

    Payment Summary

    Upwork Job

    BugZero Checklist (@mallenexpensify)

    • (if NewFeature) I have created a PR for any necessary HelpDot updates, or I confirmed no updates are necessary.
    • I have verified the correct assignees and roles are listed above and updated the necessary manual offers
    • I have verified that there are no duplicate or incorrect contracts on Upwork for this job (https://www.upwork.com/ab/applicants/2049849402501236092/hired)
    • I have verified the PR was not reverted
    • I have applied any discounts due to bugs/regressions introduced by this PR
    • I have paid out the Upwork contracts or cancelled the ones that are incorrect
    • I have verified the payment summary above is correct
  17. added
    Daily
    and removed
    Weekly
    on May 13, 2026
  18. mallenexpensify commented on May 13, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @abzokhattab due $250 via NewDot

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

  19. abzokhattab commented on May 15, 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 on the PR: https://github.com/Expensify/App/pull/47338/changes#r3245089692

    • [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/637603

    Regression Test Proposal

    Precondition:

    • Have an account with delegate/copilot access configured

    Test:

    1. Open DevTools → Network tab, filter by ReconnectApp
    2. Open the account switcher and switch into a delegate account
    3. Verify ReconnectApp is NOT called after OpenApp
    4. Switch back to the original account
    5. Verify ReconnectApp is NOT called after OpenApp

    Do we agree 👍 or 👎

    Zapier Logs Run ID: 00040eee-dc0a-ab56-e027-81933595e194
  20. quinthar commented on Jun 7, 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 PaymentBugDailyExternalReviewing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions