Skip to content

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
mainfrom
devin/1778689302-stabilize-pairing-code
Open

fix: stabilize pairing code — request once per connection + auto-reconnect with auth reset#18
devin-ai-integration[bot] wants to merge 3 commits into
mainfrom
devin/1778689302-stabilize-pairing-code

Conversation

@devin-ai-integration
Copy link
Copy Markdown

📋 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

  1. 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.

  2. 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.

  3. Exponential backoff on pairing reconnects — delays increase from 5s → 10s → 15s (capped) to avoid rapid-fire reconnection loops that trigger WhatsApp bans.

  4. Reset reconnect counter on successpairingReconnectCount = 0 when connection reaches open state.

Validated

Tested with standalone Baileys script on VPS. Pairing code 2EQRDLA7 for +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

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

  • Manual testing completed — pairing code worked for +234 number
  • Functionality verified in development environment (VPS)
  • No breaking changes introduced — only affects pairing flow
  • Tested with direct Baileys connection and via Evolution API

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios

📝 Additional Notes

  • Companion fix in Botwave repo adds proxy-skip during pairing, timeout increase to 10min, and QR code display on dashboard
  • Local Redis migration (replacing Upstash which hit 500K request limit) was also part of this production fix

Link to Devin session: https://app.devin.ai/sessions/4caa72c136194ff587244d583c6a847c
Requested by: @eksucampusmarketplace-cell

devin-ai-integration Bot and others added 3 commits May 13, 2026 16:21
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>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

1 participant