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

How to get status code from WebSocketClosedError? #176

Closed
tbenst opened this issue Dec 22, 2021 · 2 comments
Closed

How to get status code from WebSocketClosedError? #176

tbenst opened this issue Dec 22, 2021 · 2 comments

Comments

@tbenst
Copy link

tbenst commented Dec 22, 2021

I'm trying to debug why all my websockets keep crashing at the same time. I'm subscribed to a feed.

I get the following error: Debug: WebSocketClosedError("while read(ws|client) Client side closed socket connection - Performed closing handshake.")

I believe this comes from

throw(WebSocketClosedError("while read(ws|$(server_str)) $(err.message) - Performed closing handshake."))
which in turn is triggered by
""" Read a frame: turn bytes from the websocket into a WebSocketFragment."""
function read_frame(ws::WebSocket)
# Try to read two bytes. There is no guarantee that two bytes are actually allocated.
ab = Array{UInt8}(undef, 2)
if readbytes!(ws.socket, ab) != 2
throw(WebSocketError(1006, "Client side closed socket connection"))
end

It seems like the issue is on the Julia (client) side rather than on the webserver. Any tips for how I might debug? Thank you!

@hustf
Copy link
Collaborator

hustf commented Dec 22, 2021

I can see how this is confusing. We have two subtypes of WebSockets. The 'read' function's argument is a WebSocket of the 'client' type. What that means is ambigious - which side of the connection is a client? Are we reading from our end or from the other end? Enabling more debug output will hopefully set that straight.

We know from the message that WebSockets.jl had WebSocketError 1002 or 1006 (the definition set is shown in the code). Either, the other side did something not according to protocol or the connection was lost. To play nice, our side initiated a separate task which will be trying to close the connection in the proper way during the following TIMEOUT_CLOSEHANDSHAKE seconds.

To get details from @debug statements, the simplest way is from the command line:
Users\f> julia -g 2

If you want full clarity, we have an additional log level, @WSLog. See, ´examples/count_with_logger.jl´.

I suppose you have been calling the 'readguarded' function? Use 'read' instead for full debugging sessions - most closing events are normal timeouts from a browser (i.e. from a client) or from a server, and some don't follow the protocol while closing.

@tbenst
Copy link
Author

tbenst commented Dec 24, 2021

@hustf thank you! That's super helpful. And yes indeed, I've been using readguarded and just switched to read.

It turns out there were two issues:

  1. I was writing malformed messages. This led to Debug: ErrorException("WebSocket{MbedTLS.SSLContext} does not support byte I/O") error

  2. I forgot my heardbeat message was commented out. As such, the connection was unceremoniously dropping, and I got a 1006 error.

issue solved, many thanks!!

@tbenst tbenst closed this as completed Dec 24, 2021
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

No branches or pull requests

2 participants