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

Handle abnormal WebSocket close on client (fixes #114) #140

Merged
merged 5 commits into from
Nov 28, 2024

Conversation

MaxMaeder
Copy link
Contributor

Modified the WebSocket Stream to insert the control frame closed: 1006 when a client tries to read from a stream after it's been abnormally closed by the server.

In my implementation, 'abnormally closed' means the underlying TCP stream was closed without the WebSocket-level closing handshake (we get an io.EOF). I admit I am newer to working with WebSockets on this low of a level, so there may be other cases we need to check.

Previously, tests (and presumably library users) would expect to receive this io.EOF error from methods like nextFrame() after the connection was abnormally closed, so I maintain this behavior while also returning the aforementioned control frame.

I've also added tests to check correct client behavior on abnormal server close for both synchronous and asynchronous stream methods.

Separately, I recently applied for Talos’s backend internship position. If this is a helpful fix maybe you can look at my application :) It should be under Maxim Maeder / [email protected]

@MaxMaeder MaxMaeder requested a review from sergiu128 as a code owner November 22, 2024 21:55
Copy link
Collaborator

@sergiu128 sergiu128 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 the PR! The change looks good and is correct! I've left some comments - once resolved we can merge and release a new version. About your application - I've flagged it with the team!

codec/websocket/stream.go Outdated Show resolved Hide resolved
codec/websocket/stream.go Outdated Show resolved Hide resolved
codec/websocket/stream_test.go Outdated Show resolved Hide resolved
codec/websocket/stream_test.go Show resolved Hide resolved
codec/websocket/stream_test.go Show resolved Hide resolved
codec/websocket/stream_test.go Outdated Show resolved Hide resolved
@MaxMaeder
Copy link
Contributor Author

Thanks for the review, glad to hear my changes were correct for the most part!

I've addressed the requested changes:

  • Cleaned up how I'm constructing the Frame, thanks for pointing out how Go automatically coerces types!
  • Added check to make sure WebSocket is StateActive before starting the test
  • Replaced t.Fail and assertState in my tests with assert.* methods
  • Updated srv.Accept to accept an optional callback called on port assignment by making it a variadic function
    • Let me know if this is bad practice, it kind of seems like a 'hack' to get around Go's lack of function overloading
    • Also updated my tests to use this callback so the server port is not hard-coded

Happy to knock out #66 too if how I implemented dynamic port assignment in my tests looks good, although it looks like port assignments are hardcoded in tests other than stream_test as well, so I'd likely need to make other changes too.

@MaxMaeder MaxMaeder requested a review from sergiu128 November 27, 2024 23:36
@sergiu128
Copy link
Collaborator

This all looks really good! Thank you! I made some notes in #66 , let's continue the discussion there.

@sergiu128 sergiu128 merged commit 99dacd3 into talostrading:master Nov 28, 2024
2 checks passed
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