-
Notifications
You must be signed in to change notification settings - Fork 325
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
fix panic: concurrent write to websocket connection #3908
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3908 +/- ##
==========================================
+ Coverage 75.38% 76.05% +0.67%
==========================================
Files 303 327 +24
Lines 25923 27855 +1932
==========================================
+ Hits 19541 21186 +1645
- Misses 5360 5578 +218
- Partials 1022 1091 +69
... and 5 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@@ -36,6 +37,41 @@ var upgrader = websocket.Upgrader{ | |||
WriteBufferSize: 1024, | |||
} | |||
|
|||
// type safeWebsocketConn wraps websocket.Conn with a mutex |
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.
the link could be added here
https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency
good finding |
@@ -70,7 +104,8 @@ func (wsSvr *WebsocketHandler) handleConnection(ctx context.Context, ws *websock | |||
}) | |||
|
|||
ctx, cancel := context.WithCancel(ctx) | |||
go ping(ctx, ws, cancel) | |||
safeWs := &safeWebsocketConn{ws: ws} | |||
go ping(ctx, safeWs, cancel) |
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.
- according to https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency, Reader method also need concurrent control, like
SetReadDeadline
,SetReadLimit
- L95-103 above,
SetReadDeadline
called 2 times, is that a duplicate?
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.
- see websocket code, panic is only triggered on write(WriteMessage, WriteJSON), Set* will not
if c.isWriting {
panic("concurrent write to websocket connection")
}
- the first one is used to set the initial read deadline, ensuring that data is read from the WebSocket connection within the specified time. The second one is used to reset the read deadline after receiving a heartbeat message.
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.
but pkg.go.dev clearly stated:
"Applications are responsible for ensuring that no more than one goroutine calls the write methods (NextWriter, SetWriteDeadline, WriteMessage, WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and that no more than one goroutine calls the read methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler) concurrently."
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Description
as title
Fixes #3907
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Test Configuration:
Checklist: