sitegui / nodejs-websocket

A node.js module for websocket server and client
MIT License
736 stars 155 forks source link

Catching ECONNRESET Exception #26

Closed isaackwan closed 8 years ago

isaackwan commented 8 years ago

Hello,

I am on OS X 10.11 and running node v5.3.0. I am running the basic demo app on README.

Whenever a connection is closed, an error of ECONNRESET is thrown and unhandled. I wonder if it is possible to fix the crash?

Connection closed
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at exports._errnoException (util.js:855:11)
    at TCP.onread (net.js:544:26)

I am sorry that because I am rather new to node, I am not sure if I can debug this myself. Thanks in advance!

sitegui commented 8 years ago

Hi @isaackwan and welcome to Node.js

The ECONNRESET happens when the "connection was forcibly closed by a peer". The 'error' event is emitted in the connection instance and since there is no listener registered for it, Node.js throws an exception and crashes. This is intended and by design of Node.js.

If you want to ignore this error, you can add a listener to 'error' event on every new connection, like this:

conn.on('error', function (err) {
    if (err.code !== 'ECONNRESET') {
        // Ignore ECONNRESET and re throw anything else
        throw err
    }
})

For some guidance on how to debug this kind of situation, you may like to look at http://stackoverflow.com/questions/10814481/how-to-debug-a-socket-hang-up-error-in-nodejs/11542134#11542134

I'm closing this now, but feel free to reopen if it does not work for you ;) Best regards