uWebSockets.js icon indicating copy to clipboard operation
uWebSockets.js copied to clipboard

Receving 1006 CLOSE_ABNORMAL on random basis

Open sailingwithsandeep opened this issue 1 year ago • 14 comments

Hello folks,

We have been using this service in many projects now(And we are quite happy with it), And each one is getting is same random disconnection issues.

We tried with latest version v20.43.0 also.

Still no luck!!

Node Version : v20.11.1

sailingwithsandeep avatar Jun 13 '24 12:06 sailingwithsandeep

When you get 1006 it comes from the library and there is a reason string as second argument

uNetworkingAB avatar Jun 13 '24 15:06 uNetworkingAB

Close event has 3 arguments: ws, code, reason

uNetworkingAB avatar Jun 13 '24 16:06 uNetworkingAB

@uNetworkingAB

I am getting empty message!!!

Here is code snippet:

 const msg = Buffer.from(message).toString('utf8');
    log.warn(`[WS ${socket_id} ${userId} ${sUsername} ${nsp}] Disconnected with code: ${reason} ${msg ? `and message: ${msg}` : ''}`);

sailingwithsandeep avatar Jun 14 '24 05:06 sailingwithsandeep

The callback has ws, code, reason why do you have reason and message and no code ? or you renamed to confusing names ?

e3dio avatar Jun 14 '24 16:06 e3dio

The signature of the callback is close?: (ws: WebSocket<UserData>, code: number, message: ArrayBuffer) => void; with 3 args ws, code, message

but for code 1006 we got empty string value for message post conversion using Buffer.from(message).toString('utf8')

so whenever we got empty string we mapped code using this to identify meaning of the code and it comes down to "Abnormal Closure" image

anuj-kr-jha avatar Jun 15 '24 23:06 anuj-kr-jha

There are only a few places where 1006 is given without a reason message and those are all protocol errors. Someone is sending you invalid websocket messages

uNetworkingAB avatar Jun 17 '24 05:06 uNetworkingAB

Having the same issue here! It closes without a reason

ygorcarmo avatar Jun 19 '24 11:06 ygorcarmo

Connection has been closed. Message . Code 1006 
close(ws, code, message) {
  const data = Buffer.from(message).toString()
      console.log(`Connection has been closed. Message ${data}. Code ${code} `)
}

ygorcarmo avatar Jun 19 '24 11:06 ygorcarmo

If you expose your service on the internet, all kinds of invalid WebSocket clients can connect. When there is no reason given, it most likely comes from parsing a non-standard WebSocket close message here:

https://github.com/uNetworking/uWebSockets/blob/01058d90e9d092c2d6551956d7e0785e0e94ff21/src/WebSocketProtocol.h#L173

This could be fixed to attach the message "Protocol error" or similar. But for now, just assume that no reason means protocol error, or "various error".

Any client that mistreats the standard will have its close frame replaced with 1006

uNetworkingAB avatar Jun 19 '24 11:06 uNetworkingAB

That thing should ideally give you "Invalid close frame received" as reason string

uNetworkingAB avatar Jun 19 '24 11:06 uNetworkingAB

It's fixed in this commit https://github.com/uNetworking/uWebSockets/commit/7eb5d561b44d159ef6630be73f0300e996f53921

uNetworkingAB avatar Jun 19 '24 11:06 uNetworkingAB

It seems to be fixed after I moved all async operations from the on open function to the on upgrade. Would that make any sense to you?

ygorcarmo avatar Jun 19 '24 12:06 ygorcarmo

No that makes no sense

uNetworkingAB avatar Jun 25 '24 10:06 uNetworkingAB

I forgot to update this! Soz @uNetworkingAB

The problem wasn't solved after moving stuff around. My workaround at this stage is making the front end try to reconnect to the WebSocket every time the WebSocket connection closes with a 1006 code and no message.

ygorcarmo avatar Jun 26 '24 07:06 ygorcarmo

We're seeing this too after upgrading from 20.33.0 to 20.62.0.

dwickr avatar Aug 02 '24 13:08 dwickr

We need a new release of uWS.js with latest uWS to get that newly added reason message:

Make use of ERR_INVALID_CLOSE_PAYLOAD to better explain such closures.

With that, it should be obvious what the reason is (lol; pun not intended)

uNetworkingAB avatar Aug 02 '24 14:08 uNetworkingAB

Probably good to have a basic smoke test for this as well.

uNetworkingAB avatar Aug 02 '24 14:08 uNetworkingAB

https://github.com/uNetworking/uWebSockets/commit/57f26629f9895200bdc0d8cca1a73410db8e1a9f adds even more reasons for 1006. One very plausible explanation is simply receiving TCP FIN. If you receive TCP FIN before a WebSocket close frame, it will just give you 1006. I guess that can be very common for invalid clients to act.

uNetworkingAB avatar Aug 02 '24 15:08 uNetworkingAB

If you run this https://github.com/uNetworking/uWebSockets.js/blob/master/tests/smoke.js with the "nightly" builds it will pass like so:

Listening to port 9001
A WebSocket connected!
Client connected to server
WebSocket closed with code: 1006 and reason: Received invalid WebSocket frame
Test passed: Invalid WebSocket frame
Client WebSocket closed with code: 1006 and reason: <Buffer >
A WebSocket connected!
Client connected to server
WebSocket closed with code: 1006 and reason: Received TCP FIN before WebSocket close frame
Test passed: TCP FIN before WebSocket close frame
Client WebSocket closed with code: 1006 and reason: <Buffer >
All tests passed.

Yes, the test is written by ChatGPT 😄

uNetworkingAB avatar Aug 02 '24 17:08 uNetworkingAB

GitHub actions is down for macos today. Will release when it's functioning again

uNetworkingAB avatar Aug 02 '24 19:08 uNetworkingAB

You can already take the latest Linux binaries today, I have issues with GitHub Actions macOS migration so release will take long time

uNetworkingAB avatar Aug 06 '24 10:08 uNetworkingAB

Thanks! @uNetworkingAB

ygorcarmo avatar Aug 06 '24 23:08 ygorcarmo

You should be able to get pretty detailed stats now and see exactly what is wrong. Could you share the main reason for 1006 when you know?

uNetworkingAB avatar Aug 06 '24 23:08 uNetworkingAB

For sure!!

ygorcarmo avatar Aug 07 '24 04:08 ygorcarmo

@uNetworkingAB here is the error I got today: Connection has been closed. Message Received TCP FIN before WebSocket close frame. Code 1006

ygorcarmo avatar Aug 19 '24 09:08 ygorcarmo

I get those when phones go to sleep / locks and connection closes, pretty normal but nice to have the new detailed reason messages Received TCP FIN before WebSocket close frame

e3dio avatar Aug 19 '24 10:08 e3dio

Perfect. That's the new message. It makes sense.

From the perspective of the server, this is according to spec (you're not allowed to send FIN before WebSocket close frame, so 1006 is correct and really the only thing you can get because there is no WebSocket close frame lol).

From the perspective of the client, it looks like nothing happened but the OS will send a FIN without asking the client, and this breaks the WebSocket protocol. So it's an "issue" of the client, it needs to respond to OS events of going down. But in practice, nobody cares, this is not really a problem. Just treat it as normal closure IMO

uNetworkingAB avatar Aug 19 '24 11:08 uNetworkingAB