read ECONNRESET error on disconnect (ws 3.3.3)
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Description
Upon updating to ws 3.3.3, when a client disconnects the following exception is thrown:
events.js:183
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at _errnoException (util.js:1024:11)
at TCP.onread (net.js:615:25)
Code works fine in previous version.
Reproducible in:
version: 3.3.3 Node.js version(s): v8.9.1 OS version(s): Windows 10 Pro, Fall update
Steps to reproduce:
- Create a websocket server
- Connect to the websocket server via Chrome (`new WebSocket('ws://localhost:3000'))
- Disconnect from the websocket server (via refreshing the page in Chrome)
Expected result:
The disconnect is handled gracefully, with the 'close' event being called.
Actual result:
An exception is thrown, which crashes the app:
events.js:183
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at _errnoException (util.js:1024:11)
at TCP.onread (net.js:615:25)
Issue is caused by lack of error listener on the socket, and can be fixed like so:
ws.on('error', () => console.log('errored'));
This isn't documented in the README.md, and seems to be a pretty big breaking change.
You need to always add a listener for the 'error' event as there are other errors that can be emitted.
net.Socket errors are no longer swallowed in version 3.3.3.
his is the case for the latest Google browsers
@lpinca So can we add it to README? If it's a required thing to do, why is it absent there?
Edit. This is totally a good change, but it should be at least documented properly.
It is documented, see https://github.com/websockets/ws/blob/180b388a2ed83fddd667d88970a943d0f47d32ec/doc/ws.md#event-error-1
@lpinca You document that that event is where any errors from underlying net.Socket are forwarded to, but not the fact that a simple client disconnection will cause such an exception.
To me, because you have a 'close' event, I'd assume that exceptions caused as a result of a client disconnecting would be handled by this library, since it's something that the library seems to be able to detect and act on.
I think is a reasonable assumption to make, given that clients disconnecting is a perfectly normal event for a socket server.
We've been running into this issue as well, but it's puzzling.
Chrome 62 and below do not cause an error using these steps, and neither do any other browsers that I've tested (Firefox 57, Firefox Mobile (57), Chrome Mobile (62)).
Further, with Wireshark I wasn't able to observe any significant difference in the close frames. Both Firefox and Chrome send the same 1001 closing code ("Going away").
With the error handler, it appears when closing a Chrome 63 tab that the error handler and the close handler are called at roughly the same time, but I haven't yet confirmed this.
@G-Rath
but not the fact that a simple client disconnection will cause such an exception.
Yes because it shouldn't, the issue seems to happen only on Chrome 63.
To me, because you have a 'close' event, I'd assume that exceptions caused as a result of a client disconnecting would be handled by this library, since it's something that the library seems to be able to detect and act on.
I think is a reasonable assumption to make, given that clients disconnecting is a perfectly normal event for a socket server.
Swallowing errors is never a good idea. Developers can ignore errors but they may want to know if the connection was closed cleanly or not.
@quigleyj97
I didn't check but maybe latest Chrome sends the close frame and abruptly closes the connection immediately after that.
@lpinca That all makes far more sense; that's a mistake on my part - I didn't test it with another browser, and so assumed that that was something that commonly happens with client disconnections.
~~I can no longer reproduce the issue on Chrome 63.0.3239.108 can anyone confirm?~~
Nevermind, here is a test case which doesn't use ws:
const assert = require('assert');
const crypto = require('crypto');
const http = require('http');
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
const data = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
(function () {
var ws = new WebSocket('ws://localhost:8080');
ws.onopen = function () {
console.log('open');
};
})();
</script>
</body>
</html>`;
const server = http.createServer();
server.on('request', (req, res) => {
res.setHeader('Content-Type', 'text/html');
res.end(data);
});
server.on('upgrade', (req, socket) => {
const key = crypto.createHash('sha1')
.update(req.headers['sec-websocket-key'] + GUID)
.digest('base64');
socket.on('error', console.error);
socket.on('data', (data) => {
assert.ok(data.slice(0, 2).equals(Buffer.from([0x88, 0x82])));
socket.write(Buffer.from([0x88, 0x00]));
});
socket.write([
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Accept: ${key}`,
'\r\n'
].join('\r\n'));
});
server.listen(8080, () => console.log('Listening on *:8080'));
I'm in the same boat.
ws.on('error', () => console.log('errored'));
this doesn't solve the problem.
@ProgrammingLife it should.
In https://github.com/websockets/ws/issues/1266#issue-285162876 you added the listener on the server so make sure you are adding it to the WebSocket instance instead.
I'm in the same boat.
ws.on('error', () => console.log('errored'));this doesn't solve the problem.
I can confirm this statement, when the handler is applied to the server. No more crashes when applied to each connection individually.
These errors occur quite often on Chrome V 63.0.3239.84 (=latest for my distribution).
FF 57.0.3 doesn't produce this error at all. Same with Edge on Windows clients.
Node v8.9.3 ws 3.3.3 ubuntu1~16.04.5 Linux 4.4.0-21-generic (x86_64)
According to https://bugs.chromium.org/p/chromium/issues/detail?id=798194#c6 this works as intended.
What we can do here is adding a default 'error' listener which is a noop, so people are not forced to add one. Users can still get errors by adding their own 'error' listener(s).
Thoughts?
Another possible workaround for Chrome is calling close() from a listener of the 'beforeunload' event:
window.addEventListener('beforeunload', function () {
ws.close();
});
I like this proposal https://github.com/websockets/ws/issues/1256#issuecomment-355536426
The error looks like a bug in the library since it started to happen recently.
@endel the error is not a bug in the library and https://github.com/websockets/ws/issues/1256#issuecomment-354305677 is the proof. The fact that it only happens on Chrome is another proof.
You get the error only on ws@>=3.3.3 because it is the first version that does not swallow the net.Socket errors.
Hi I had the same issue, According to websocket specification, it is necessary to send a close frame before disconnecting from the server. That's why after properly closing the socket with sending the close frame this error didn't occur. as @lpinca mentioned.
window.addEventListener('beforeunload', function () {
ws.close();
});
So I guess its fair to emit an error that point. Make sure you close the websocket from the client side before disconnecting, this could happen due to closing the browser or refreshing
@jkasun well all browsers sends a close frame when you refresh the page if you didn't already close the WebSocket. The problem is that Chrome closes the TCP socket while the server response is on the wire.
To be honest I don't know why it doesn't do the same when the WebSocket is closed from a listener of the 'beforeunload' event. Maybe it does but the small timing difference is sufficient (at least for the above example) to prevent the issue from happening.