Skip to content

fix(whatsapp-gateway): reconnect on all non-logout disconnections#556

Open
f-liva wants to merge 1 commit intoRightNow-AI:mainfrom
f-liva:fix/whatsapp-gateway-reconnect
Open

fix(whatsapp-gateway): reconnect on all non-logout disconnections#556
f-liva wants to merge 1 commit intoRightNow-AI:mainfrom
f-liva:fix/whatsapp-gateway-reconnect

Conversation

@f-liva
Copy link

@f-liva f-liva commented Mar 12, 2026

Summary

  • Treat all disconnect reasons except loggedOut as recoverable — the gateway now reconnects automatically on connectionClosed (428), connectionLost (408), connectionReplaced (440), badSession, and all other Baileys disconnect codes
  • Exponential backoff (2s → 3s → 4.5s → ... capped at 60s) replaces the fixed 2s retry delay, with counter reset on successful connection
  • Auto-connect on startup when auth_store/creds.json exists from a previous session, eliminating the need for a manual POST /login/start after process restarts

Problem

The gateway only reconnected on restartRequired and timedOut. All other disconnect codes silently killed the connection permanently. Combined with no auto-connect on startup, any transient network issue or process restart meant permanent disconnection until manual intervention.

Real-world impact: the gateway would show connected: false on /health but never recover, silently dropping all incoming WhatsApp messages.

Closes #555

Test plan

  • Start gateway with existing auth_store/ credentials → should auto-connect without POST /login/start
  • Simulate network interruption (e.g., block WhatsApp IPs briefly) → gateway should reconnect with increasing delays visible in logs
  • Verify backoff resets to 0 after successful reconnection
  • Verify loggedOut still clears auth store and stops (no reconnect loop)
  • Verify /health endpoint reflects correct connected status during reconnection attempts

🤖 Generated with Claude Code

…h exponential backoff

The gateway only reconnected on `restartRequired` and `timedOut` disconnect
reasons. All other Baileys disconnect codes (connectionClosed/428,
connectionLost/408, connectionReplaced/440, multideviceMismatch, etc.) fell
through to the else branch which treated them as "QR expired" and stopped
trying. This caused the gateway to silently die after any transient network
issue or WhatsApp server-side session refresh.

Changes:
- Treat every disconnect reason except `loggedOut` as recoverable
- Add exponential backoff (2s base, 1.5x multiplier, 60s cap) instead of
  a fixed 2s delay
- Reset backoff counter on successful connection
- Auto-connect on startup when auth credentials exist from a previous
  session, eliminating the need for a manual POST /login/start after
  process restarts

Closes RightNow-AI#529

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

WhatsApp gateway silently disconnects and never reconnects

1 participant