fix: recover zombie connections on KeepAliveTimeout - #160
Open
FlavioPulli wants to merge 1 commit into
Open
Conversation
With EnableAutoReconnect disabled, a TCP connection that dies silently (no FIN/RST from the server) never emits events.Disconnected: keepalive pings just keep timing out and the instance stays flagged as connected while being unable to send or receive — a zombie that only a manual restart fixes. whatsmeow's docs explicitly suggest using KeepAliveTimeout to force a faster disconnect+reconnect. Handle KeepAliveTimeout and KeepAliveRestored: both are forwarded to the CONNECTION webhook subscription so operators can observe socket health, and after 3 consecutive timeouts the instance is restarted through the same non-blocking ReconnectClient path the Disconnected handler already uses (exact-match on the count so ongoing restarts aren't duplicated).
Reviewer's GuideHandle whatsmeow keepalive timeout/restored events to surface socket health via CONNECTION webhooks and automatically restart zombie instances after repeated timeouts, while wiring the new events into global queue routing. Sequence diagram for keepalive timeout handling and zombie connection recoverysequenceDiagram
participant WhatsmeowClient
participant MyClient
participant whatsmeowService
WhatsmeowClient->>MyClient: events.KeepAliveTimeout
MyClient->>MyClient: myEventHandler
MyClient->>whatsmeowService: CallWebhook (eventType KeepAliveTimeout)
whatsmeowService->>whatsmeowService: sendToQueueOrWebhook (subscription CONNECTION)
alt [ErrorCount == 3]
MyClient->>whatsmeowService: ReconnectClient(instanceID)
end
WhatsmeowClient->>MyClient: events.KeepAliveRestored
MyClient->>MyClient: myEventHandler
MyClient->>whatsmeowService: CallWebhook (eventType KeepAliveRestored)
whatsmeowService->>whatsmeowService: sendToQueueOrWebhook (subscription CONNECTION)
Flow diagram for routing keepalive events to CONNECTION global queuesflowchart LR
A[Receive eventType] --> B{eventType in
Connected / PairSuccess /
TemporaryBan / LoggedOut /
ConnectFailure / Disconnected /
KeepAliveTimeout / KeepAliveRestored}
B -->|yes| C[Set globalEventType = CONNECTION]
C --> D[SendToGlobalQueues]
B -->|no| E[Route using other mappings]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The
KeepAliveTimeoutlogic hardcodes the restart threshold at exactly 3 timeouts; consider making this threshold configurable (or at least a named constant) so operators can tune it without code changes. - In the
KeepAliveRestoredcase you don't populatepostMap["data"]as you do forKeepAliveTimeout; if webhooks expect a consistent structure, consider including relevant metadata (e.g.,LastSuccessor a timestamp) or explicitly documenting the difference. - Within the
KeepAliveTimeouthandler you callloggerWrapper.GetLoggermultiple times with the sameinstanceID; consider storing the logger in a local variable to avoid repeated lookups and keep the logging code a bit cleaner.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `KeepAliveTimeout` logic hardcodes the restart threshold at exactly 3 timeouts; consider making this threshold configurable (or at least a named constant) so operators can tune it without code changes.
- In the `KeepAliveRestored` case you don't populate `postMap["data"]` as you do for `KeepAliveTimeout`; if webhooks expect a consistent structure, consider including relevant metadata (e.g., `LastSuccess` or a timestamp) or explicitly documenting the difference.
- Within the `KeepAliveTimeout` handler you call `loggerWrapper.GetLogger` multiple times with the same `instanceID`; consider storing the logger in a local variable to avoid repeated lookups and keep the logging code a bit cleaner.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This was referenced Aug 4, 2026
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.
With
EnableAutoReconnectdisabled (whatsmeow.go:464), a TCP connection that dies silently (no FIN/RST from the server — NAT timeout, network flap) never emitsevents.Disconnected: keepalive pings just keep timing out and the instance stays flagged as connected while being unable to send or receive. Only a manual restart fixes it. whatsmeow's own docs onKeepAliveTimeoutsuggest exactly this: "Clients may use this event to decide to force a disconnect+reconnect faster."This handles
KeepAliveTimeoutandKeepAliveRestored:CONNECTIONwebhook subscription so operators can observe socket health;ReconnectClientpath theDisconnectedhandler already uses (exact-match on the count so an ongoing restart isn't duplicated).We run multiple production instances behind this and it eliminated our "zombie instance" incidents (instance reported connected, no traffic flowing, external watchdog required).
Retargeted to
develop(replaces #126), as requested by @iagocotta in #128 (comment).Note that
mainanddevelophave no common ancestor — the GitHub API refuses to compare them (No common ancestor between main and develop) — so the base branch of the original PR could not simply be edited. This branch was created fromdevelopand the commit cherry-picked onto it.Verified on this branch:
go build ./...andgo vet ./...both pass.Summary by Sourcery
Handle keepalive timeout and restoration events to recover zombie connections and expose socket health via connection webhooks.
Bug Fixes:
Enhancements: