-
Notifications
You must be signed in to change notification settings - Fork 83
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: Add atomic.Bool to ensure wsConnection.Disconnect() is only called once #359
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #359 +/- ##
==========================================
+ Coverage 78.46% 78.86% +0.40%
==========================================
Files 25 25
Lines 2419 2423 +4
==========================================
+ Hits 1898 1911 +13
+ Misses 413 406 -7
+ Partials 108 106 -2 ☔ View full report in Codecov by Sentry. |
@@ -19,6 +20,7 @@ type wsConnection struct { | |||
// For more: https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency | |||
connMutex *sync.Mutex | |||
wsConn *websocket.Conn | |||
closed *atomic.Bool |
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.
Does this need to be a pointer? Seems to be like unnecessary extra allocation.
if c.closed.Load() { | ||
return nil | ||
} | ||
c.closed.Store(true) |
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.
To ensure this happens once, I think the correct way is to use the atomic CompareAndSwap
instead of separate Load/Store which can race.
assert.True(t, atomic.LoadInt32(&connectionCloseCalled) == 0) | ||
|
||
// Close connection from server side | ||
mutex.Lock() |
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.
What does this mutex protect against? Does serverConn
value ever change (except from nil
to the only connection we get)? Can serverConn
be nil here?
require.Equal(t, 1, len(logger.logs)) | ||
require.Contains(t, logger.logs[0], "Errorf: Cannot read a message from WebSocket: read tcp") | ||
require.Contains(t, logger.logs[0], "use of closed network connection") |
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.
Do we care about the log messages produced? It looks fragile to make the tests dependent on the exact formatting of the messages.
Addresses this issue by addding an atomic.Bool so we only close the underlying websocket connection once. This PR also adds the test
TestDisconnectServerWSConnection
to ensure that that error log isn't generated when the connection is closed server-side. I renamedTestDisconnectWSConnection
toTestDisconnectClientWSConnection
since it tests closing the connection from the client-side, though the comments in the test implied it tested disconnecting from the server.Perhaps it would be better to simply ignore the error when we close the connection twice? I'm unsure if there are error conditions where we'd want to be able to try to close the connection again.