From 7a283b5204a4faaf1948f1c48dba547f4c464a7f Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 27 Oct 2026 17:58:31 +0000 Subject: [PATCH 01/19] fix: keep a read limit set before NetConn NetConn always called SetReadLimit(-1), so a limit set before NetConn was silently dropped while one set after it applied. NetConn now disables the limit only if SetReadLimit was never called on the Conn. --- conn.go | 1 + conn_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ netconn.go | 7 +++++-- read.go | 5 +++++ ws_js.go | 6 ++++++ 5 files changed, 71 insertions(+), 2 deletions(-) diff --git a/conn.go b/conn.go index 0569bd2c..1df92623 100644 --- a/conn.go +++ b/conn.go @@ -59,6 +59,7 @@ type Conn struct { readHeaderBuf [8]byte readControlBuf [maxControlPayload]byte msgReader *msgReader + readLimitSet atomic.Bool // Write state. msgWriter *msgWriter diff --git a/conn_test.go b/conn_test.go index c03c02b3..ebf06468 100644 --- a/conn_test.go +++ b/conn_test.go @@ -410,6 +410,60 @@ func TestConn(t *testing.T) { assert.Equal(t, "read msg", s, string(b)) }) + for _, tc := range []struct { + name string + setLimit func(ctx context.Context, c *websocket.Conn) net.Conn + }{ + {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(1024) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }}, + {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + nc := websocket.NetConn(ctx, c, websocket.MessageBinary) + c.SetReadLimit(1024) + return nc + }}, + } { + t.Run("netConn/readLimitKept/"+tc.name, func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + n1 := tc.setLimit(tt.ctx, c1) + _ = c2.CloseRead(tt.ctx) + + writeDone := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageBinary, make([]byte, 4096)) + }) + + _, err := io.ReadFull(n1, make([]byte, 4096)) + assert.ErrorIs(t, websocket.ErrMessageTooBig, err) + + _ = c2.CloseNow() + <-writeDone + }) + } + + t.Run("netConn/readLimitDisabledBeforeNetConn", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + c1.SetReadLimit(-1) + n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary) + n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageBinary) + + s := strings.Repeat("papa", 1<<20) + errs := xsync.Go(func() error { + _, err := n2.Write([]byte(s)) + if err != nil { + return err + } + return n2.Close() + }) + + b, err := io.ReadAll(n1) + assert.Success(t, err) + assert.Success(t, <-errs) + assert.Equal(t, "read msg", s, string(b)) + }) + t.Run("netConn/pastDeadline", func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) diff --git a/netconn.go b/netconn.go index a854faa4..1dc1e79f 100644 --- a/netconn.go +++ b/netconn.go @@ -44,9 +44,12 @@ import ( // A received StatusNormalClosure or StatusGoingAway close frame will be translated to // io.EOF when reading. // -// Furthermore, the ReadLimit is set to -1 to disable it. +// If SetReadLimit has not been called on c, NetConn disables the read limit. +// A limit set with SetReadLimit before or after NetConn is kept. func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { - c.SetReadLimit(-1) + if !c.readLimitSet.Load() { + c.setReadLimit(-1) + } nc := &netConn{ c: c, diff --git a/read.go b/read.go index 5ef42734..15663029 100644 --- a/read.go +++ b/read.go @@ -95,6 +95,11 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { // // Set to -1 to disable. func (c *Conn) SetReadLimit(n int64) { + c.readLimitSet.Store(true) + c.setReadLimit(n) +} + +func (c *Conn) setReadLimit(n int64) { if n >= 0 { // We read one more byte than the limit in case // there is a fin frame that needs to be read. diff --git a/ws_js.go b/ws_js.go index 95f13e1a..e7c92ef6 100644 --- a/ws_js.go +++ b/ws_js.go @@ -46,6 +46,7 @@ type Conn struct { // read limit for a message in bytes. msgReadLimit atomic.Int64 + readLimitSet atomic.Bool closeReadMu sync.Mutex closeReadCtx context.Context @@ -421,6 +422,11 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { // SetReadLimit implements *Conn.SetReadLimit for wasm. func (c *Conn) SetReadLimit(n int64) { + c.readLimitSet.Store(true) + c.setReadLimit(n) +} + +func (c *Conn) setReadLimit(n int64) { c.msgReadLimit.Store(n) } From a9945105ddf88fdee1cda2bb9e04fb181e419c6c Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 27 Oct 2026 17:58:31 +0000 Subject: [PATCH 02/19] test: cover the NetConn read limit rule on Wasm The Wasm Conn tracks SetReadLimit calls separately from the native Conn, so the same four cases run in the browser against the echo server. --- ws_js_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/ws_js_test.go b/ws_js_test.go index a1d846fa..40f777c4 100644 --- a/ws_js_test.go +++ b/ws_js_test.go @@ -2,6 +2,8 @@ package websocket_test import ( "context" + "io" + "net" "net/http" "os" "strings" @@ -11,6 +13,7 @@ import ( "github.com/coder/websocket" "github.com/coder/websocket/internal/test/assert" "github.com/coder/websocket/internal/test/wstest" + "github.com/coder/websocket/internal/test/xrand" ) func TestWasm(t *testing.T) { @@ -72,6 +75,62 @@ func TestWasmCloseReasonInvalidUTF8(t *testing.T) { assert.Success(t, err) } +func TestWasmNetConnReadLimit(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + netConn func(ctx context.Context, c *websocket.Conn) net.Conn + msgSize int + wantErr error + }{ + {"Unset", func(ctx context.Context, c *websocket.Conn) net.Conn { + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 65536, nil}, + {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(-1) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 65536, nil}, + {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(1024) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 4096, websocket.ErrMessageTooBig}, + {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + nc := websocket.NetConn(ctx, c, websocket.MessageBinary) + c.SetReadLimit(1024) + return nc + }, 4096, websocket.ErrMessageTooBig}, + } { + t.Run(tc.name, func(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{"echo"}, + }) + assert.Success(t, err) + // The echo server expects StatusNormalClosure. + defer c.Close(websocket.StatusNormalClosure, "") + + nc := tc.netConn(ctx, c) + msg := []byte(xrand.String(tc.msgSize)) + _, err = nc.Write(msg) + assert.Success(t, err) + + got := make([]byte, len(msg)) + _, err = io.ReadFull(nc, got) + if tc.wantErr != nil { + assert.ErrorIs(t, tc.wantErr, err) + return + } + assert.Success(t, err) + assert.Equal(t, "read msg", msg, got) + }) + } +} + func TestWasmDialTimeout(t *testing.T) { t.Parallel() From 04364e1c7f174437586b0f5811cb4bc7b64130ab Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 27 Oct 2026 18:02:59 +0000 Subject: [PATCH 03/19] test: move the Wasm NetConn read limit test to its own file The fix-close-race-net-errclosed branch adds a test at the same place in ws_js_test.go, and the two insertions conflict on merge. --- netconn_js_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ ws_js_test.go | 59 -------------------------------------- 2 files changed, 70 insertions(+), 59 deletions(-) create mode 100644 netconn_js_test.go diff --git a/netconn_js_test.go b/netconn_js_test.go new file mode 100644 index 00000000..046162ca --- /dev/null +++ b/netconn_js_test.go @@ -0,0 +1,70 @@ +package websocket_test + +import ( + "context" + "io" + "net" + "os" + "testing" + "time" + + "github.com/coder/websocket" + "github.com/coder/websocket/internal/test/assert" + "github.com/coder/websocket/internal/test/xrand" +) + +func TestWasmNetConnReadLimit(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + netConn func(ctx context.Context, c *websocket.Conn) net.Conn + msgSize int + wantErr error + }{ + {"Unset", func(ctx context.Context, c *websocket.Conn) net.Conn { + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 65536, nil}, + {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(-1) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 65536, nil}, + {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(1024) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 4096, websocket.ErrMessageTooBig}, + {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + nc := websocket.NetConn(ctx, c, websocket.MessageBinary) + c.SetReadLimit(1024) + return nc + }, 4096, websocket.ErrMessageTooBig}, + } { + t.Run(tc.name, func(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{"echo"}, + }) + assert.Success(t, err) + // The echo server expects StatusNormalClosure. + defer c.Close(websocket.StatusNormalClosure, "") + + nc := tc.netConn(ctx, c) + msg := []byte(xrand.String(tc.msgSize)) + _, err = nc.Write(msg) + assert.Success(t, err) + + got := make([]byte, len(msg)) + _, err = io.ReadFull(nc, got) + if tc.wantErr != nil { + assert.ErrorIs(t, tc.wantErr, err) + return + } + assert.Success(t, err) + assert.Equal(t, "read msg", msg, got) + }) + } +} diff --git a/ws_js_test.go b/ws_js_test.go index 40f777c4..a1d846fa 100644 --- a/ws_js_test.go +++ b/ws_js_test.go @@ -2,8 +2,6 @@ package websocket_test import ( "context" - "io" - "net" "net/http" "os" "strings" @@ -13,7 +11,6 @@ import ( "github.com/coder/websocket" "github.com/coder/websocket/internal/test/assert" "github.com/coder/websocket/internal/test/wstest" - "github.com/coder/websocket/internal/test/xrand" ) func TestWasm(t *testing.T) { @@ -75,62 +72,6 @@ func TestWasmCloseReasonInvalidUTF8(t *testing.T) { assert.Success(t, err) } -func TestWasmNetConnReadLimit(t *testing.T) { - t.Parallel() - - for _, tc := range []struct { - name string - netConn func(ctx context.Context, c *websocket.Conn) net.Conn - msgSize int - wantErr error - }{ - {"Unset", func(ctx context.Context, c *websocket.Conn) net.Conn { - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 65536, nil}, - {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(-1) - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 65536, nil}, - {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(1024) - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 4096, websocket.ErrMessageTooBig}, - {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - nc := websocket.NetConn(ctx, c, websocket.MessageBinary) - c.SetReadLimit(1024) - return nc - }, 4096, websocket.ErrMessageTooBig}, - } { - t.Run(tc.name, func(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{"echo"}, - }) - assert.Success(t, err) - // The echo server expects StatusNormalClosure. - defer c.Close(websocket.StatusNormalClosure, "") - - nc := tc.netConn(ctx, c) - msg := []byte(xrand.String(tc.msgSize)) - _, err = nc.Write(msg) - assert.Success(t, err) - - got := make([]byte, len(msg)) - _, err = io.ReadFull(nc, got) - if tc.wantErr != nil { - assert.ErrorIs(t, tc.wantErr, err) - return - } - assert.Success(t, err) - assert.Equal(t, "read msg", msg, got) - }) - } -} - func TestWasmDialTimeout(t *testing.T) { t.Parallel() From 36ab36c962b02a5b57c7fa66bc122f5f451e1391 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 19:40:43 +0000 Subject: [PATCH 04/19] docs(netconn.go): say a kept read limit applies per message A net.Conn caller reads a byte stream and cannot tell that a kept read limit counts each WebSocket message separately. Before the read limit was kept, NetConn disabled it, so the doc did not need to say this. --- netconn.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/netconn.go b/netconn.go index 1dc1e79f..554ad90b 100644 --- a/netconn.go +++ b/netconn.go @@ -45,7 +45,9 @@ import ( // io.EOF when reading. // // If SetReadLimit has not been called on c, NetConn disables the read limit. -// A limit set with SetReadLimit before or after NetConn is kept. +// A limit set with SetReadLimit before or after NetConn is kept. It applies +// to each WebSocket message, such as each Write by a peer using NetConn. +// Reading a message that exceeds it returns an error wrapping ErrMessageTooBig. func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { if !c.readLimitSet.Load() { c.setReadLimit(-1) From 6a74d2aa7d4a1d9a597ed78d4f66464a34c7a8b3 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 19:41:46 +0000 Subject: [PATCH 05/19] test: fold the disabled NetConn read limit case into the table The standalone test repeated netConn/readLimit with one added SetReadLimit(-1) call. As a table case with an expected result, it sits next to the cases it contrasts with, matching the Wasm test. --- conn_test.go | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/conn_test.go b/conn_test.go index ebf06468..114e52bb 100644 --- a/conn_test.go +++ b/conn_test.go @@ -413,16 +413,22 @@ func TestConn(t *testing.T) { for _, tc := range []struct { name string setLimit func(ctx context.Context, c *websocket.Conn) net.Conn + msgSize int + wantErr error }{ + {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(-1) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, 65536, nil}, {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { c.SetReadLimit(1024) return websocket.NetConn(ctx, c, websocket.MessageBinary) - }}, + }, 4096, websocket.ErrMessageTooBig}, {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { nc := websocket.NetConn(ctx, c, websocket.MessageBinary) c.SetReadLimit(1024) return nc - }}, + }, 4096, websocket.ErrMessageTooBig}, } { t.Run("netConn/readLimitKept/"+tc.name, func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) @@ -430,39 +436,24 @@ func TestConn(t *testing.T) { n1 := tc.setLimit(tt.ctx, c1) _ = c2.CloseRead(tt.ctx) + msg := xrand.Bytes(tc.msgSize) writeDone := xsync.Go(func() error { - return c2.Write(tt.ctx, websocket.MessageBinary, make([]byte, 4096)) + return c2.Write(tt.ctx, websocket.MessageBinary, msg) }) - _, err := io.ReadFull(n1, make([]byte, 4096)) - assert.ErrorIs(t, websocket.ErrMessageTooBig, err) - - _ = c2.CloseNow() - <-writeDone - }) - } - - t.Run("netConn/readLimitDisabledBeforeNetConn", func(t *testing.T) { - tt, c1, c2 := newConnTest(t, nil, nil) - - c1.SetReadLimit(-1) - n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary) - n2 := websocket.NetConn(tt.ctx, c2, websocket.MessageBinary) - - s := strings.Repeat("papa", 1<<20) - errs := xsync.Go(func() error { - _, err := n2.Write([]byte(s)) - if err != nil { - return err + got := make([]byte, len(msg)) + _, err := io.ReadFull(n1, got) + if tc.wantErr != nil { + assert.ErrorIs(t, tc.wantErr, err) + _ = c2.CloseNow() + <-writeDone + return } - return n2.Close() + assert.Success(t, err) + assert.Success(t, <-writeDone) + assert.Equal(t, "read msg", msg, got) }) - - b, err := io.ReadAll(n1) - assert.Success(t, err) - assert.Success(t, <-errs) - assert.Equal(t, "read msg", s, string(b)) - }) + } t.Run("netConn/pastDeadline", func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) From 71d67301eaf7e0b2572d7208483fc4efa1af4fbc Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 19:42:12 +0000 Subject: [PATCH 06/19] test: name the NetConn read limit test message sizes Each message size must exceed a limit defined elsewhere, and a bare literal hides which one. Naming the sizes by the limit they exceed shows which cases stop testing anything if a default or limit changes. --- conn_test.go | 16 +++++++++++----- netconn_js_test.go | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/conn_test.go b/conn_test.go index 114e52bb..b803c810 100644 --- a/conn_test.go +++ b/conn_test.go @@ -410,6 +410,12 @@ func TestConn(t *testing.T) { assert.Equal(t, "read msg", s, string(b)) }) + const ( + readLimit = 1024 + overReadLimit = 4 * readLimit + overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. + ) + for _, tc := range []struct { name string setLimit func(ctx context.Context, c *websocket.Conn) net.Conn @@ -419,16 +425,16 @@ func TestConn(t *testing.T) { {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { c.SetReadLimit(-1) return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 65536, nil}, + }, overDefaultLimit, nil}, {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(1024) + c.SetReadLimit(readLimit) return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 4096, websocket.ErrMessageTooBig}, + }, overReadLimit, websocket.ErrMessageTooBig}, {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { nc := websocket.NetConn(ctx, c, websocket.MessageBinary) - c.SetReadLimit(1024) + c.SetReadLimit(readLimit) return nc - }, 4096, websocket.ErrMessageTooBig}, + }, overReadLimit, websocket.ErrMessageTooBig}, } { t.Run("netConn/readLimitKept/"+tc.name, func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) diff --git a/netconn_js_test.go b/netconn_js_test.go index 046162ca..d4cffccb 100644 --- a/netconn_js_test.go +++ b/netconn_js_test.go @@ -16,6 +16,12 @@ import ( func TestWasmNetConnReadLimit(t *testing.T) { t.Parallel() + const ( + readLimit = 1024 + overReadLimit = 4 * readLimit + overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. + ) + for _, tc := range []struct { name string netConn func(ctx context.Context, c *websocket.Conn) net.Conn @@ -24,20 +30,20 @@ func TestWasmNetConnReadLimit(t *testing.T) { }{ {"Unset", func(ctx context.Context, c *websocket.Conn) net.Conn { return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 65536, nil}, + }, overDefaultLimit, nil}, {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { c.SetReadLimit(-1) return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 65536, nil}, + }, overDefaultLimit, nil}, {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(1024) + c.SetReadLimit(readLimit) return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, 4096, websocket.ErrMessageTooBig}, + }, overReadLimit, websocket.ErrMessageTooBig}, {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { nc := websocket.NetConn(ctx, c, websocket.MessageBinary) - c.SetReadLimit(1024) + c.SetReadLimit(readLimit) return nc - }, 4096, websocket.ErrMessageTooBig}, + }, overReadLimit, websocket.ErrMessageTooBig}, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() From d8e9d6929a398af7f864148c41f11fbbea2e0773 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 19:57:40 +0000 Subject: [PATCH 07/19] test: name the NetConn read limit cases the same on both platforms The native closure was named setLimit although it also creates the net.Conn, and the native cases ran as TestConn subtests under different names than the Wasm cases. A top-level TestNetConnReadLimit with a netConn field matches TestWasmNetConnReadLimit case for case. --- conn_test.go | 106 ++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/conn_test.go b/conn_test.go index b803c810..bbdd6c94 100644 --- a/conn_test.go +++ b/conn_test.go @@ -410,57 +410,6 @@ func TestConn(t *testing.T) { assert.Equal(t, "read msg", s, string(b)) }) - const ( - readLimit = 1024 - overReadLimit = 4 * readLimit - overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. - ) - - for _, tc := range []struct { - name string - setLimit func(ctx context.Context, c *websocket.Conn) net.Conn - msgSize int - wantErr error - }{ - {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(-1) - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, overDefaultLimit, nil}, - {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(readLimit) - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, overReadLimit, websocket.ErrMessageTooBig}, - {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - nc := websocket.NetConn(ctx, c, websocket.MessageBinary) - c.SetReadLimit(readLimit) - return nc - }, overReadLimit, websocket.ErrMessageTooBig}, - } { - t.Run("netConn/readLimitKept/"+tc.name, func(t *testing.T) { - tt, c1, c2 := newConnTest(t, nil, nil) - - n1 := tc.setLimit(tt.ctx, c1) - _ = c2.CloseRead(tt.ctx) - - msg := xrand.Bytes(tc.msgSize) - writeDone := xsync.Go(func() error { - return c2.Write(tt.ctx, websocket.MessageBinary, msg) - }) - - got := make([]byte, len(msg)) - _, err := io.ReadFull(n1, got) - if tc.wantErr != nil { - assert.ErrorIs(t, tc.wantErr, err) - _ = c2.CloseNow() - <-writeDone - return - } - assert.Success(t, err) - assert.Success(t, <-writeDone) - assert.Equal(t, "read msg", msg, got) - }) - } - t.Run("netConn/pastDeadline", func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) @@ -587,6 +536,61 @@ func TestConn(t *testing.T) { }) } +func TestNetConnReadLimit(t *testing.T) { + t.Parallel() + + const ( + readLimit = 1024 + overReadLimit = 4 * readLimit + overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. + ) + + for _, tc := range []struct { + name string + netConn func(ctx context.Context, c *websocket.Conn) net.Conn + msgSize int + wantErr error + }{ + {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(-1) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, overDefaultLimit, nil}, + {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(readLimit) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, overReadLimit, websocket.ErrMessageTooBig}, + {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + nc := websocket.NetConn(ctx, c, websocket.MessageBinary) + c.SetReadLimit(readLimit) + return nc + }, overReadLimit, websocket.ErrMessageTooBig}, + } { + t.Run(tc.name, func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + n1 := tc.netConn(tt.ctx, c1) + _ = c2.CloseRead(tt.ctx) + + msg := xrand.Bytes(tc.msgSize) + writeDone := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageBinary, msg) + }) + + got := make([]byte, len(msg)) + _, err := io.ReadFull(n1, got) + if tc.wantErr != nil { + assert.ErrorIs(t, tc.wantErr, err) + _ = c2.CloseNow() + <-writeDone + return + } + assert.Success(t, err) + assert.Success(t, <-writeDone) + assert.Equal(t, "read msg", msg, got) + }) + } +} + func TestWasm(t *testing.T) { t.Parallel() if os.Getenv("CI") == "" { From 585c3a1d95bd64542b00865955e86bad3b21fd9e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 19:58:55 +0000 Subject: [PATCH 08/19] test: check the default read limit the NetConn cases rely on The cases that expect NetConn to read without error only test something while their message exceeds the default read limit. A control case that reads the same size without NetConn fails if the default is raised to that size, instead of letting those cases pass without a limit to disable. --- conn_test.go | 17 +++++++++++++++++ netconn_js_test.go | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/conn_test.go b/conn_test.go index bbdd6c94..12a9f583 100644 --- a/conn_test.go +++ b/conn_test.go @@ -545,6 +545,23 @@ func TestNetConnReadLimit(t *testing.T) { overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. ) + // A default at or above overDefaultLimit fails this case instead of letting + // the cases that expect no error pass without a limit to disable. + t.Run("DefaultWithoutNetConn", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + _ = c2.CloseRead(tt.ctx) + writeDone := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageBinary, xrand.Bytes(overDefaultLimit)) + }) + + _, _, err := c1.Read(tt.ctx) + assert.ErrorIs(t, websocket.ErrMessageTooBig, err) + + _ = c2.CloseNow() + <-writeDone + }) + for _, tc := range []struct { name string netConn func(ctx context.Context, c *websocket.Conn) net.Conn diff --git a/netconn_js_test.go b/netconn_js_test.go index d4cffccb..5e5ef1be 100644 --- a/netconn_js_test.go +++ b/netconn_js_test.go @@ -22,6 +22,27 @@ func TestWasmNetConnReadLimit(t *testing.T) { overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. ) + // A default at or above overDefaultLimit fails this case instead of letting + // the cases that expect no error pass without a limit to disable. + t.Run("DefaultWithoutNetConn", func(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{"echo"}, + }) + assert.Success(t, err) + defer c.Close(websocket.StatusNormalClosure, "") + + err = c.Write(ctx, websocket.MessageBinary, xrand.Bytes(overDefaultLimit)) + assert.Success(t, err) + + _, _, err = c.Read(ctx) + assert.ErrorIs(t, websocket.ErrMessageTooBig, err) + }) + for _, tc := range []struct { name string netConn func(ctx context.Context, c *websocket.Conn) net.Conn From 55cf4bd816f6b404279793f0c626b66aeaa0a62f Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 20:49:28 +0000 Subject: [PATCH 09/19] fix: prevent NetConn from overwriting a concurrent read limit NetConn checked readLimitSet and then disabled the limit, so a SetReadLimit that ran between the two steps was overwritten and left the connection without a read limit. A mutex around both operations makes the check and the update atomic. --- conn.go | 6 +++++- netconn.go | 4 +++- read.go | 4 +++- ws_js.go | 10 ++++++++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/conn.go b/conn.go index 1df92623..0a3377f7 100644 --- a/conn.go +++ b/conn.go @@ -59,7 +59,11 @@ type Conn struct { readHeaderBuf [8]byte readControlBuf [maxControlPayload]byte msgReader *msgReader - readLimitSet atomic.Bool + + // readLimitMu makes NetConn's readLimitSet check atomic with SetReadLimit. + // It is never held during I/O, so it need not be the context-aware mu. + readLimitMu sync.Mutex + readLimitSet bool // Write state. msgWriter *msgWriter diff --git a/netconn.go b/netconn.go index 554ad90b..f1b46ba7 100644 --- a/netconn.go +++ b/netconn.go @@ -49,9 +49,11 @@ import ( // to each WebSocket message, such as each Write by a peer using NetConn. // Reading a message that exceeds it returns an error wrapping ErrMessageTooBig. func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { - if !c.readLimitSet.Load() { + c.readLimitMu.Lock() + if !c.readLimitSet { c.setReadLimit(-1) } + c.readLimitMu.Unlock() nc := &netConn{ c: c, diff --git a/read.go b/read.go index 15663029..4f9cf7d4 100644 --- a/read.go +++ b/read.go @@ -95,7 +95,9 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { // // Set to -1 to disable. func (c *Conn) SetReadLimit(n int64) { - c.readLimitSet.Store(true) + c.readLimitMu.Lock() + defer c.readLimitMu.Unlock() + c.readLimitSet = true c.setReadLimit(n) } diff --git a/ws_js.go b/ws_js.go index e7c92ef6..376802a5 100644 --- a/ws_js.go +++ b/ws_js.go @@ -46,7 +46,11 @@ type Conn struct { // read limit for a message in bytes. msgReadLimit atomic.Int64 - readLimitSet atomic.Bool + + // readLimitMu makes NetConn's readLimitSet check atomic with SetReadLimit. + // It is never held during I/O, so it need not be the context-aware mu. + readLimitMu sync.Mutex + readLimitSet bool closeReadMu sync.Mutex closeReadCtx context.Context @@ -422,7 +426,9 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context { // SetReadLimit implements *Conn.SetReadLimit for wasm. func (c *Conn) SetReadLimit(n int64) { - c.readLimitSet.Store(true) + c.readLimitMu.Lock() + defer c.readLimitMu.Unlock() + c.readLimitSet = true c.setReadLimit(n) } From bce73d3d71e3b1e86a0cdfd62db8ce5f9eaebfe3 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 3 Nov 2026 21:02:33 +0000 Subject: [PATCH 10/19] docs: say what readLimitSet means on both Conn types The field comment described only readLimitMu. A reader also needs to know that only SetReadLimit sets readLimitSet, because NetConn disables a limit set through setReadLimit alone. --- conn.go | 5 +++-- ws_js.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/conn.go b/conn.go index 0a3377f7..7c7c945b 100644 --- a/conn.go +++ b/conn.go @@ -60,8 +60,9 @@ type Conn struct { readControlBuf [maxControlPayload]byte msgReader *msgReader - // readLimitMu makes NetConn's readLimitSet check atomic with SetReadLimit. - // It is never held during I/O, so it need not be the context-aware mu. + // readLimitSet is true once SetReadLimit has been called, so NetConn + // keeps the limit. readLimitMu makes NetConn's check and update atomic + // with SetReadLimit. It is never held during I/O. readLimitMu sync.Mutex readLimitSet bool diff --git a/ws_js.go b/ws_js.go index 376802a5..6c328b95 100644 --- a/ws_js.go +++ b/ws_js.go @@ -47,8 +47,9 @@ type Conn struct { // read limit for a message in bytes. msgReadLimit atomic.Int64 - // readLimitMu makes NetConn's readLimitSet check atomic with SetReadLimit. - // It is never held during I/O, so it need not be the context-aware mu. + // readLimitSet is true once SetReadLimit has been called, so NetConn + // keeps the limit. readLimitMu makes NetConn's check and update atomic + // with SetReadLimit. It is never held during I/O. readLimitMu sync.Mutex readLimitSet bool From 6450ffc22cb18f35e4d79908aada2674b14e1d78 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 4 Nov 2026 11:20:34 +0000 Subject: [PATCH 11/19] test: keep the NetConn read limit cases in TestConn A separate TestNetConnReadLimit split the NetConn read limit tests across two functions, because the case without a limit stays in TestConn as netConn/readLimit. The cases now run under that name. --- conn_test.go | 140 +++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 72 deletions(-) diff --git a/conn_test.go b/conn_test.go index 12a9f583..a5830b77 100644 --- a/conn_test.go +++ b/conn_test.go @@ -410,6 +410,74 @@ func TestConn(t *testing.T) { assert.Equal(t, "read msg", s, string(b)) }) + const ( + readLimit = 1024 + overReadLimit = 4 * readLimit + overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. + ) + + // A default at or above overDefaultLimit fails this case instead of letting + // the cases that expect no error pass without a limit to disable. + t.Run("netConn/readLimit/DefaultWithoutNetConn", func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + _ = c2.CloseRead(tt.ctx) + writeDone := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageBinary, xrand.Bytes(overDefaultLimit)) + }) + + _, _, err := c1.Read(tt.ctx) + assert.ErrorIs(t, websocket.ErrMessageTooBig, err) + + _ = c2.CloseNow() + <-writeDone + }) + + for _, tc := range []struct { + name string + netConn func(ctx context.Context, c *websocket.Conn) net.Conn + msgSize int + wantErr error + }{ + {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(-1) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, overDefaultLimit, nil}, + {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + c.SetReadLimit(readLimit) + return websocket.NetConn(ctx, c, websocket.MessageBinary) + }, overReadLimit, websocket.ErrMessageTooBig}, + {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + nc := websocket.NetConn(ctx, c, websocket.MessageBinary) + c.SetReadLimit(readLimit) + return nc + }, overReadLimit, websocket.ErrMessageTooBig}, + } { + t.Run("netConn/readLimit/"+tc.name, func(t *testing.T) { + tt, c1, c2 := newConnTest(t, nil, nil) + + n1 := tc.netConn(tt.ctx, c1) + _ = c2.CloseRead(tt.ctx) + + msg := xrand.Bytes(tc.msgSize) + writeDone := xsync.Go(func() error { + return c2.Write(tt.ctx, websocket.MessageBinary, msg) + }) + + got := make([]byte, len(msg)) + _, err := io.ReadFull(n1, got) + if tc.wantErr != nil { + assert.ErrorIs(t, tc.wantErr, err) + _ = c2.CloseNow() + <-writeDone + return + } + assert.Success(t, err) + assert.Success(t, <-writeDone) + assert.Equal(t, "read msg", msg, got) + }) + } + t.Run("netConn/pastDeadline", func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) @@ -536,78 +604,6 @@ func TestConn(t *testing.T) { }) } -func TestNetConnReadLimit(t *testing.T) { - t.Parallel() - - const ( - readLimit = 1024 - overReadLimit = 4 * readLimit - overDefaultLimit = 2 * 32768 // SetReadLimit documents the 32768 byte default. - ) - - // A default at or above overDefaultLimit fails this case instead of letting - // the cases that expect no error pass without a limit to disable. - t.Run("DefaultWithoutNetConn", func(t *testing.T) { - tt, c1, c2 := newConnTest(t, nil, nil) - - _ = c2.CloseRead(tt.ctx) - writeDone := xsync.Go(func() error { - return c2.Write(tt.ctx, websocket.MessageBinary, xrand.Bytes(overDefaultLimit)) - }) - - _, _, err := c1.Read(tt.ctx) - assert.ErrorIs(t, websocket.ErrMessageTooBig, err) - - _ = c2.CloseNow() - <-writeDone - }) - - for _, tc := range []struct { - name string - netConn func(ctx context.Context, c *websocket.Conn) net.Conn - msgSize int - wantErr error - }{ - {"DisabledBeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(-1) - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, overDefaultLimit, nil}, - {"BeforeNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - c.SetReadLimit(readLimit) - return websocket.NetConn(ctx, c, websocket.MessageBinary) - }, overReadLimit, websocket.ErrMessageTooBig}, - {"AfterNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { - nc := websocket.NetConn(ctx, c, websocket.MessageBinary) - c.SetReadLimit(readLimit) - return nc - }, overReadLimit, websocket.ErrMessageTooBig}, - } { - t.Run(tc.name, func(t *testing.T) { - tt, c1, c2 := newConnTest(t, nil, nil) - - n1 := tc.netConn(tt.ctx, c1) - _ = c2.CloseRead(tt.ctx) - - msg := xrand.Bytes(tc.msgSize) - writeDone := xsync.Go(func() error { - return c2.Write(tt.ctx, websocket.MessageBinary, msg) - }) - - got := make([]byte, len(msg)) - _, err := io.ReadFull(n1, got) - if tc.wantErr != nil { - assert.ErrorIs(t, tc.wantErr, err) - _ = c2.CloseNow() - <-writeDone - return - } - assert.Success(t, err) - assert.Success(t, <-writeDone) - assert.Equal(t, "read msg", msg, got) - }) - } -} - func TestWasm(t *testing.T) { t.Parallel() if os.Getenv("CI") == "" { From a90083ff265b671151496da278a810c41b6bbde6 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 4 Nov 2026 16:38:52 +0000 Subject: [PATCH 12/19] docs: fix comment sentences that start lowercase or give code intent The readLimitSet comment started a sentence with readLimitMu and said NetConn keeps the limit. The Wasm test said the echo server expects a status instead of saying what fails. --- conn.go | 7 ++++--- netconn_js_test.go | 3 ++- ws_js.go | 7 ++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/conn.go b/conn.go index 7c7c945b..61ebaa64 100644 --- a/conn.go +++ b/conn.go @@ -60,9 +60,10 @@ type Conn struct { readControlBuf [maxControlPayload]byte msgReader *msgReader - // readLimitSet is true once SetReadLimit has been called, so NetConn - // keeps the limit. readLimitMu makes NetConn's check and update atomic - // with SetReadLimit. It is never held during I/O. + // readLimitSet is true once SetReadLimit has been called, and NetConn + // then leaves the limit unchanged. Holding readLimitMu makes NetConn's + // check of readLimitSet and its update of the limit atomic with + // SetReadLimit. The mutex is never held during I/O. readLimitMu sync.Mutex readLimitSet bool diff --git a/netconn_js_test.go b/netconn_js_test.go index 5e5ef1be..2daeab62 100644 --- a/netconn_js_test.go +++ b/netconn_js_test.go @@ -76,7 +76,8 @@ func TestWasmNetConnReadLimit(t *testing.T) { Subprotocols: []string{"echo"}, }) assert.Success(t, err) - // The echo server expects StatusNormalClosure. + // The echo server fails TestWasm unless the close status is + // StatusNormalClosure. defer c.Close(websocket.StatusNormalClosure, "") nc := tc.netConn(ctx, c) diff --git a/ws_js.go b/ws_js.go index 6c328b95..a5ec3221 100644 --- a/ws_js.go +++ b/ws_js.go @@ -47,9 +47,10 @@ type Conn struct { // read limit for a message in bytes. msgReadLimit atomic.Int64 - // readLimitSet is true once SetReadLimit has been called, so NetConn - // keeps the limit. readLimitMu makes NetConn's check and update atomic - // with SetReadLimit. It is never held during I/O. + // readLimitSet is true once SetReadLimit has been called, and NetConn + // then leaves the limit unchanged. Holding readLimitMu makes NetConn's + // check of readLimitSet and its update of the limit atomic with + // SetReadLimit. The mutex is never held during I/O. readLimitMu sync.Mutex readLimitSet bool From 52e383ca619e9393314fb21d0d42cfa67b3fcad6 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 4 Nov 2026 17:22:54 +0000 Subject: [PATCH 13/19] docs(netconn.go): say why NetConn disables the read limit Three of the four sentences repeated the NetConn and SetReadLimit docs. The doc now states the rule once and the reason for it. --- netconn.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/netconn.go b/netconn.go index f1b46ba7..ce0564f9 100644 --- a/netconn.go +++ b/netconn.go @@ -44,10 +44,8 @@ import ( // A received StatusNormalClosure or StatusGoingAway close frame will be translated to // io.EOF when reading. // -// If SetReadLimit has not been called on c, NetConn disables the read limit. -// A limit set with SetReadLimit before or after NetConn is kept. It applies -// to each WebSocket message, such as each Write by a peer using NetConn. -// Reading a message that exceeds it returns an error wrapping ErrMessageTooBig. +// NetConn disables the read limit unless SetReadLimit has been called on c, +// because each Write to a net.Conn becomes one message and can be any size. func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { c.readLimitMu.Lock() if !c.readLimitSet { From 3eb3568fc44fec9bcd6bc5bb4ec9309d7a2424c0 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 5 Nov 2026 12:06:26 +0000 Subject: [PATCH 14/19] test: run SetReadLimit concurrently with NetConn Without readLimitMu, the race detector reports the unsynchronized readLimitSet access on every run, so this case catches a removed lock under go test -race. --- conn_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/conn_test.go b/conn_test.go index a5830b77..3356bb63 100644 --- a/conn_test.go +++ b/conn_test.go @@ -452,6 +452,15 @@ func TestConn(t *testing.T) { c.SetReadLimit(readLimit) return nc }, overReadLimit, websocket.ErrMessageTooBig}, + {"ConcurrentWithNetConn", func(ctx context.Context, c *websocket.Conn) net.Conn { + set := xsync.Go(func() error { + c.SetReadLimit(readLimit) + return nil + }) + nc := websocket.NetConn(ctx, c, websocket.MessageBinary) + <-set + return nc + }, overReadLimit, websocket.ErrMessageTooBig}, } { t.Run("netConn/readLimit/"+tc.name, func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) From 5fff897cc161c288ebc08ca879dfdf39b5ec2956 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 5 Nov 2026 12:09:44 +0000 Subject: [PATCH 15/19] refactor: keep the NetConn read limit locking per platform NetConn is shared by both platforms but locked readLimitMu and read readLimitSet, private fields that conn.go and ws_js.go each declare. The check now lives next to SetReadLimit on each platform. --- netconn.go | 6 +----- read.go | 8 ++++++++ ws_js.go | 8 ++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/netconn.go b/netconn.go index ce0564f9..be27b25a 100644 --- a/netconn.go +++ b/netconn.go @@ -47,11 +47,7 @@ import ( // NetConn disables the read limit unless SetReadLimit has been called on c, // because each Write to a net.Conn becomes one message and can be any size. func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn { - c.readLimitMu.Lock() - if !c.readLimitSet { - c.setReadLimit(-1) - } - c.readLimitMu.Unlock() + c.disableReadLimitUnlessSet() nc := &netConn{ c: c, diff --git a/read.go b/read.go index 4f9cf7d4..417100d7 100644 --- a/read.go +++ b/read.go @@ -101,6 +101,14 @@ func (c *Conn) SetReadLimit(n int64) { c.setReadLimit(n) } +func (c *Conn) disableReadLimitUnlessSet() { + c.readLimitMu.Lock() + defer c.readLimitMu.Unlock() + if !c.readLimitSet { + c.setReadLimit(-1) + } +} + func (c *Conn) setReadLimit(n int64) { if n >= 0 { // We read one more byte than the limit in case diff --git a/ws_js.go b/ws_js.go index a5ec3221..01f76a14 100644 --- a/ws_js.go +++ b/ws_js.go @@ -434,6 +434,14 @@ func (c *Conn) SetReadLimit(n int64) { c.setReadLimit(n) } +func (c *Conn) disableReadLimitUnlessSet() { + c.readLimitMu.Lock() + defer c.readLimitMu.Unlock() + if !c.readLimitSet { + c.setReadLimit(-1) + } +} + func (c *Conn) setReadLimit(n int64) { c.msgReadLimit.Store(n) } From 06124b16cad73a3c57cfb8ff8d66bf77d9693730 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 5 Nov 2026 12:41:11 +0000 Subject: [PATCH 16/19] docs: say that setReadLimit does not set readLimitSet A caller that expects it to behave like SetReadLimit would have its limit disabled by the next NetConn. --- read.go | 2 ++ ws_js.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/read.go b/read.go index 417100d7..d953ceb6 100644 --- a/read.go +++ b/read.go @@ -109,6 +109,8 @@ func (c *Conn) disableReadLimitUnlessSet() { } } +// setReadLimit does not set readLimitSet, so NetConn can still disable the +// limit. func (c *Conn) setReadLimit(n int64) { if n >= 0 { // We read one more byte than the limit in case diff --git a/ws_js.go b/ws_js.go index 01f76a14..a139d515 100644 --- a/ws_js.go +++ b/ws_js.go @@ -442,6 +442,8 @@ func (c *Conn) disableReadLimitUnlessSet() { } } +// setReadLimit does not set readLimitSet, so NetConn can still disable the +// limit. func (c *Conn) setReadLimit(n int64) { c.msgReadLimit.Store(n) } From af53fde3d9aa8423ec09530564ff776b9dda0a8f Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 6 Nov 2026 19:41:27 +0000 Subject: [PATCH 17/19] refactor: inline setReadLimit Its two callers shared no logic, because disableReadLimitUnlessSet always passes -1, and it was an unlocked way to change the limit without setting readLimitSet. The limit and readLimitSet now change only inside the two locked methods. --- read.go | 22 ++++++++-------------- ws_js.go | 10 ++-------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/read.go b/read.go index d953ceb6..ce46f38c 100644 --- a/read.go +++ b/read.go @@ -98,20 +98,6 @@ func (c *Conn) SetReadLimit(n int64) { c.readLimitMu.Lock() defer c.readLimitMu.Unlock() c.readLimitSet = true - c.setReadLimit(n) -} - -func (c *Conn) disableReadLimitUnlessSet() { - c.readLimitMu.Lock() - defer c.readLimitMu.Unlock() - if !c.readLimitSet { - c.setReadLimit(-1) - } -} - -// setReadLimit does not set readLimitSet, so NetConn can still disable the -// limit. -func (c *Conn) setReadLimit(n int64) { if n >= 0 { // We read one more byte than the limit in case // there is a fin frame that needs to be read. @@ -121,6 +107,14 @@ func (c *Conn) setReadLimit(n int64) { c.msgReader.limitReader.limit.Store(n) } +func (c *Conn) disableReadLimitUnlessSet() { + c.readLimitMu.Lock() + defer c.readLimitMu.Unlock() + if !c.readLimitSet { + c.msgReader.limitReader.limit.Store(-1) + } +} + const defaultReadLimit = 32768 func newMsgReader(c *Conn) *msgReader { diff --git a/ws_js.go b/ws_js.go index a139d515..54a29cc0 100644 --- a/ws_js.go +++ b/ws_js.go @@ -431,23 +431,17 @@ func (c *Conn) SetReadLimit(n int64) { c.readLimitMu.Lock() defer c.readLimitMu.Unlock() c.readLimitSet = true - c.setReadLimit(n) + c.msgReadLimit.Store(n) } func (c *Conn) disableReadLimitUnlessSet() { c.readLimitMu.Lock() defer c.readLimitMu.Unlock() if !c.readLimitSet { - c.setReadLimit(-1) + c.msgReadLimit.Store(-1) } } -// setReadLimit does not set readLimitSet, so NetConn can still disable the -// limit. -func (c *Conn) setReadLimit(n int64) { - c.msgReadLimit.Store(n) -} - func (c *Conn) setCloseErr(err error) { c.closeErrOnce.Do(func() { c.closeErr = fmt.Errorf("WebSocket closed: %w", err) From 673537df01a75e06bfb43adf52bb3eda7170f6bd Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 6 Nov 2026 19:46:30 +0000 Subject: [PATCH 18/19] docs: shorten the readLimitMu and readLimitSet comment It described the body of disableReadLimitUnlessSet. The mutex now uses the "Protects following" form that closeMu uses. --- conn.go | 6 ++---- ws_js.go | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/conn.go b/conn.go index 61ebaa64..6761da3f 100644 --- a/conn.go +++ b/conn.go @@ -60,11 +60,9 @@ type Conn struct { readControlBuf [maxControlPayload]byte msgReader *msgReader + readLimitMu sync.Mutex // Protects following. Never held during I/O. // readLimitSet is true once SetReadLimit has been called, and NetConn - // then leaves the limit unchanged. Holding readLimitMu makes NetConn's - // check of readLimitSet and its update of the limit atomic with - // SetReadLimit. The mutex is never held during I/O. - readLimitMu sync.Mutex + // then leaves the limit unchanged. readLimitSet bool // Write state. diff --git a/ws_js.go b/ws_js.go index 54a29cc0..fb045748 100644 --- a/ws_js.go +++ b/ws_js.go @@ -47,11 +47,9 @@ type Conn struct { // read limit for a message in bytes. msgReadLimit atomic.Int64 + readLimitMu sync.Mutex // Protects following. Never held during I/O. // readLimitSet is true once SetReadLimit has been called, and NetConn - // then leaves the limit unchanged. Holding readLimitMu makes NetConn's - // check of readLimitSet and its update of the limit atomic with - // SetReadLimit. The mutex is never held during I/O. - readLimitMu sync.Mutex + // then leaves the limit unchanged. readLimitSet bool closeReadMu sync.Mutex From 009bbd4b9aec7f8e924ede4e4e65338afcb5d2da Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 6 Nov 2026 19:53:55 +0000 Subject: [PATCH 19/19] test(conn_test.go): name the no limit NetConn case Unset Next to the netConn/readLimit/ subtests, netConn/readLimit read as their parent. The Wasm table already calls this case Unset. --- conn_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn_test.go b/conn_test.go index 3356bb63..fb0aaacb 100644 --- a/conn_test.go +++ b/conn_test.go @@ -379,7 +379,7 @@ func TestConn(t *testing.T) { } }) - t.Run("netConn/readLimit", func(t *testing.T) { + t.Run("netConn/readLimit/Unset", func(t *testing.T) { tt, c1, c2 := newConnTest(t, nil, nil) n1 := websocket.NetConn(tt.ctx, c1, websocket.MessageBinary)