Skip to content

Detect dead/half-open control websocket (read deadline + protocol ping) - #387

Merged
oschwartz10612 merged 1 commit into
fosrl:mainfrom
DanivosYoun:fix/ws-half-open-detection
Jun 23, 2026
Merged

oschwartz10612 merged 1 commit into
fosrl:mainfrom
DanivosYoun:fix/ws-half-open-detection

Conversation

@DanivosYoun

@DanivosYoun DanivosYoun commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

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-level newt/ping JSON message, not a protocol PING frame:

  • readPumpWithDisconnectDetection calls conn.ReadMessage() with no deadline, so on a half-open connection it blocks forever.
  • sendPing does WriteJSON({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.
  • The existing SetPongHandler is 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:

  • Read deadline: arm SetReadDeadline(pongWait) on connect, where pongWait = 2 × pingInterval (min 20s), and refresh it on every pong and on every inbound message (covers servers that answer newt/ping with a message rather than a protocol pong).
  • Protocol PING: send a websocket.PingMessage alongside 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, ReadMessage errors, and the read pump reconnects.
  • Single reconnect path: on ping-write failure, close the connection and let the read pump perform the one reconnect, instead of sendPing and the read-pump defer both calling reconnect() (which could race two connections).
  • No ping-monitor leak: each connection gets a lifecycle channel so its ping monitor stops when the connection ends (previously one ping-monitor goroutine leaked per reconnect).

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/, and go test ./websocket/ pass.
  • Behaviour: with the API server behind a proxy, dropping/restarting the backend now causes newt to detect the dead connection within ~pongWait and reconnect automatically — no manual restart.

…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
DanivosYoun force-pushed the fix/ws-half-open-detection branch from 021115f to 855b2b2 Compare June 22, 2026 02:51
@oschwartz10612
oschwartz10612 merged commit 5a5897e into fosrl:main Jun 23, 2026
13 checks passed
@oschwartz10612

Copy link
Copy Markdown
Member

Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants