Skip to content

Fix web leader-election deadlock with Web Locks (SequentialQueue unable to flush) - #104990

Merged
mountiny merged 3 commits into
Expensify:mainfrom
callstack-internal:eliran/94963-leader-web-locks
Oct 31, 2026
Merged

mountiny merged 3 commits into
Expensify:mainfrom
callstack-internal:eliran/94963-leader-web-locks

Conversation

@elirangoshen

@elirangoshen elirangoshen commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

On web and PWA, the app sometimes stops sending anything. Messages stay greyed out, the Inbox loading bar spins forever, and the logs show [SequentialQueue] Unable to flush. Client is not the leader. on repeat.

Why it happens. Only one tab (the "leader") sends the shared request queue. Today the leader is whichever client ID is last in the ACTIVE_CLIENTS list in Onyx. A tab removes its ID only in beforeunload, which doesn't run when a tab crashes or the browser kills it in the background. The dead tab's ID can stay last, and then the tab that is still open never sends anything.

What this PR does. The leader is now the tab holding the activeClientLeader Web Lock:

  • The browser lets only one tab hold the lock, and releases it when that tab closes or crashes. A tab that is still open then takes over, so a dead tab can't block the queue.
  • Every init() (page load or tab focus) takes the lock, so the newest or focused tab still leads, same as before. Other tabs wait for the lock.
  • Sign-in doesn't change who leads, so several tabs can't decide they're the leader at the same moment and send the same request.
  • During a deploy, tabs from the old build still pick the leader from ACTIVE_CLIENTS. A new-build ID starts with webLock_. While an old-build ID is last in the list, the lock holder steps aside, and it takes leadership back on its next init(). This way only one tab leads while old and new builds are open together.
  • The leader still saves its ID to ACTIVE_CLIENTS, so SignInPage keeps showing "Another login page is open" in the other tabs.
  • Browsers without Web Locks keep the old logic. Native is unchanged (always the leader).

contributingGuides/SEQUENTIAL_QUEUE.md is updated to match.

Fixed Issues

$ #94963
PROPOSAL: N/A — internal bug fix.

Automated Tests

New tests/unit/ActiveClientManagerTest.ts runs several simulated tabs against a shared fake lock manager. It covers:

  • a single tab still leads when a stale ID is last in ACTIVE_CLIENTS
  • the newest tab is the only leader
  • 7 tabs calling init() at once give exactly one leader
  • when the leader tab closes, a tab that is still open takes over
  • a focused tab takes leadership back
  • the lock holder steps aside while an old-build tab leads, then takes over again
  • the fallback without Web Locks

Tests

