question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: is 'close' emitted by Websocket when a socket error occurs?

See original GitHub issue

Description

I’ve searched through the source code and documentation and I find this inconclusive.

I have a Webserver that also serves websocket connections in some endpoints. And I need to clear some things up when the connection closes, or it is for some external reason, not available anymore and useless to both the client and the server.

documentation about the error is really not enough.

Also, looking at the source code I can see that when an error happens, the error is emitted and the socket is destroyed. But, is this code really executed when an error happens? Because there is also this, hence the confusion.

Reproducible in:

  • version: 5.2.2
  • Node.js version(s): 12.18.3
  • OS version(s): Linux 31a5635782a1 5.3.0-64-generic #58-Ubuntu SMP Fri Jul 10 19:33:51 UTC 2020 x86_64 Linux

Steps to reproduce:

I cannot reproduce, that’s the problem. I’ve tried.

I’m trying to determine if my application handles some low level Linux socket errors correctly, for example, EPIPE or ECONNRESET. I’m getting these errors in my deploy environment because I have an error report system.

I don’t know if they are related to the real issue: memory leak.

I’ve tried locally to reproduce these errors, by running the application in my PC and forcing some errors in socket level, but I don’t know how to do that.

Anyway, I’m suspicious that these memory leaks happen because I’m not gracefully shutting down/closing/releasing every thing that I initiate when a websocket connection opens.

I am registering websocket.on('close'=>clearEverythingUp) but is it enough, considering some socket errors might happen?

Expected result:

websocket.on(‘close’) to be called when a socket error happens

Actual result:

I’m not sure what happens because it is hard to replicate.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
lpincacommented, Dec 4, 2020

Yes, the 'close' event is always emitted. Do not confuse the server 'close' event with the WebSocket 'close' event.

1reaction
lpincacommented, Dec 7, 2020

Ok. So, in my case, with these specific errors, the ‘close’ event is not triggered, right?

'close' is emitted every time the connection is closed so it is emitted for socket errors like EPIPE and ECONNRESET.

And if so, for what kind of errors does Websocket emit ‘error’ events?

The 'error' event is emitted during the handshake if an error occurs and for protocol errors, for example when receiving an invalid WebSocket frame or a message whose size is larger than the maximum allowed. When an 'error' event is emitted the connection is automatically closed.

No low level socket errors are emitted websocket at all? So can you help me on how to handle these errors?

There is nothing to do. The connection is closed and the 'close' event emitted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Websocket errors when client and server are closed ... - GitHub
This happens because wss.close() destroys the socket on the server and right after that the client tries to send a close frame on...
Read more >
WebSocket: error event - Web APIs | MDN
The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent...
Read more >
finding out the reason for Web Socket disconnect happened
In our Application, data will be continuously flowing for every 1 second through web Socket , and as per our application design ,...
Read more >
error: websocket was closed before the connection ... - You.com
It happens when you call close() on websocket when connection is not established. The code you include in the question does not include...
Read more >
WebSockets - Handling Errors - Tutorialspoint
The onerror event is fired when something wrong occurs between the communications. The event onerror is followed by a connection termination, which is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found