Skip to content

Commit d7b2a63

Browse files
yperbasisclaudeinfo@weblogix.biz
authored
rpc: use library Close helper for websocket disconnect (#20913)
## Summary Follow-up to #20788. Address review feedback from @anacrolix: > I thought there was a helper in the websocket library for this so you don't need to spin up a bunch of timers and goroutines? The `coder/websocket` `Conn.Close(code, reason)` method already performs the close handshake with bounded internal timeouts (5s write, 5s read), so the manual goroutine + timer plumbing in `wsConnAdapter.Close` is redundant. Replace it with a direct call. The existing `TestWebsocketServerGracefulClose` test still passes — clients see a clean `StatusNormalClosure` (1000) instead of `StatusAbnormalClosure` (1006). ## Test plan - [x] `go test ./rpc/ -run Websocket` — all pass - [x] `make lint` — clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: info@weblogix.biz <admin@10gbps.weblogix.it>
1 parent cc38945 commit d7b2a63

1 file changed

Lines changed: 1 addition & 22 deletions

File tree

rpc/websocket.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -257,28 +257,7 @@ type wsConnAdapter struct {
257257
}
258258

259259
func (a *wsConnAdapter) Close() error {
260-
// Attempt a graceful WebSocket close handshake first, so the peer sees a clean
261-
// 1000 (normal closure) instead of 1006 (abnormal). Keep the handshake fully async
262-
// so server shutdown doesn't block per connection, and force-close after a bounded
263-
// grace period if the peer doesn't complete the close promptly.
264-
closeDone := make(chan struct{})
265-
go func() {
266-
defer close(closeDone)
267-
_ = a.conn.Close(websocket.StatusNormalClosure, "")
268-
}()
269-
270-
go func() {
271-
timer := time.NewTimer(time.Second)
272-
defer timer.Stop()
273-
274-
select {
275-
case <-closeDone:
276-
case <-timer.C:
277-
_ = a.conn.CloseNow()
278-
}
279-
}()
280-
281-
return nil
260+
return a.conn.Close(websocket.StatusNormalClosure, "")
282261
}
283262

284263
func (a *wsConnAdapter) SetWriteDeadline(t time.Time) error {

0 commit comments

Comments
 (0)