#17487: fix: WhatsApp connection stability - continue reconnection after max attempts
channel: whatsapp-web
stale
size: XS
Cluster:
WhatsApp Connection Stability Fixes
## Summary
Instead of permanently stopping after max reconnection attempts (default 12), the WhatsApp gateway monitor now continues with periodic recovery attempts using the heartbeat interval. This allows the WhatsApp connection to automatically recover without requiring manual gateway restart after transient disconnections that occur after long uptime (8-12 hours).
## Changes
- Modified `src/web/auto-reply/monitor.ts`: When max reconnection attempts are reached, the code now continues with periodic recovery attempts (every 60 seconds by default) instead of breaking out of the monitoring loop entirely.
- The reconnection logic now distinguishes between initial reconnection attempts (with exponential backoff) and recovery attempts (with fixed interval).
## Testing
- Existing tests pass (reconnect.test.ts, session.test.ts)
- The fix is backward compatible - normal reconnection behavior is unchanged
- Only the behavior after max attempts is modified to allow automatic recovery
Fixes openclaw/openclaw#17475
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR changes the WhatsApp reconnection behavior so the gateway no longer permanently stops after exhausting max reconnection attempts (default 12). Instead, it transitions to periodic recovery attempts at a fixed interval (`heartbeatSeconds`, default 60s). This addresses a real operational pain point where transient disconnections after long uptime required manual restarts.
- The core logic change in `monitorWebChannel` replaces the `break` after max attempts with a fixed-interval retry using `heartbeatSeconds * 1000` as the delay, while preserving exponential backoff for initial attempts.
- The existing `reconnectAttempts` reset at line 348 (when uptime exceeds heartbeat interval) ensures the counter resets after a successful recovery, restoring normal backoff behavior.
- The warn-level log + `runtime.error()` in the `maxAttemptsReached` branch fires on **every** recovery cycle (not just the first), which will produce repetitive log output during extended outages.
- The existing e2e test `"stops after hitting max reconnect attempts"` expects `monitorWebChannel` to return after max attempts. With this change, the loop continues indefinitely, which will cause that test to hang (as noted in previous review thread).
<h3>Confidence Score: 3/5</h3>
- The reconnection logic change itself is correct and addresses a real issue, but an existing e2e test will break and logging could be improved.
- Score of 3 reflects that the core logic change is sound (proper backoff differentiation, counter reset works correctly, cleanup is handled), but the PR has an unresolved test compatibility issue (the "stops after hitting max reconnect attempts" e2e test will hang) and produces unnecessarily verbose logging during extended outages. The behavioral change is intentional and well-motivated, but the test needs updating before this can be safely merged.
- `src/web/auto-reply/monitor.ts` — verify that the e2e test `"stops after hitting max reconnect attempts"` is updated to reflect the new never-terminate behavior, and consider reducing log verbosity during periodic recovery.
<sub>Last reviewed commit: 043e542</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#9727: fix(whatsapp): retry reconnect loop on initial connection failure
by luizlf · 2026-02-05
88.1%
#22367: fix(whatsapp): prevent permanent listener loss after abort during r...
by mcinteerj · 2026-02-21
80.9%
#9515: fix(web): retry WhatsApp 515 restart up to 3 times with delay
by Sebachowa · 2026-02-05
79.7%
#22143: Fix memory leak in WhatsApp channel reconnection loop
by lancejames221b · 2026-02-20
78.8%
#16628: feat(web): implement three-tier graduated retry strategy
by KrE80r · 2026-02-14
76.5%
#3071: fix: WhatsApp 515 error retry not triggering
by rabsef-bicrym · 2026-01-28
76.1%
#19303: Fix WhatsApp internal error leakage + cron.run timeout defaults
by koala73 · 2026-02-17
75.7%
#16923: fix(web): resolve stale socket race condition in WhatsApp auto-reply
by dorukardahan · 2026-02-15
75.7%
#6302: fix: Add timeouts to prevent indefinite hangs (issues #4954, #4956,...
by batumilove · 2026-02-01
75.1%
#12870: fix: recover from telegram fetch errors (issue #12835)
by ambicuity · 2026-02-09
74.1%