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
b050273
fix: keep a read limit set before NetConn
mafredri eeacc7f
test: cover the NetConn read limit rule on Wasm
mafredri 125d60d
test: move the Wasm NetConn read limit test to its own file
mafredri cb74ece
docs(netconn.go): say a kept read limit applies per message
mafredri 598dd5b
test: fold the disabled NetConn read limit case into the table
mafredri 01daeff
test: name the NetConn read limit test message sizes
mafredri ea90a58
test: name the NetConn read limit cases the same on both platforms
mafredri 32c8a81
test: check the default read limit the NetConn cases rely on
mafredri 69b18fe
fix: prevent NetConn from overwriting a concurrent read limit
mafredri bc04f4b
docs: say what readLimitSet means on both Conn types
mafredri 25b1918
test: keep the NetConn read limit cases in TestConn
mafredri a43a2eb
docs: fix comment sentences that start lowercase or give code intent
mafredri 9c7b5c8
docs(netconn.go): say why NetConn disables the read limit
mafredri 60e691c
test: run SetReadLimit concurrently with NetConn
mafredri edbbce1
refactor: keep the NetConn read limit locking per platform
mafredri b520823
docs: say that setReadLimit does not set readLimitSet
mafredri 1b42378
refactor: inline setReadLimit
mafredri 6e0fee0
docs: shorten the readLimitMu and readLimitSet comment
mafredri fced513
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.