Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shazlehu
Copy link

@shazlehu shazlehu commented Mar 4, 2025

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 renamed TestDisconnectWSConnection to TestDisconnectClientWSConnection 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.

@shazlehu shazlehu requested a review from a team as a code owner March 4, 2025 00:03
Copy link

codecov bot commented Mar 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.86%. Comparing base (d27bb62) to head (5d765eb).

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.
📢 Have feedback on the report? Share it here.

@@ -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
Copy link
Member

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.

Comment on lines +40 to +43
if c.closed.Load() {
return nil
}
c.closed.Store(true)
Copy link
Member

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()
Copy link
Member

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?

Comment on lines +364 to +366
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")
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants