0

I keep getting two exceptions on my server that close the process even though the entire script is wrapped in a try catch block. The exceptions are:

events.js:160
throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at exports._errnoException (util.js:1022:11)
    at WriteWrap.afterWrite [as oncomplete] (net.js:804:14)

and

Error: invalid opcode: 7
    at Receiver.start (/home/mysite/public_html/node_modules/ws/lib/Receiver.js:211:18)
    at Receiver.add (/home/mysite/public_html/node_modules/ws/lib/Receiver.js:134:14)
    at Socket._ultron.on (/home/mysite/public_html/node_modules/ws/lib/WebSocket.js:139:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)

From the sockets lib.

Does anyone know what is causing this and how to prevent it?

edit:

An excerpt:

try
{
    wss.broadcast = function broadcast(data) {
      wss.clients.forEach(function (client) {
        if (client.readyState == WebSocket.OPEN) {
          client.send(data);
        }
      });
    };


    wss.on("connection", function (ws)
    {
        if(blacklist.indexOf(GetClientIP(ws)) != -1)
        {
            console.log("Kicking banned");
            ws.close();
            return;
        }

        clientCount++;

        wss.broadcast(welcomeMessage);


        ws.on("message", function (message)
        {
            ProcessMessage(message);
        });

        ws.on("close", function ()
        {
            clientCount--;

            wss.broadcast(goodbyeMessage);
        });
    });
}
catch(exxx)
{
    console.log("Caught a exception);

    console.log(exxx);
}
6
  • can you show your code ? Commented Apr 3, 2017 at 22:05
  • 1
    @AymanElTemsahi The entire code is very big, but this is the gist of the connection code. The exception isn't caught in the try catch clause at all. Commented Apr 3, 2017 at 22:22
  • What external module does wss come from? It appears to me that you are missing some error handling for socket write errors. A high level try/catch will not catch asynchronous errors from socket writes. You will have to catch those locally, according to the doc for whatever module wss and ws comes from. Commented Apr 3, 2017 at 22:51
  • @jfriend00 Its from the ws library npmjs.com/package/npmdoc-ws Commented Apr 3, 2017 at 23:51
  • Well, you picked a module that appears to have no decent documentation on how you are supposed to handle errors. If you really want to know, you will have to study the source code for the module in detail and figure out what it does with errors. This is all I can find github.com/websockets/ws#error-handling-best-practices and I'm not even sure that's the right module and I'm not sure that is the type of error you are experiencing. Commented Apr 4, 2017 at 0:27

1 Answer 1

1

Attach an error handler to your websocket to catch errors:

wss.on("connection", function (ws) {
  ...

  ws.on('error', function(err) {
    console.log('Websocket error!: ' + err);
  });

  ...
});

This prevents Node.js from crashing when you get an error like EPIPE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.