From 33c1bd59ff23a7b22f8804a6b83833015edfc159 Mon Sep 17 00:00:00 2001 From: YQ Date: Fri, 24 Apr 2026 17:27:39 +0800 Subject: [PATCH] rpc: send WebSocket close frame on client disconnect (#33909) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `rpc.Client.Close()` is called, the TCP connection is torn down without sending a WebSocket Close frame. The server sees `websocket: close 1006 (abnormal closure): unexpected EOF` instead of a clean 1000 (normal closure). ### Root cause `websocketCodec.close()` delegates to `jsonCodec.close()` which calls `c.conn.Close()` — gorilla/websocket's `Conn.Close` explicitly "[closes the underlying network connection without sending or waiting for a close message](https://pkg.go.dev/github.com/gorilla/websocket#Conn.Close)" (per RFC 6455). ### Fix Send a WebSocket Close control frame (opcode 0x8, status 1000) before closing the underlying connection. Uses `WriteControl` with the same `encMu` mutex pattern already used by `pingLoop` for write serialization, and reuses the existing `wsPingWriteTimeout` (5s) constant. `WriteControl` errors are safe to ignore — the connection may already be broken by the time we attempt the close frame. Fixes #30482 --- rpc/websocket.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rpc/websocket.go b/rpc/websocket.go index 543ff617ba..ec676b9caf 100644 --- a/rpc/websocket.go +++ b/rpc/websocket.go @@ -324,6 +324,16 @@ func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header, readL } func (wc *websocketCodec) close() { + // Send a WebSocket Close frame before closing the underlying connection, + // so the server sees a clean 1000 (normal closure) instead of 1006 (abnormal). + wc.jsonCodec.encMu.Lock() + wc.conn.WriteControl( + websocket.CloseMessage, + websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), + time.Now().Add(wsPingWriteTimeout), + ) + wc.jsonCodec.encMu.Unlock() + wc.jsonCodec.close() wc.wg.Wait() }