Skip to content

fix: Add atomic.Bool to ensure wsConnection.Disconnect() is only called once #359

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

Open
wants to merge 4 commits 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 80.24%. Comparing base (d27bb62) to head (c9ca144).
Report is 33 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #359      +/-   ##
==========================================
+ Coverage   78.46%   80.24%   +1.77%     
==========================================
  Files          25       26       +1     
  Lines        2419     2500      +81     
==========================================
+ Hits         1898     2006     +108     
+ Misses        413      383      -30     
- Partials      108      111       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree we should avoid fragility. The original motivation for this change was to deal with superfluous error logs produced by the server-side Disconnect() calls. Open to suggestions for a better criteria for to test for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps as a compromise: in testLogger store error logs separate from debug logs and here only assert that there is an error log produced, but don't check the error message itself. This should be fairly stable since we always expect exactly one error log and don't really care about the message itself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good compromise. All set.

@tigrannajaryan
Copy link
Member

@shazlehu Is this still in progress?

@shazlehu
Copy link
Author

@tigrannajaryan I'll make a push to get this over the finish line in the next week

@shazlehu shazlehu requested a review from tigrannajaryan June 16, 2025 19:21
Copy link
Member

@tigrannajaryan tigrannajaryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @shazlehu

Mostly LGTM, just 2 minor comments.

}

var _ types.Connection = (*wsConnection)(nil)

func (c wsConnection) Connection() net.Conn {
func newWSConnection(wsConn *websocket.Conn) types.Connection {
return &wsConnection{wsConn: wsConn, connMutex: sync.Mutex{}, 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.

I think zero-initialization for connMutex and closed is fine, e.g. this should work the same way and is less verbose:

Suggested change
return &wsConnection{wsConn: wsConn, connMutex: sync.Mutex{}, closed: atomic.Bool{}}
return &wsConnection{wsConn: wsConn}

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.

Perhaps as a compromise: in testLogger store error logs separate from debug logs and here only assert that there is an error log produced, but don't check the error message itself. This should be fairly stable since we always expect exactly one error log and don't really care about the message itself.

@shazlehu shazlehu force-pushed the fix/extra-error-log branch from 4a52657 to b30f020 Compare July 11, 2025 15:41
@shazlehu shazlehu requested a review from tigrannajaryan July 11, 2025 15:43
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