Repository navigation
Description
Bug type
Behavior bug (incorrect output/state without crash)
Beta release blocker
No
Summary
On a multi-tenant gateway (12 WhatsApp accounts), all 12 channels enter a perpetual health-monitor: restarting (reason: stopped) loop after their underlying WhatsApp Web sessions are invalidated. The health monitor restarts channels regardless of why they stopped, so channels stopped by terminal DisconnectReasons (loggedOut, connectionReplaced, badSession) get re-started indefinitely with no chance of success — they need a fresh QR pairing, not a restart. Over ~20 hours this drove gateway RSS from <500 MB to 12.9 GB peak, CPU to 16 h of 20 h wall time, and RPC agents.files.get latency from <200 ms to 24-25 s, blocking unrelated tenants on the same gateway.
Steps to reproduce
- Run an OpenClaw gateway with multiple WhatsApp Web accounts (
channels.whatsapp.accounts.*) on2026.5.3-1. - Wait for one or more accounts to receive a terminal disconnect from WhatsApp servers — easiest reproductions:
- User logs out the linked device from the phone (Settings → Linked Devices → unlink) →
DisconnectReason.loggedOut(401). - User links a new device that replaces the OpenClaw session →
DisconnectReason.connectionReplaced(440). - Auth state corruption →
DisconnectReason.badSession.
- User logs out the linked device from the phone (Settings → Linked Devices → unlink) →
- Observe that the channel transitions to
running=falseafter the close handler. - Watch
~/.openclaw/logs/gateway.log(or the systemd-user journal) for ~30 min:firing every health-check interval, per channel, with each restart immediately failing the same way.[health-monitor] [whatsapp:cp-tenantN] health-monitor: restarting (reason: stopped) - With 12 stuck channels, observe gateway RSS climb steadily (Baileys WebSocket + Noise handshake state per attempt), event-loop delay rise, and other RPCs degrade.
Expected behavior
- Channels stopped by terminal DisconnectReasons are marked in a state distinct from "stopped" (e.g.
reauth_required) and not restarted by the health-monitor. - A
channel.reauth_requiredevent (or equivalent) is emitted on the gateway event stream so control planes can surface a re-pairing prompt to the operator. channels.statusexposes areauthRequired: booleanfield so downstream consumers can distinguish "needs human action" from "transient outage".- Channels that exit for retryable reasons (
connectionClosed,connectionLost,restartRequired,timedOut) continue to be restarted as today. - A bounded restart budget exists for any restart loop that escapes the DisconnectReason classification (defense-in-depth — see Additional information).
Actual behavior
- Channel exits with
connection: 'close'and a terminalDisconnectReasonfrom Baileys. - Gateway sets the channel to
running=false(status:stopped). - Health monitor wakes on its interval, sees
running=false, and callsstartChannel(). - New connection immediately fails with the same terminal reason (or a
connectionReplaced440 because the previous Noise key is now stale on WhatsApp's side). - Loop continues indefinitely. Per-attempt cost (Baileys auth state + protobuf parsing + WebSocket setup) accumulates: in our case 12 channels × ~one attempt every health-check interval × no upper bound on consecutive failures.
Observed metrics (gateway PID, single instance, 20h window):
- RSS: <500 MB → 12.9 GB peak (~26× growth).
- CPU: 16 h of 20 h (~80%).
agents.files.getRPC p50/p99: ~150 ms / ~24-25 s (normal <200 ms).- Other tenants' control-plane RPCs queued behind the saturated event loop.
OpenClaw version
2026.5.3-1
Operating system
Ubuntu 24.04 (Linux 6.8.0-110-generic), VPS
Install method
npm global, gateway as systemd --user unit (openclaw-gateway.service)
Model
Not relevant to this bug — failure is upstream of any model invocation. (Mixed: Anthropic Claude Opus 4.7, Sonnet 4.6 for tenant agents.)
Provider / routing chain
Not relevant — failure is in the WhatsApp transport before any agent runs.
Additional provider/model setup details
Multi-tenant SaaS control plane: ~12 active WhatsApp accounts on a single gateway, each tied to a distinct tenant. This is the relevant difference from prior single-user reports — the blast radius spans tenants.
Logs, screenshots, and evidence
# 12 channels entering health-monitor restart on the same global sweep
[health-monitor] [whatsapp:cp-tenant1] health-monitor: restarting (reason: stopped)
[health-monitor] [whatsapp:cp-tenant2] health-monitor: restarting (reason: stopped)
[health-monitor] [whatsapp:cp-tenant3] health-monitor: restarting (reason: stopped)
... (12 lines, all within the same second) ...
# RPC latency during saturation vs. recovery
[ws] ⇄ res ✓ agents.files.get 24913ms conn=<redacted> id=<redacted>
[ws] ⇄ res ✓ agents.files.get 24241ms conn=<redacted> id=<redacted>
# After the offending channel was removed from config:
[ws] ⇄ res ✓ agents.files.get 36ms conn=<redacted> id=<redacted>
# Gateway RSS over 20h (sampled via systemd-cgtop / RSS column of /proc/<pid>/status)
00:00 RSS= 462 MB
04:00 RSS= 3.1 GB
08:00 RSS= 6.4 GB
12:00 RSS= 9.8 GB
16:00 RSS= 11.7 GB
20:00 RSS= 12.9 GB ← peak before manual intervention
Impact and severity
- Affected: any operator running >1 WhatsApp account on a single gateway where one or more accounts is invalidated upstream (logout from phone, account ban, device replaced). Severity scales with channel count.
- Severity: high — degrades the entire gateway, not just the affected channel. In multi-tenant deployments, one tenant's logout can cause cross-tenant outage.
- Frequency: 100% reproducible whenever a WhatsApp Web session ends with a terminal
DisconnectReason. Frequency in the wild depends on user behavior; in our population we see it weekly. - Consequence: gateway memory exhaustion, event-loop saturation, missed cross-channel replies, RPC latency that cascades into agent-side timeouts. Recovery currently requires manual gateway restart + per-channel re-pairing.
Additional information
Relation to prior closed issues
This is the same problem family as #48390, #49305, #51342, #54614, #70463, but the reason logged is now stopped (not stale-socket), and the symptom is observed on 2026.5.3-1 after those fixes shipped. Reading the closed-issue history, the post-fix gateway appears to have moved the health-monitor trigger from "stale event timestamp" to "channel running=false" — which closes the stale-socket false positive, but reintroduces the same loop because the health-monitor still cannot tell why a channel is stopped.
The architectural gap: the health-monitor and the close-reason classifier are decoupled. The close handler knows the channel was logged out; that information does not reach the health-monitor.
Proposed fix #1 — terminal DisconnectReason classification (root cause)
Capture connection.update from Baileys and, when connection === 'close', read lastDisconnect.error.output.statusCode (or lastDisconnect.error.data.reason) and map to DisconnectReason:
DisconnectReason |
Code | Action |
|---|---|---|
loggedOut |
401 | Mark channel reauth_required; do not restart; clear stored auth state |
connectionReplaced |
440 | Mark channel reauth_required; do not restart |
badSession |
— | Mark channel reauth_required; clear auth-profiles.json; do not restart |
connectionClosed / connectionLost / restartRequired / timedOut |
408 / 428 / 515 / 408 | Restart as today (retryable) |
multideviceMismatch |
— | Mark reauth_required; do not restart |
Additions to public surface:
channels.statusRPC: add optionalreauthRequired: boolean(and ideallyreauthReason: 'logged_out' | 'connection_replaced' | 'bad_session' | ...). Optional → does not break existing clients.- New gateway event:
channel.reauth_requiredon the event stream, with{ channelId, accountId, reason }payload, so control planes can proactively show a re-pair / new-QR prompt. - When a fresh QR scan completes successfully, clear
reauthRequiredand resume normal health-monitor behavior automatically.
Proposed fix #2 — bounded restart budget (defense-in-depth)
For close reasons that fall outside the mapping (new Baileys constants, unmapped 4xx, undocumented disconnects, or close-before-connection.update-fires races):
- After N consecutive restart attempts (suggested default 5) within a rolling window of X minutes (suggested 10 min), stop restarting.
- Mark the channel
disconnected (max_retries_exceeded); surface the samechannel.reauth_requiredevent so operators see the failure. - Reset the counter when the channel stays connected for Y minutes (suggested 5 min) without a close.
- Make N, X, Y configurable under
gateway.channelHealthMonitor.{maxConsecutiveRestarts, restartWindowMinutes, healthyResetMinutes}.
Why both fixes, not just one:
- Fix fix: add @lid format support and allowFrom wildcard handling #1 alone fails on classes the gateway doesn't yet know about (new Baileys releases, undocumented WhatsApp server codes). Fix Login fails with 'WebSocket Error (socket hang up)' ECONNRESET #2 catches that.
- Fix Login fails with 'WebSocket Error (socket hang up)' ECONNRESET #2 alone preserves the bad UX of silent failure: operators only learn after the budget exhausts (5 attempts × ~30 s ≈ 2-3 min lag) and lose the strong "logged out → please re-pair" signal that Fix fix: add @lid format support and allowFrom wildcard handling #1 provides.
Last known good / first known bad
We have not bisected — this is structural rather than a regression. Prior closed issues in the same family suggest the loop has existed across several versions with different surface symptoms (stale-socket → stopped).
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
devjotaduo commentedon May 6, 2026
erro
clawsweeper commentedon May 6, 2026
Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed June 28, 2026, 5:22 PM ET / 21:22 UTC.
Summary
Keep open: current main still treats non-manual stopped WhatsApp accounts as restartable, and the open linked pull request remains the active but not-yet-merged fix candidate.
Reproducibility: yes. from source inspection: current main preserves WhatsApp logged-out/conflict health states, but generic health-monitor and ChannelManager paths still restart non-manual stopped accounts. I did not run a live WhatsApp unlink or replacement-session test.
Root-cause cluster
Relationship:
canonicalCanonical: #78419
Summary: This issue is the canonical report for the WhatsApp terminal-disconnect restart loop; the open linked PR is the candidate fix, while related merged/auth and Telegram health-monitor work only partially overlap.
Members:
fixed_by_candidate: fix(gateway): stop terminal WhatsApp restart loops #78511 - The PR uses closing syntax for this issue and changes the implicated gateway and WhatsApp terminal-disconnect paths.partial_overlap: fix(whatsapp): preserve auth on terminal disconnects #93076 - The merged PR preserved WhatsApp auth on terminal disconnects but did not add the missing generic no-restart guard.partial_overlap: [Bug]: Gateway crash-loops on a fixed interval under Telegram multi-account churn — health-monitor restart-cascade, often misread as config corruption #89785 - The open Telegram report shares the gateway health-monitor restart-cascade theme, but it names a different channel and reproduction surface.Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.
Next step
No separate ClawSweeper fix job should be queued while #78511 is open as the explicit candidate fix.
Review details
Best possible solution:
Land or replace the linked fix so terminal WhatsApp disconnects suppress both health-monitor and ChannelManager auto-restarts, while deferring new event/config surfaces to explicit maintainer API direction.
Do we have a high-confidence way to reproduce the issue?
Yes, from source inspection: current main preserves WhatsApp logged-out/conflict health states, but generic health-monitor and ChannelManager paths still restart non-manual stopped accounts. I did not run a live WhatsApp unlink or replacement-session test.
Is this the best way to solve the issue?
Unclear for the full requested surface: the linked terminal-disconnect guard is the right narrow direction for the bug, but the public event/status/config additions need maintainer product direction and the open PR still needs its review finding resolved.
AGENTS.md: found and applied where relevant.
Remaining risk / open question:
Codex review notes: model internal, reasoning high; reviewed against 686a2876c7f4.
Label changes
Label changes:
maturity:stable: The affected gateway runtime health/restart surface is part of the M4 Stable gateway-runtime taxonomy surface.Label justifications:
P1: The reported loop can saturate a shared gateway, grow heap sharply, and degrade multi-tenant channel/RPC workflows.impact:crash-loop: The issue is about a health-monitor restart loop that can make the gateway effectively unavailable.impact:message-loss: The report describes missed cross-channel replies and WhatsApp delivery failures while the loop consumes the gateway.impact:session-state: Terminal WhatsApp session states are preserved locally but not carried into restart policy, causing state drift between re-pair-needed and restartable.maturity:stable: The affected gateway runtime health/restart surface is part of the M4 Stable gateway-runtime taxonomy surface.Evidence reviewed
What I checked:
src/gateway/channel-health-policy.ts:63, 686a2876c7f4)src/gateway/channel-health-monitor.ts:168, 686a2876c7f4)src/gateway/server-channels.ts:722, 686a2876c7f4)extensions/whatsapp/src/auto-reply/monitor-state.ts:15, 686a2876c7f4)Likely related people:
src/gateway/channel-health-monitor.ts,src/gateway/server-channels.ts,extensions/whatsapp/src/auto-reply/monitor-state.ts)extensions/whatsapp/src/auto-reply/monitor.ts,extensions/whatsapp/src/connection-controller.ts)src/gateway/channel-health-monitor.ts,src/gateway/channel-health-policy.ts)src/gateway/channel-health-policy.ts,src/gateway/channel-health-monitor.ts,src/gateway/server-channels.ts)How this review workflow works
@clawsweeper re-reviewcomments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.@clawsweeper re-reviewor@clawsweeper re-runon an open PR or issue to request a fresh review only.@clawsweeper reviewto request a fresh review only.@clawsweeper autofix,@clawsweeper automerge,@clawsweeper fix ci, or@clawsweeper address review.@clawsweeper explainto ask for more context, or@clawsweeper stopto stop active automation.rutherlesdev commentedon May 6, 2026
Responses to the open API-surface questions from a control-plane perspective:
Is existing
healthStateenough?Mostly yes —
'conflict' | 'logged-out' | 'disconnected'already carries everything a control plane needs to render a "please re-pair" UI. The only friction today is thathealthStateappears underlastDisconnectrather than as a top-level field onchannels.status(happy to be corrected if I missed it).If a new field lands, prefer a discriminated reason over a boolean:
Lets us show the right operator copy ("phone unlinked" vs. "another device replaced this session" vs. "credentials corrupted — re-pair") without log-string matching.
Event-stream signal: a
channel.status_changedevent with the new state would let control planes drop theirchannels.statuspolling. Not a blocker — happy to poll.Restart budget: flexible on defaults. Our incident would have been contained by max 5 consecutive failed restarts within 10 minutes, reset after 5 minutes connected. Configurability nice-to-have, not a release blocker.
Scope split, if useful:
channel-health-monitor.tsskips accounts whererunning=falseANDhealthStateis terminal, with the regression test outlined in the triage.Happy with either: keep as one issue, or split.
barnacle-openclaw commentedon May 31, 2026
This issue has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.
fix(gateway): stop terminal WhatsApp restart loops (openclaw#78511)
fix(gateway): stop terminal WhatsApp restart loops (openclaw#78511)