Question: is 'close' emitted by Websocket when a socket error occurs?
See original GitHub issueDescription
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:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Yes, the
'close'
event is always emitted. Do not confuse the server'close'
event with theWebSocket
'close'
event.'close'
is emitted every time the connection is closed so it is emitted for socket errors likeEPIPE
andECONNRESET
.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.There is nothing to do. The connection is closed and the
'close'
event emitted.