fix: stabilize pairing code — request once per connection + auto-reconnect with auth reset#18
Open
devin-ai-integration[bot] wants to merge 3 commits into
Open
Conversation
When the proxy drops the Baileys WebSocket during pairing, the close handler was clearing the pairing code and requesting a new one on reconnect. Each requestPairingCode() call invalidates the previous code, so the user's code became invalid before they could enter it. Now the pairing code is preserved in memory across reconnections. The auth state (identity keys) persists, so the original code stays valid. The guard in connectionUpdate prevents re-requesting when a code already exists. Also fixes the reconnect guard to allow reconnection during pairing code mode (phoneNumber set) even without wuid/statusCode. Co-Authored-By: eksucampusmarketplace@gmail.com <eksucampusmarketplace@gmail.com>
Baileys QR refs exhaust after ~5 rotations (~3.5 min) and WhatsApp closes the connection with 428 (connectionClosed). Previously, 428 was in codesToNotReconnect which triggered LOGOUT — killing the pairing session before the user could enter the code. Changes: - Always reconnect during pairing (phoneNumber set, no wuid) regardless of disconnect reason code - Clear pairingCode on reconnect so a fresh valid code is requested (old code is tied to the old socket registration session) - Add 5s delay before pairing reconnect to prevent rapid-fire loops when WhatsApp rate-limits connections Co-Authored-By: eksucampusmarketplace@gmail.com <eksucampusmarketplace@gmail.com>
…auth reset on QR exhaustion - Request pairing code only on first QR event, reuse across QR rotations (prevents code invalidation every 60s when new QR triggers new code) - Track pairingReconnectCount to detect QR ref exhaustion (code 428) - After first reconnect, clear auth state for fresh WhatsApp registration (gets new 3.5-min QR window instead of 1-7 second rate-limited sessions) - Exponential backoff on pairing reconnects (5s, 10s, 15s max) - Reset reconnect counter on successful connection (state=open) - Preserve existing auth on 515 restart (don't break mid-handshake) Co-Authored-By: eksucampusmarketplace@gmail.com <eksucampusmarketplace@gmail.com>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
Fixes the critical issue where pairing codes were being invalidated every 60 seconds, making it impossible for 43+ users to link their WhatsApp devices.
Root Cause
Each QR rotation (every ~60s) triggered
requestPairingCode(), generating a new code and invalidating the previous one on WhatsApp's servers. Users had only ~20s to enter an 8-character code before it was replaced.Changes
Request pairing code only once per connection — guard
if (!this.instance.qrcode.pairingCode)prevents re-requesting on each QR rotation. The code stays valid for the full 3.5-minute connection window.Auto-reconnect with auth reset on QR exhaustion (code 428) — after the initial 3.5-min window, WhatsApp rate-limits reconnections. The fix clears auth state on subsequent reconnects so WhatsApp treats it as a new device registration, allocating a fresh set of QR refs.
Exponential backoff on pairing reconnects — delays increase from 5s → 10s → 15s (capped) to avoid rapid-fire reconnection loops that trigger WhatsApp bans.
Reset reconnect counter on success —
pairingReconnectCount = 0when connection reachesopenstate.Validated
Tested with standalone Baileys script on VPS. Pairing code
2EQRDLA7for +234 number linked successfully — device connected and received messages.🔗 Related Issue
Fixes pairing code invalidation blocking 43+ users from connecting WhatsApp Business devices.
🧪 Type of Change
🧪 Testing
✅ Checklist
📝 Additional Notes
Link to Devin session: https://app.devin.ai/sessions/4caa72c136194ff587244d583c6a847c
Requested by: @eksucampusmarketplace-cell