A Go WebSocket read fails with close 1006 (abnormal closure): unexpected EOF
Fix it fast
Most common cause, 1 of 4The peer went away without a closing handshake
Treat 1001 (going away) and 1006 as expected endings: clean up the client and log only the other codes, the way the gorilla chat example does.
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
return // unregister the client and close the connection
}Short answer
"websocket: close 1006 (abnormal closure): unexpected EOF" from gorilla/websocket v1.5.3 means the TCP stream ended with no close frame; 1006 is filled in locally, never sent. The peer vanished, a proxy cut an idle socket, a remote server dropped a client that never read its pings, or your side called Close() without a close message.
Which cause do you have?
Log the close code together with how long the connection lived and how long it had been quiet. A drop at random moments is a peer leaving; a drop a fixed time after the last message is a proxy; a drop a fixed time after connecting, whatever the traffic, is a remote server timing out its pings.
start := time.Now()
for {
if _, _, err := conn.ReadMessage(); err != nil {
var ce *websocket.CloseError
if errors.As(err, &ce) {
log.Printf("close code=%d text=%q alive=%s", ce.Code, ce.Text, time.Since(start))
}
return
}
}Then find the line you see in this list and open its fix:
- 1The peer went away without a closing handshake
The error shows up on the server whenever a user closes a tab, a phone loses signal or a client process is killed - 2A proxy or load balancer closes the connection after a quiet period
The drop happens a fixed time after the last message, often 60 seconds behind nginx, and never while data is flowing - 3Your client never reads, so the remote server's pings go unanswered
A Go client that only writes, or reads in bursts, is dropped by a third-party feed a fixed time after connecting - 4Your own side closes the network connection without a close message
The other end reports 1006 every time you shut down, redeploy or kick a client
What does the error look like?
websocket: close 1006 (abnormal closure): unexpected EOFread: websocket: close 1006 (abnormal closure): unexpected EOF
Cause 1 of 4
The peer went away without a closing handshake
You seeThe error shows up on the server whenever a user closes a tab, a phone loses signal or a client process is killed
Check
A close 1006 unexpected EOF on the server, at random moments, usually means a client left without a closing handshake: RFC 6455 reserves 1006 for a connection that ended without a Close control frame being sent or received. A browser tab that is killed, a laptop that sleeps or a dropped mobile network all end the TCP stream this way. On the server this is normal traffic, not a bug.
grep -n "IsUnexpectedCloseError\|IsCloseError" *.goFix
Treat 1001 (going away) and 1006 as expected endings: clean up the client and log only the other codes, the way the gorilla chat example does.
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
return // unregister the client and close the connection
}Verify
Open the page, close the tab, and watch the server log: the client is removed and no error line is printed.
go run .Confirmed by github.com
Cause 2 of 4
A proxy or load balancer closes the connection after a quiet period
You seeThe drop happens a fixed time after the last message, often 60 seconds behind nginx, and never while data is flowing
Check
A close 1006 unexpected EOF that lands a fixed time after the last message points to an idle timeout in a proxy: nginx closes a proxied WebSocket when the backend sends nothing for 60 seconds by default, and Cloudflare likewise closes a WebSocket that carries no data for a while. nginx documents two remedies: a longer proxy_read_timeout, or ping frames from the server to reset the timer.
grep -rn "proxy_read_timeout" /etc/nginx/Fix
Ping from the Go side more often than the shortest idle limit in the path, and extend the read deadline each time a pong comes back. This is the pattern from the gorilla chat example: one goroutine owns all writes and the pings, another owns all reads.
const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
)
// reader goroutine
c.conn.SetReadDeadline(time.Now().Add(pongWait))
c.conn.SetPongHandler(func(string) error {
return c.conn.SetReadDeadline(time.Now().Add(pongWait))
})
// writer goroutine
ticker := time.NewTicker(pingPeriod)
defer ticker.Stop()
for range ticker.C {
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}Verify
Log each pong for a while, then leave a connection idle through the proxy for several minutes. Pongs keep arriving and the read never fails.
c.conn.SetPongHandler(func(string) error {
log.Println("pong")
return c.conn.SetReadDeadline(time.Now().Add(pongWait))
})Confirmed by nginx.org
Cause 3 of 4
Your client never reads, so the remote server's pings go unanswered
You seeA Go client that only writes, or reads in bursts, is dropped by a third-party feed a fixed time after connecting
Check
A close 1006 unexpected EOF on a client that never reads means the remote server gave up waiting for pongs, because gorilla/websocket answers pings and close frames only from inside the read methods: NextReader, ReadMessage and the message reader. A program that never calls them never sends a pong, so a server that requires pongs closes the TCP connection.
grep -n "ReadMessage\|NextReader" *.goFix
Run a read loop for the whole life of the connection, even when the messages themselves are of no interest. The package documentation shows a loop that reads and discards.
go func() {
for {
if _, _, err := conn.NextReader(); err != nil {
conn.Close()
return
}
}
}()Verify
Keep the client connected past the point where it used to drop; the feed keeps sending and no 1006 is logged.
go run ./cmd/clientConfirmed by pkg.go.dev
Cause 4 of 4
Your own side closes the network connection without a close message
You seeThe other end reports 1006 every time you shut down, redeploy or kick a client
Check
A close 1006 unexpected EOF reported by the other end after your own planned shutdown comes from Conn.Close(), which closes the socket immediately and neither sends nor waits for a close frame.
grep -n "\.Close()" *.goFix
Send a close frame first, give the peer a moment to answer, and only then close the socket, as the gorilla echo client does.
err := conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
}
select {
case <-done: // the read loop saw the peer's close
case <-time.After(time.Second):
}
conn.Close()Verify
Stop the program with Ctrl+C. The peer now logs close code 1000 instead of 1006.
go run .Confirmed by github.com
How do I stop it coming back?
- Give every connection one reading goroutine and one writing goroutine from the start: a gorilla connection supports one concurrent reader and one concurrent writer.
- Keep the ping period below the shortest idle timeout of any proxy, balancer or CDN in the path.
- Close with a close frame on planned shutdowns, so real failures stand out in the logs.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, stegnor.com.