Repository navigation
Detect dead/half-open control websocket (read deadline + protocol ping) - #387
Merged
oschwartz10612 merged 1 commit intoJun 23, 2026
Merged
Conversation
DanivosYoun
requested review from
miloschwartz and
oschwartz10612
as code owners
June 22, 2026 02:41
…ne + protocol ping) The control websocket had no read deadline and only sent application-level "newt/ping" JSON (never protocol PING frames), so a half-open connection — for example a cloud load balancer keeping the socket open after the backend API server restarted — was never detected. ReadMessage blocked forever while the buffered ping writes kept succeeding, so newt stayed "connected" to a dead peer and only recovered after a manual restart. - Arm a read deadline (pongWait = 2x pingInterval, min 20s) and refresh it on every pong and every inbound message. - Send a protocol-level PING alongside the existing app ping, so a compliant server replies with a PONG that refreshes the read deadline. - On ping-write failure, close the connection and let the read pump perform a single reconnect (previously sendPing and the read-pump defer could both trigger a reconnect, racing two connections). - Give each connection a lifecycle channel so its ping monitor stops when the connection ends (previously one ping-monitor goroutine leaked per reconnect).
DanivosYoun
force-pushed
the
fix/ws-half-open-detection
branch
from
June 22, 2026 02:51
021115f to
855b2b2
Compare
oschwartz10612
approved these changes
Jun 23, 2026
Member
|
Thanks so much! |
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.
Problem
When the backend API server has an outage and then recovers, newt does not reconnect on its own — it stays stuck and has to be restarted by hand before it works again. This reproduces reliably when newt connects through a cloud load balancer / reverse proxy in front of the API server.
Root cause
The control websocket (
websocket/client.go) has no read deadline, and the keepalive is an application-levelnewt/pingJSON message, not a protocol PING frame:readPumpWithDisconnectDetectioncallsconn.ReadMessage()with no deadline, so on a half-open connection it blocks forever.sendPingdoesWriteJSON({type:"newt/ping"}). On a half-open socket (e.g. an LB holding the front-end TCP open after the backend died/restarted) these writes are buffered and keep succeeding, so no write error is ever raised.SetPongHandleris effectively dead code, because newt never sends a protocol PING, so no PONG is ever received.Net effect: newt believes it is still connected to a peer that is gone, never tears the connection down, and never reconnects — until the process is restarted.
Fix
Standard gorilla websocket keepalive, plus two related cleanups:
SetReadDeadline(pongWait)on connect, wherepongWait = 2 × pingInterval(min 20s), and refresh it on every pong and on every inbound message (covers servers that answernewt/pingwith a message rather than a protocol pong).websocket.PingMessagealongside the existing app ping; a compliant server auto-replies with a PONG that refreshes the deadline. If the peer is gone, no pong arrives, the read deadline expires,ReadMessageerrors, and the read pump reconnects.sendPingand the read-pump defer both callingreconnect()(which could race two connections).No server-side changes required — protocol PING/PONG is handled automatically by any standards-compliant websocket server.
Testing
go build ./...(all non-example packages),go vet ./websocket/, andgo test ./websocket/pass.pongWaitand reconnect automatically — no manual restart.