fix: recover zombie connections on KeepAliveTimeout - #126
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 KeepAlive timeout/restoration events to surface socket health via CONNECTION webhooks and automatically restart zombie instances after repeated keepalive failures. Sequence diagram for KeepAliveTimeout handling and automatic reconnectsequenceDiagram
participant WhatsAppServer
participant MyClient
participant WhatsmeowService
participant WebhookSubscriber
WhatsAppServer->>MyClient: events.KeepAliveTimeout
MyClient->>MyClient: myEventHandler(KeepAliveTimeout)
MyClient->>WhatsmeowService: CallWebhook(eventType=KeepAliveTimeout)
WhatsmeowService->>WebhookSubscriber: sendToQueueOrWebhook(globalEventType=CONNECTION)
alt [ErrorCount == 3]
MyClient->>MyClient: LogWarn("3 consecutive keepalive timeouts")
MyClient->>WhatsmeowService: ReconnectClient(instanceID)
end
WhatsAppServer->>MyClient: events.KeepAliveRestored
MyClient->>MyClient: myEventHandler(KeepAliveRestored)
MyClient->>WhatsmeowService: CallWebhook(eventType=KeepAliveRestored)
WhatsmeowService->>WebhookSubscriber: sendToQueueOrWebhook(globalEventType=CONNECTION)
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 restart-on-timeout logic currently triggers purely on ErrorCount == 3; if this is intended only when EnableAutoReconnect is disabled, consider explicitly checking that flag to avoid overlapping with the built-in reconnect behavior.
- The threshold of 3 consecutive keepalive timeouts is hard-coded; consider making this configurable so operators can tune sensitivity based on their deployment characteristics.
- For KeepAliveTimeout/KeepAliveRestored, you might want to include consistent, richer data in the webhook payload (e.g., last success timestamp and error count for restored events as well) to make monitoring and alerting easier.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The restart-on-timeout logic currently triggers purely on ErrorCount == 3; if this is intended only when EnableAutoReconnect is disabled, consider explicitly checking that flag to avoid overlapping with the built-in reconnect behavior.
- The threshold of 3 consecutive keepalive timeouts is hard-coded; consider making this configurable so operators can tune sensitivity based on their deployment characteristics.
- For KeepAliveTimeout/KeepAliveRestored, you might want to include consistent, richer data in the webhook payload (e.g., last success timestamp and error count for restored events as well) to make monitoring and alerting easier.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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).
Summary by Sourcery
Handle keepalive timeout and restoration events by exposing them as CONNECTION events and triggering controlled instance restarts after repeated timeouts to avoid zombie connections when auto-reconnect is disabled.
New Features:
Bug Fixes:
Enhancements: