Not sure when this comes up but just wondering if you have changed any code that might cause these to occur more frequently.

I used to run ws without
socket.on('error'
Now I have to have it or it crashes the application.
[email protected]
[email protected]
macOS Sierra
Do you have a test case to reproduce the issue or is this something that happens randomly on a production app?
Randomly on socket close while on development. Haven't seen it yet on production.
Can you try to upgrade to latest v6.x and see if the issue persists? Does you app interact with the _socket property directly?
No, to the second question. I'll get back to the first when I get time.
Just upgraded to [email protected] today and started seeing this (Node v6.10.0, Debian 7). I have haproxy set up to do websocket health checks so it does a quick connect / disconnect every second or so. I detect these types of connections and handle them differently, which includes not assigning handlers including socket errors (hence the crash).
What I was doing was detecting these types of connections and manually running a close() on them. Right before this I checked readyState and it was set to open. I think maybe what happened was the connection was closed / closing in some way by the time close() actually ran.
All I had to do to fix this was to setTimeout for a second later, check readyState, and if the connection is still open, I run a close(). Before I was running [email protected] and had to check readyState before a send as well otherwise I'd get a crash.
Just as I posted this, I decided to try running close() without a readyState check a second after connection, and it worked without a crash. I then tried 100ms later and it worked as well. Seems there is a timing issue such that if I call close() too soon after connection, I get the "write EPIPE" error and a crash.
@brenc thanks. Are you able to create a reproducible test case?
Ok I think I've found a possible explanation for this. Here is a test case which doesn't use ws but reproduces what I think happens:
'use strict';
const arr = [];
function callback(err) {
if (err) arr.push({ from: 'callback', err });
}
process.on('exit', () => console.error(arr));
process.stdout.on('error', (err) => arr.push({ from: 'listener', err }));
for (var i = 0; i < 100; i++) {
process.stdout.write('something\n', callback);
}
Running the above example with
node index.js | head
prints
something
something
something
something
something
something
something
something
something
[ { from: 'callback',
err:
{ Error: write EPIPE
at exports._errnoException (util.js:1033:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:816:14) code: 'EPIPE', errno: 'EPIPE', syscall: 'write' } },
{ from: 'listener',
err:
{ Error: write EPIPE
at exports._errnoException (util.js:1033:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:816:14) code: 'EPIPE', errno: 'EPIPE', syscall: 'write' } } ]
so the error listener on the socket is called after the write callback. When the callback is invoked, the error is emitted on the WebSocket instance and if there isn't a listener the process crashes.
I don't see this as a big issue, we can avoid re-emitting the error but I think it is actually a valuable info.
An error listener on the WebSocket instance is sufficient and should always be added anyway as there are a lot of more errors which can be emitted and make the process crash if they are not handled.
Makes sense. I definitely think that an error listener should always be added. I might have to add one even for these health check connections.
Closing, please comment or reopen if needed.
I get this error too when I am writing my multiplayer game. I use the following code to send a message.
try {
ws.send(msg, function(error) {
if(error == undefined)
return;
ws.close();
logger.debug("Async error:"+error);
});
} catch (e) {
logger.debug("Sync error: " + e);
ws.close();
}
But I still get the error when I am playing the game normally.
I have read the source code of Sender.js. I think the problem is in the sendFrame method:
https://github.com/websockets/ws/blob/master/lib/Sender.js#L380
As you mentioned we need to add an error listener to the websocket instance. Do I need to do that for all ws.send? Is it so often that I will get an error during websocket connection? I almost always get an connection error when I play my game for a while.
Following your advice I add an error listener on the virtual connections I make on the server. But I still get the error Async error:Error: write EPIPE following by Async error:Error: not opened, only this time the server doesn't crash.
The problem though, is that the client is crashing. Anything I can do to fix this?
Most helpful comment
I get this error too when I am writing my multiplayer game. I use the following code to send a message.
But I still get the error when I am playing the game normally.
I have read the source code of
Sender.js. I think the problem is in thesendFramemethod:https://github.com/websockets/ws/blob/master/lib/Sender.js#L380
As you mentioned we need to add an error listener to the websocket instance. Do I need to do that for all
ws.send? Is it so often that I will get an error during websocket connection? I almost always get an connection error when I play my game for a while.Following your advice I add an error listener on the virtual connections I make on the server. But I still get the error
Async error:Error: write EPIPEfollowing byAsync error:Error: not opened, only this time the server doesn't crash.The problem though, is that the client is crashing. Anything I can do to fix this?