fix: prevent stale change:ref listener from triggering false QR disconnect#127090
fix: prevent stale change:ref listener from triggering false QR disconnect#127090Adi1231234 wants to merge 1 commit intowwebjs:mainfrom
Conversation
c22c185 to
f6b972a
Compare
|
Hey @purpshell @BenyFilho 👋 Would any of you have a chance to review this? It fixes a pretty annoying bug where QR scan works but the client disconnects because an old listener fires. Happy to hop on Discord or chat if anything needs clarifying! |
Need user tests to validate it, and update your changes with branch main |
f6b972a to
5c2a4ce
Compare
|
Hey @BenyFilho, just rebased onto the latest main, everything is up to date and CI is green. I keep having to rebase these because other PRs get merged and create conflicts with my open ones. Would love to get them merged so I can stop chasing this 😅 About the user tests you mentioned, what exactly do you mean by that? I'm happy to help with whatever is needed to get these moving. |
Need user tests to validate it, and update your changes with branch main |
5c2a4ce to
1606b19
Compare
the change:ref listener on AuthStore.Conn was never removed after a successful QR scan. When WhatsApp Web clears the ref after authentication begins, the listener would fire onQRChangedEvent with an invalid ref, causing a spurious 'Max qrcode retries reached' disconnect. three fixes: 1. Reset qrRetries when loading_screen fires 2. Guard against null/undefined ref in the change:ref callback 3. Remove the change:ref listener once change:hasSynced fires
1606b19 to
18cdaf8
Compare
|
Hey @BenyFilho, thanks for the follow up! |
|
Closing in favor of #201653, which consolidates this PR together with #127083 into a single, more comprehensive fix. The new PR includes all the changes from both PRs plus additional fixes (listener cleanup, concurrency guard, atomic hasSynced check, qrRetries reset) - all validated with A/B diagnostic testing across 5 real-world scenarios. Thanks everyone for the feedback and reviews here - it really helped shape the final solution! |
Fixes #127089
Problem
Two related bugs in the QR handling code:
The
change:reflistener onAuthStore.Connis never removed after a successful QR scan. When WhatsApp Web clears the ref during/after authentication (setting it to null/undefined), the listener firesonQRChangedEventwith an invalid ref, incrementingqrRetriespastqrMaxRetriesand triggering a spurious"Max qrcode retries reached"disconnect while authentication is already in progress.The
qrRetriescounter never resets. Pre-scan QR refreshes consume retry attempts, so any post-scan QR event (even legitimate) immediately hits the limit. If linking fails and WA Web falls back to QR mode, the user gets zero retry attempts.Changes
Three layered fixes in
src/Client.js, each addressing a different aspect:Reset
qrRetriesonloading_screen: When the loading screen fires (QR was successfully scanned and authentication begins), reset the counter to 0. If the linking fails and WA Web falls back to QR, the user gets a fresh set of retry attempts.Null guard on
change:ref: SkiponQRChangedEventwhen ref is null/undefined. Prevents malformed QR strings (starting with"undefined,") from being emitted and counted as retry attempts.Listener cleanup on
change:hasSynced: Remove thechange:reflistener when authentication completes (change:hasSyncedonAuthStore.AppState, which is the same object asWAWebSocketModel.Socket). Prevents any stale QR events from firing after the client is fully connected.How it was found
Production logs showed a client reaching loading 95-99% after a successful QR scan, then disconnecting 3 minutes later. The final QR event had
qrLength=146(vs normal239), confirming the ref was the literal string"undefined". TheqrRetriescounter (already at 4 from normal pre-scan QR refreshes) was pushed to 5, exceedingqrMaxRetries=4.