From 861f56d84985df85f2c01c24b308b5b36e6ad5b7 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 22 May 2026 10:47:49 +0000 Subject: [PATCH 1/4] fix: suppress net.ErrClosed on concurrent Close Close and CloseNow returned a wrapped net.ErrClosed when the internal read loop or write path won the casClosing race before the user-facing call. The defer that suppresses net.ErrClosed was only registered on the CAS winner path. Separate user-facing close tracking from internal close coordination. A userClosed atomic tracks whether the user has called Close/CloseNow before. Combined with isClosed, this gives three distinct behaviors: - First user call, close in progress or peer closed: nil. - Concurrent user calls while close is in progress: nil, returns instantly without blocking. - User call after close has fully completed: net.ErrClosed. --- close.go | 20 ++++++++++---------- conn.go | 7 ++++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/close.go b/close.go index 58a6faf4..0a7e1f24 100644 --- a/close.go +++ b/close.go @@ -118,11 +118,7 @@ func (c *Conn) Close(code StatusCode, reason string) error { func (c *Conn) closeAndWait(ce CloseError) (err error) { defer errd.Wrap(&err, "failed to close WebSocket") - if c.casClosing() { - err = c.waitGoroutines() - if err != nil { - return err - } + if c.userClosed.Swap(true) && c.isClosed() { return net.ErrClosed } defer func() { @@ -131,6 +127,10 @@ func (c *Conn) closeAndWait(ce CloseError) (err error) { } }() + if c.casClosing() { + return c.waitGoroutines() + } + err = c.closeHandshake(ce) err2 := c.close() @@ -151,11 +151,7 @@ func (c *Conn) closeAndWait(ce CloseError) (err error) { func (c *Conn) CloseNow() (err error) { defer errd.Wrap(&err, "failed to immediately close WebSocket") - if c.casClosing() { - err = c.waitGoroutines() - if err != nil { - return err - } + if c.userClosed.Swap(true) && c.isClosed() { return net.ErrClosed } defer func() { @@ -164,6 +160,10 @@ func (c *Conn) CloseNow() (err error) { } }() + if c.casClosing() { + return c.waitGoroutines() + } + err = c.close() err2 := c.waitGoroutines() diff --git a/conn.go b/conn.go index 0569bd2c..9b63cb41 100644 --- a/conn.go +++ b/conn.go @@ -77,9 +77,10 @@ type Conn struct { closeReadCtx context.Context closeReadDone chan struct{} - closing atomic.Bool - closeMu sync.Mutex // Protects following. - closed chan struct{} + userClosed atomic.Bool // Set by Close/CloseNow on first user call. + closing atomic.Bool + closeMu sync.Mutex // Protects following. + closed chan struct{} pingCounter atomic.Int64 activePingsMu sync.Mutex From 9a5805542a1448163b8db67629120614043258a9 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 26 Oct 2026 18:25:44 +0000 Subject: [PATCH 2/4] fix: keep library closes from failing the first Close When the read or write path closed the connection first, for example after the peer's close frame, the user's first Close or CloseNow returned a wrapped net.ErrClosed. The first user call now returns nil in that case, and library code that closes the connection (CloseRead and NetConn reads) uses an internal close so it does not count as that call. --- close.go | 42 ++++++++++++++++++++++++--------- conn.go | 11 +++++---- conn_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ netconn.go | 2 +- read.go | 2 +- 5 files changed, 105 insertions(+), 17 deletions(-) diff --git a/close.go b/close.go index 0a7e1f24..712de568 100644 --- a/close.go +++ b/close.go @@ -107,6 +107,17 @@ func CloseStatus(err error) StatusCode { // Close will unblock all goroutines interacting with the connection once // complete. func (c *Conn) Close(code StatusCode, reason string) error { + firstCall := !c.closeCalled.Swap(true) + err := c.closeWithHandshake(code, reason) + if firstCall && errors.Is(err, net.ErrClosed) { + return nil + } + return err +} + +// closeWithHandshake implements Close for library code, which must not set +// closeCalled. +func (c *Conn) closeWithHandshake(code StatusCode, reason string) error { ce, wireErr := wireCloseError(code, reason) err := c.closeAndWait(ce) if err != nil { @@ -118,7 +129,11 @@ func (c *Conn) Close(code StatusCode, reason string) error { func (c *Conn) closeAndWait(ce CloseError) (err error) { defer errd.Wrap(&err, "failed to close WebSocket") - if c.userClosed.Swap(true) && c.isClosed() { + if c.casClosing() { + err = c.waitGoroutines() + if err != nil { + return err + } return net.ErrClosed } defer func() { @@ -127,10 +142,6 @@ func (c *Conn) closeAndWait(ce CloseError) (err error) { } }() - if c.casClosing() { - return c.waitGoroutines() - } - err = c.closeHandshake(ce) err2 := c.close() @@ -148,10 +159,23 @@ func (c *Conn) closeAndWait(ce CloseError) (err error) { // CloseNow closes the WebSocket connection without attempting a close handshake. // Use when you do not want the overhead of the close handshake. -func (c *Conn) CloseNow() (err error) { +func (c *Conn) CloseNow() error { + firstCall := !c.closeCalled.Swap(true) + err := c.closeWithoutHandshake() + if firstCall && errors.Is(err, net.ErrClosed) { + return nil + } + return err +} + +func (c *Conn) closeWithoutHandshake() (err error) { defer errd.Wrap(&err, "failed to immediately close WebSocket") - if c.userClosed.Swap(true) && c.isClosed() { + if c.casClosing() { + err = c.waitGoroutines() + if err != nil { + return err + } return net.ErrClosed } defer func() { @@ -160,10 +184,6 @@ func (c *Conn) CloseNow() (err error) { } }() - if c.casClosing() { - return c.waitGoroutines() - } - err = c.close() err2 := c.waitGoroutines() diff --git a/conn.go b/conn.go index 9b63cb41..b79ccce2 100644 --- a/conn.go +++ b/conn.go @@ -77,10 +77,13 @@ type Conn struct { closeReadCtx context.Context closeReadDone chan struct{} - userClosed atomic.Bool // Set by Close/CloseNow on first user call. - closing atomic.Bool - closeMu sync.Mutex // Protects following. - closed chan struct{} + // closeCalled is set by the first Close or CloseNow call. Library code + // closes the connection without setting it, so the user's first Close + // does not report net.ErrClosed when the library closed first. + closeCalled atomic.Bool + closing atomic.Bool + closeMu sync.Mutex // Protects following. + closed chan struct{} pingCounter atomic.Int64 activePingsMu sync.Mutex diff --git a/conn_test.go b/conn_test.go index c03c02b3..2b364f02 100644 --- a/conn_test.go +++ b/conn_test.go @@ -495,6 +495,71 @@ func TestConn(t *testing.T) { assert.ErrorIs(t, websocket.ErrClosed, err2) }) + t.Run("firstCloseAfterLibraryClose", func(t *testing.T) { + t.Parallel() + + closers := []struct { + name string + close func(*websocket.Conn) error + }{ + {"Close", func(c *websocket.Conn) error { return c.Close(websocket.StatusNormalClosure, "") }}, + {"CloseNow", (*websocket.Conn).CloseNow}, + } + for _, closer := range closers { + t.Run(closer.name, func(t *testing.T) { + t.Parallel() + + t.Run("peerClose", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + readErr := xsync.Go(func() error { + _, _, err := c1.Read(tt.ctx) + return err + }) + assert.Success(t, c2.Close(websocket.StatusNormalClosure, "")) + assert.Success(t, assertCloseStatus(websocket.StatusNormalClosure, <-readErr)) + + assert.Success(t, closer.close(c1)) + }) + + t.Run("closeReadDataMessage", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + closeReadCtx := c1.CloseRead(tt.ctx) + readErr := xsync.Go(func() error { + _, _, err := c2.Read(tt.ctx) + return err + }) + assert.Success(t, c2.Write(tt.ctx, websocket.MessageText, []byte("x"))) + assert.Success(t, assertCloseStatus(websocket.StatusPolicyViolation, <-readErr)) + <-closeReadCtx.Done() + + assert.Success(t, closer.close(c1)) + }) + + t.Run("netConnUnexpectedType", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + nc := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary) + readErr := xsync.Go(func() error { + _, _, err := c2.Read(tt.ctx) + return err + }) + // net.Pipe writes block until nc.Read reads them. + writeErr := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageText, []byte("x")) + }) + _, err := nc.Read(make([]byte, 1)) + assert.Contains(t, err, "unexpected frame type") + assert.Success(t, <-writeErr) + assert.Success(t, assertCloseStatus(websocket.StatusUnsupportedData, <-readErr)) + + assert.Success(t, closer.close(c1)) + }) + }) + } + }) + t.Run("MidReadClose", func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) diff --git a/netconn.go b/netconn.go index a854faa4..207393b6 100644 --- a/netconn.go +++ b/netconn.go @@ -174,7 +174,7 @@ func (nc *netConn) read(p []byte) (int, error) { } if typ != nc.msgType { err := fmt.Errorf("unexpected frame type read (expected %v): %v", nc.msgType, typ) - nc.c.Close(StatusUnsupportedData, err.Error()) + nc.c.closeWithHandshake(StatusUnsupportedData, err.Error()) return 0, err } nc.reader = r diff --git a/read.go b/read.go index 5ef42734..502eb9b2 100644 --- a/read.go +++ b/read.go @@ -79,7 +79,7 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { defer c.close() _, _, err := c.Reader(ctx) if err == nil { - c.Close(StatusPolicyViolation, "unexpected data message") + c.closeWithHandshake(StatusPolicyViolation, "unexpected data message") } }() return ctx From aca462558843b1cf36a4621c6454908919f5c7e9 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 26 Oct 2026 18:25:51 +0000 Subject: [PATCH 3/4] fix: keep browser closes from failing the first Close on Wasm On Wasm, the user's first Close returned a wrapped net.ErrClosed after the browser or library code had already closed the connection, the same failure as the native fix. Close now applies the same first-call rule, and the read limit, read timeout, CloseRead and finalizer paths use an internal close. TestWasm gains a server mode that closes first. --- conn_test.go | 15 +++++++++++++++ ws_js.go | 25 ++++++++++++++++++++----- ws_js_test.go | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/conn_test.go b/conn_test.go index 2b364f02..99a849bc 100644 --- a/conn_test.go +++ b/conn_test.go @@ -608,6 +608,21 @@ func TestWasm(t *testing.T) { } s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Sec-WebSocket-Protocol") == "close" { + // Closes first so the client's Close runs after the browser + // closed the connection. + c, err := websocket.Accept(w, r, &websocket.AcceptOptions{ + Subprotocols: []string{"close"}, + InsecureSkipVerify: true, + }) + if err == nil { + err = c.Close(websocket.StatusNormalClosure, "") + } + if err != nil { + t.Error(err) + } + return + } err := echoServer(w, r, &websocket.AcceptOptions{ Subprotocols: []string{"echo"}, InsecureSkipVerify: true, diff --git a/ws_js.go b/ws_js.go index 95f13e1a..603864ca 100644 --- a/ws_js.go +++ b/ws_js.go @@ -56,6 +56,10 @@ type Conn struct { closeErrOnce sync.Once closeErr error closeWasClean bool + // closeCalled is set by the first Close or CloseNow call. Library code + // and the browser close the connection without setting it, so the user's + // first Close does not report net.ErrClosed after either closed it. + closeCalled atomic.Bool releaseOnClose func() releaseOnError func() @@ -125,7 +129,7 @@ func (c *Conn) init() { } func (c *Conn) closeWithInternal() { - c.Close(StatusInternalError, "something went wrong") + c.closeWithHandshake(StatusInternalError, "something went wrong") } // Read attempts to read a message from the connection. @@ -145,7 +149,7 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) { readLimit := c.msgReadLimit.Load() if readLimit >= 0 && int64(len(p)) > readLimit { reason := fmt.Errorf("read limited at %d bytes", c.msgReadLimit.Load()) - c.Close(StatusMessageTooBig, reason.Error()) + c.closeWithHandshake(StatusMessageTooBig, reason.Error()) return 0, nil, fmt.Errorf("%w: %v", ErrMessageTooBig, reason) } return typ, p, nil @@ -154,7 +158,7 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) { func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) { select { case <-ctx.Done(): - c.Close(StatusPolicyViolation, "read timed out") + c.closeWithHandshake(StatusPolicyViolation, "read timed out") return 0, nil, ctx.Err() case <-c.readSignal: case <-c.closed: @@ -233,6 +237,17 @@ func (c *Conn) write(typ MessageType, p []byte) error { // 123 bytes without splitting a rune. If it truncated the reason and the close // had no other error, Close returns an error wrapping ErrCloseReasonTruncated. func (c *Conn) Close(code StatusCode, reason string) error { + firstCall := !c.closeCalled.Swap(true) + err := c.closeWithHandshake(code, reason) + if firstCall && errors.Is(err, net.ErrClosed) { + return nil + } + return err +} + +// closeWithHandshake implements Close for library code, which must not set +// closeCalled. +func (c *Conn) closeWithHandshake(code StatusCode, reason string) error { // Browsers throw on a reason over 123 bytes and leave the connection open. reason, truncErr := wireCloseReason(reason) err := c.closeAndWait(code, reason) @@ -410,10 +425,10 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { go func() { defer cancel() - defer c.CloseNow() + defer c.closeWithHandshake(StatusGoingAway, "") _, _, err := c.read(ctx) if err != nil { - c.Close(StatusPolicyViolation, "unexpected data message") + c.closeWithHandshake(StatusPolicyViolation, "unexpected data message") } }() return ctx diff --git a/ws_js_test.go b/ws_js_test.go index a1d846fa..4d11a62d 100644 --- a/ws_js_test.go +++ b/ws_js_test.go @@ -2,6 +2,7 @@ package websocket_test import ( "context" + "net" "net/http" "os" "strings" @@ -72,6 +73,25 @@ func TestWasmCloseReasonInvalidUTF8(t *testing.T) { assert.Success(t, err) } +func TestWasmFirstCloseAfterPeerClose(t *testing.T) { + t.Parallel() + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + c, _, err := websocket.Dial(ctx, os.Getenv("WS_ECHO_SERVER_URL"), &websocket.DialOptions{ + Subprotocols: []string{"close"}, + }) + assert.Success(t, err) + + _, _, err = c.Read(ctx) + // Read returns once the browser has closed the connection. + assert.ErrorIs(t, net.ErrClosed, err) + + err = c.Close(websocket.StatusNormalClosure, "") + assert.Success(t, err) +} + func TestWasmDialTimeout(t *testing.T) { t.Parallel() From 538ba5c200f13fac01efd9dee88880c3f04ba1a8 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 20 Nov 2026 15:19:00 +0000 Subject: [PATCH 4/4] fix: keep wsjson.Read's close from failing the first Close wsjson.Read closed the connection with the public Close when the message was not valid JSON, so the application's first Close or CloseNow returned net.ErrClosed. wsjson cannot call the unexported closeWithHandshake, so package websocket stores it in internal/libclose during initialization and wsjson.Read calls it from there. --- close_shared.go | 6 ++++++ conn_test.go | 20 ++++++++++++++++++++ internal/libclose/libclose.go | 10 ++++++++++ wsjson/wsjson.go | 7 ++++++- 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 internal/libclose/libclose.go diff --git a/close_shared.go b/close_shared.go index ec2e94eb..02d87a5f 100644 --- a/close_shared.go +++ b/close_shared.go @@ -4,8 +4,14 @@ import ( "fmt" "strings" "unicode/utf8" + + "github.com/coder/websocket/internal/libclose" ) +func init() { + libclose.Close = (*Conn).closeWithHandshake +} + // maxControlPayload is the maximum length of a control frame payload. // See https://tools.ietf.org/html/rfc6455#section-5.5. const maxControlPayload = 125 diff --git a/conn_test.go b/conn_test.go index 99a849bc..afd00506 100644 --- a/conn_test.go +++ b/conn_test.go @@ -556,6 +556,26 @@ func TestConn(t *testing.T) { assert.Success(t, closer.close(c1)) }) + + t.Run("wsjsonInvalidJSON", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + readErr := xsync.Go(func() error { + _, _, err := c2.Read(tt.ctx) + return err + }) + // net.Pipe writes block until wsjson.Read reads them. + writeErr := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageText, []byte("{")) + }) + var v any + err := wsjson.Read(tt.ctx, c1, &v) + assert.Contains(t, err, "failed to unmarshal JSON") + assert.Success(t, <-writeErr) + assert.Success(t, assertCloseStatus(websocket.StatusInvalidFramePayloadData, <-readErr)) + + assert.Success(t, closer.close(c1)) + }) }) } }) diff --git a/internal/libclose/libclose.go b/internal/libclose/libclose.go new file mode 100644 index 00000000..85b7d182 --- /dev/null +++ b/internal/libclose/libclose.go @@ -0,0 +1,10 @@ +// Package libclose lets other packages in this module close a websocket.Conn +// the way package websocket closes it itself, so the close does not count as +// the application's first Close call. +package libclose + +// Close holds (*websocket.Conn).closeWithHandshake and has the type +// func(*websocket.Conn, websocket.StatusCode, string) error. Package websocket +// sets it during initialization. It has type any because package websocket +// imports this package, so this package cannot name those types. +var Close any diff --git a/wsjson/wsjson.go b/wsjson/wsjson.go index ffa068bf..46a6de48 100644 --- a/wsjson/wsjson.go +++ b/wsjson/wsjson.go @@ -9,9 +9,14 @@ import ( "github.com/coder/websocket" "github.com/coder/websocket/internal/bpool" "github.com/coder/websocket/internal/errd" + "github.com/coder/websocket/internal/libclose" "github.com/coder/websocket/internal/util" ) +// closeWithHandshake closes c without counting as the application's first +// Close call. +var closeWithHandshake = libclose.Close.(func(*websocket.Conn, websocket.StatusCode, string) error) + // Read reads a JSON message from c into v. // It will reuse buffers in between calls to avoid allocations. func Read(ctx context.Context, c *websocket.Conn, v any) error { @@ -36,7 +41,7 @@ func read(ctx context.Context, c *websocket.Conn, v any) (err error) { err = json.Unmarshal(b.Bytes(), v) if err != nil { - c.Close(websocket.StatusInvalidFramePayloadData, "failed to unmarshal JSON") + closeWithHandshake(c, websocket.StatusInvalidFramePayloadData, "failed to unmarshal JSON") return fmt.Errorf("failed to unmarshal JSON: %w", err) }