Web (Chrome), against staging:

  1. Sign in, open the Concierge chat, and send a message. Verify it's sent (not greyed out).
  2. Open the same chat in a second tab and send a message from it. Verify it shows up once in both tabs, and the Network tab shows a single AddComment.
  3. Close the second tab, then send a message from the first tab. Verify it's sent.
  4. Sign in with user A in Chrome and user B in a different browser.
    1. Send a message from user B to user A.
    2. In Chrome, open the chat with user B in two tabs.
    3. Send a message to user B from the second tab.
    4. Open Chrome's Task Manager, select the second tab, and click "End task".
    5. Send a message from user B. Verify it arrives in the first tab.
    6. Send a message from the first tab. Verify it's sent (not greyed out). Before this PR it stayed greyed out.
  5. Sign out. Open the sign-in page in two separate browser windows placed side by side, so both stay visible. Reload the page in the second window. Verify the first window shows "Another login page is open" and the second shows the form. (Switching tabs inside one window doesn't show the message, because the tab you switch to becomes the leader and shows the form.)
  6. In the second window, sign in with an existing account, wait for the timer, then tap "Didn't receive a security code?". Verify the Network tab shows a single RequestNewValidateCode and only one new email arrives.
  7. Close the second window. Verify the first window now shows the sign-in form.
  • Verify that no errors appear in the JS console

Offline tests

  1. With two tabs open, go offline and send a message from one of them. Verify it shows as pending.
  2. Go back online. Verify the message is sent once, with a single AddComment across both tabs.

QA Steps

These steps can be done on a phone. Use mWeb Safari (iOS) and mWeb Chrome (Android), then repeat once on desktop Chrome / Safari.

  1. Open staging.new.expensify.com, sign in, open any chat, and send a message. Verify it's sent (not greyed out).
  2. Open the same URL in a second browser tab and send a message from it. Switch back to the first tab. Verify the message shows up once.
  3. Send a message from the first tab. Verify it shows up once in both tabs.
  4. Close the second tab from the tab switcher. Send a message from the first tab. Verify it's sent and the app keeps loading new messages.
  5. Sign out. On desktop, open staging.new.expensify.com in two separate browser windows placed side by side, so both stay visible. Reload the page in the second window. Verify the first window says "Another login page is open" and the second shows the sign-in form. (Switching tabs inside one window doesn't show the message, because the tab you switch to becomes the leader and shows the form.) On mWeb, skip this step: only one tab is visible at a time, so the message can't appear.
  6. In the second window, enter the email of an existing account and continue. Verify only one magic code email arrives.
  7. Wait for the timer and tap "Didn't receive a security code?". Verify only one more email arrives (not one per open tab).
  8. Desktop Chrome only: sign in with user A in Chrome and user B in a different browser. Send a message from user B to user A. In Chrome, open the chat with user B in two tabs and send a message to user B from the second tab. Open Chrome's Task Manager, select the second tab, and click "End task". Send a message from user B and verify it arrives in the first tab. Then send a message from the first tab and verify it's sent.
  9. iOS and Android apps: send a message, go offline, send another, and go back online. Verify both are sent once. This is a check that nothing changed on native.
  • 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 verified any copy / text that was added to the app 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 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 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.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari 01-tab1-message-sent 04-tab1-sends-after-tab2-closed 05a-older-tab-another-login-page-open 05b-newest-tab-shows-sign-in-form 06-magic-code-page 07-new-code-requested-once 02-tab1-shows-tab2-message-once 03a-tab1-after-sending 03b-tab2-shows-it-once

… queue

The leader used to be whichever client ID was last in ACTIVE_CLIENTS. A tab that
closed without cleaning up could leave its ID last, and the live tab then never
flushed the SequentialQueue. The leader is now the tab holding the
activeClientLeader Web Lock, which the browser releases when the tab goes away.

Each init() steals the lock, so the newest or focused tab still leads. While
tabs from an older build are open, the lock holder steps aside when an older
build's ID is last in ACTIVE_CLIENTS, so the two builds never both send.
Browsers without Web Locks keep the old behavior.
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/libs/ActiveClientManager/index.native.ts 100.00% <100.00%> (ø)
src/libs/Network/index.ts 93.33% <100.00%> (+0.47%) ⬆️
src/libs/ActiveClientManager/index.ts 86.86% <96.87%> (+86.86%) ⬆️
... and 50 files with indirect coverage changes

@elirangoshen
elirangoshen marked this pull request as ready for review October 26, 2026 13:19
@elirangoshen
elirangoshen requested review from a team as code owners October 26, 2026 13:19
@melvin-bot
melvin-bot Bot requested review from thesahindia and removed request for a team October 26, 2026 13:19
@melvin-bot

melvin-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

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

@melvin-bot
melvin-bot Bot requested review from joekaufmanexpensify and removed request for a team October 26, 2026 13:19
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T13:37:42.154655Z fb4e3d2 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread src/libs/ActiveClientManager/index.ts Resolved

@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: fb4e3d24ec

ℹ️ 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/ActiveClientManager/index.ts Resolved
The Web Lock is granted asynchronously, so the flush that runs when a tab
gets focus can give up before the tab leads, and a waiting tab that takes
over after the leader closes never flushed at all. ActiveClientManager now
calls onBecomeLeader callbacks each time the tab becomes the leader, and the
Network flushes the SequentialQueue from there.
@joekaufmanexpensify

Copy link
Copy Markdown
Contributor

No product review needed.

@joekaufmanexpensify
joekaufmanexpensify removed their request for review October 26, 2026 20:50
@elirangoshen

Copy link
Copy Markdown
Contributor Author

the reassure tests seems flaky as I notice other pr's have this fail too

@elirangoshen

Copy link
Copy Markdown
Contributor Author

Hi @thesahindia is there an update about it ?

@thesahindia

thesahindia commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

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 any copy / text that was added to the app 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 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 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.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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 Screenshot 2026-11-01 at 2 37 26 AM
Android: mWeb Chrome
Screen.Recording.2026-11-01.at.3.05.04.AM.mov
iOS: HybridApp Screenshot 2026-11-01 at 4 02 23 AM
iOS: mWeb Safari
Screen.Recording.2026-11-01.at.2.58.35.AM.mov
MacOS: Chrome / Safari
Screen.Recording.2026-10-29.at.2.25.32.AM.mov
Screen.Recording.2026-10-29.at.2.18.34.AM.mov
Screen.Recording.2026-10-29.at.2.15.55.AM.mov

@thesahindia

Copy link
Copy Markdown
Member

@elirangoshen, please add these steps as well -

  1. Login with user A on chrome and user B on a different browser
  2. Send a message from user B to user A
  3. Open the chat of user B in two tabs on chrome
  4. Send a message to user B from the second tab
  5. Go to chrome "Task manager" > Select the second tab and click "End task"
  6. Send a message from user B
  7. Verify you received the message in the first tab

@thesahindia

Copy link
Copy Markdown
Member

Sign out. Open staging.new.expensify.com in 3 tabs. Verify only the most recently opened tab shows the sign-in form, and the others say "Another login page is open".

Is there anything specific that needs to be done to get "Another login page is open" ?

Screen.Recording.2026-10-29.at.2.37.11.AM.mov

@VickyStash

Copy link
Copy Markdown
Contributor

Note: @elirangoshen is OOO today and will continue to work on this one tomorrow!

@elirangoshen

Copy link
Copy Markdown
Contributor Author

Sign out. Open staging.new.expensify.com in 3 tabs. Verify only the most recently opened tab shows the sign-in form, and the others say "Another login page is open".

Is there anything specific that needs to be done to get "Another login page is open" ?

Screen.Recording.2026-10-29.at.2.37.11.AM.mov

Hi, The message only appears in a tab that's visible but not the leader. Switching to a tab makes it the leader, so in a single window every tab you switch to shows the form (same as on main). To see it: open the sign-in page in two separate windows side by side, then reload one of them. The other window should show "Another login page is open".

I updated the Tests and QA steps to make this clearer.

@elirangoshen

Copy link
Copy Markdown
Contributor Author

@elirangoshen, please add these steps as well -

  1. Login with user A on chrome and user B on a different browser
  2. Send a message from user B to user A
  3. Open the chat of user B in two tabs on chrome
  4. Send a message to user B from the second tab
  5. Go to chrome "Task manager" > Select the second tab and click "End task"
  6. Send a message from user B
  7. Verify you received the message in the first tab

done

@melvin-bot
melvin-bot Bot requested review from luacmartins and mountiny October 31, 2026 22:33

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

Thanks! Looks good to me, i will move this ahead to land the fix sooner than later

@mountiny
mountiny merged commit 1191e7b into Expensify:main Oct 31, 2026
37 of 44 checks passed
@OSBotify OSBotify mentioned this pull request Nov 2, 2026
75 tasks done
@OSBotify

OSBotify commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.5.28-4 🚀

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

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update needed. This PR fixes an internal bug in how browser tabs pick which one sends queued requests, so messages no longer get stuck. It adds no user-facing feature, and no help article describes this behavior.


view run

@OSBotify

OSBotify commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.5.30-0 🚀

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

@OSBotify

OSBotify commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/blimpich in version: 9.5.30-0 🚀

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