Repository navigation
fix: keep a read limit set before NetConn #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mafredri
wants to merge
19
commits into
master
Choose a base branch
from
mafredri/netconn-keep-read-limit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−3
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
06e2eb3
fix: keep a read limit set before NetConn
mafredri 3042528
test: cover the NetConn read limit rule on Wasm
mafredri 903237c
test: move the Wasm NetConn read limit test to its own file
mafredri c0f7ff5
docs(netconn.go): say a kept read limit applies per message
mafredri a374d00
test: fold the disabled NetConn read limit case into the table
mafredri 17a14f3
test: name the NetConn read limit test message sizes
mafredri e9eb77e
test: name the NetConn read limit cases the same on both platforms
mafredri 9f63442
test: check the default read limit the NetConn cases rely on
mafredri 1e0ed63
fix: prevent NetConn from overwriting a concurrent read limit
mafredri 5bdbb43
docs: say what readLimitSet means on both Conn types
mafredri 4853e33
test: keep the NetConn read limit cases in TestConn
mafredri e721587
docs: fix comment sentences that start lowercase or give code intent
mafredri d7ce30c
docs(netconn.go): say why NetConn disables the read limit
mafredri fd46fb8
test: run SetReadLimit concurrently with NetConn
mafredri aae33ed
refactor: keep the NetConn read limit locking per platform
mafredri c1c6eb9
docs: say that setReadLimit does not set readLimitSet
mafredri 426dd22
refactor: inline setReadLimit
mafredri 7772ac7
docs: shorten the readLimitMu and readLimitSet comment
mafredri 44b8a01
test(conn_test.go): name the no limit NetConn case Unset
mafredri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| 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() | ||
|
|
||
| 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) { | ||
| 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 | ||
| msgSize int | ||
| wantErr error | ||
| }{ | ||
| {"Unset", func(ctx context.Context, c *websocket.Conn) net.Conn { | ||
| return websocket.NetConn(ctx, c, websocket.MessageBinary) | ||
| }, overDefaultLimit, nil}, | ||
| {"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) { | ||
| 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 fails TestWasm unless the close status is | ||
| // 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) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P4 [CRF-22]
// Protects following.saysreadLimitMuguardsreadLimitSet, but the lock must also be held across each store to the limit, and after6e0fee0no comment says so (same text onws_js.go:50). (Gon)The limit (
limitReader.limitnatively,msgReadLimiton Wasm) is an atomic declared elsewhere, so a maintainer can movelimit.Store(-1)indisableReadLimitUnlessSetafter the unlock, which lets a concurrentSetReadLimit(n)be replaced by-1again. That edit is race-detector clean: with the flag copied under the lock and-1stored afterUnlock,go test -race -count=30 -run 'TestConn/netConn/readLimit' .passed. Suggested text on both types:readLimitMu sync.Mutex // Protects following and is held across each read limit store. Never held during I/O.