mirror of
https://github.com/fosrl/newt.git
synced 2026-03-08 23:03:03 -05:00
Panic in WebSocket client: nil *websocket.Conn due to setting c.conn to nil #70
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @LaurenceJJones on GitHub (Nov 13, 2025).
Originally assigned to: @oschwartz10612 on GitHub.
Describe the Bug
We’re seeing a runtime panic coming from the WebSocket client when the connection is being reused / reconnected:
Note the receiver for
(*websocket.Conn).NextReaderis0x0, so the panic occurs because we’re callingReadJSONon anil*websocket.Conn.Relevant code paths
readPumpWithDisconnectDetectioncalls:pingMonitorcalls:reconnect(and related shutdown logic) closes the current connection and setsc.conn = nilbefore/while these goroutines are still running.This means there’s a race where
reconnectsetsc.conntonilbetween loop iterations inreadPumpWithDisconnectDetectionorpingMonitor. On the next iteration those goroutines call methods onc.connand we end up in(*Conn).NextReader(0x0)→ nil pointer dereference.Proposed direction
For now, the minimal fix is simply not to assign
c.conn = nilwhile reader/writer goroutines are still active. Closing the underlying*websocket.Connis enough to causeReadJSON/WriteControlto return an error, which we already handle with reconnect logic. The extranilassignment is what turns a clean error into a panic.or
If you wish to keep the current setting to
nilthen before callingREADJSONwe must check if the connection isniland return but this may cause duplicate re connection attempts.Environment
To Reproduce
Simply a race condition and hard to replicate
Expected Behavior
Panic should never occur during runtime