How do I debug error ECONNRESET in Node.js?

后端 未结 14 1241
刺人心
刺人心 2020-11-22 04:02

I\'m running an Express.js application using Socket.io for a chat webapp and I get the following error randomly around 5 times during 24h. The node process is wrapped in for

相关标签:
14条回答
  • 2020-11-22 04:14

    You might have guessed it already: it's a connection error.

    "ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection. This is most probably due to one or more application protocol errors. You could look at the API server logs to see if it complains about something.

    But since you are also looking for a way to check the error and potentially debug the problem, you should take a look at "How to debug a socket hang up error in NodeJS?" which was posted at stackoverflow in relation to an alike question.

    Quick and dirty solution for development:

    Use longjohn, you get long stack traces that will contain the async operations.

    Clean and correct solution: Technically, in node, whenever you emit an 'error' event and no one listens to it, it will throw. To make it not throw, put a listener on it and handle it yourself. That way you can log the error with more information.

    To have one listener for a group of calls you can use domains and also catch other errors on runtime. Make sure each async operation related to http(Server/Client) is in different domain context comparing to the other parts of the code, the domain will automatically listen to the error events and will propagate it to it's own handler. So you only listen to that handler and get the error data. You also get more information for free.

    EDIT (2013-07-22)

    As I wrote above:

    "ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection. This is most probably due to one or more application protocol errors. You could look at the API server logs to see if it complains about something.

    What could also be the case: at random times, the other side is overloaded and simply kills the connection as a result. If that's the case, depends on what you're connecting to exactly…

    But one thing's for sure: you indeed have a read error on your TCP connection which causes the exception. You can see that by looking at the error code you posted in your edit, which confirms it.

    0 讨论(0)
  • 2020-11-22 04:14

    Another possible case (but rare) could be if you have server to server communications and have set server.maxConnections to a very low value.

    In node's core lib net.js it will call clientHandle.close() which will also cause error ECONNRESET:

    if (self.maxConnections && self._connections >= self.maxConnections) {
      clientHandle.close(); // causes ECONNRESET on the other end
      return;
    }
    
    0 讨论(0)
  • 2020-11-22 04:15

    Had the same problem today. After some research i found a very useful --abort-on-uncaught-exception node.js option. Not only it provides much more verbose and useful error stack trace, but also saves core file on application crash allowing further debug.

    0 讨论(0)
  • 2020-11-22 04:17

    I solved the problem by simply connecting to a different network. That is one of the possible problems.

    As discussed above, ECONNRESET means that the TCP conversation abruptly closed its end of the connection.

    Your internet connection might be blocking you from connecting to some servers. In my case, I was trying to connect to mLab ( cloud database service that hosts MongoDB databases). And my ISP is blocking it.

    0 讨论(0)
  • 2020-11-22 04:25

    Try adding these options to socket.io:

    const options = { transports: ['websocket'], pingTimeout: 3000, pingInterval: 5000 };
    

    I hope this will help you !

    0 讨论(0)
  • 2020-11-22 04:27

    I had a similar problem where apps started erroring out after an upgrade of Node. I believe this can be traced back to Node release v0.9.10 this item:

    • net: don't suppress ECONNRESET (Ben Noordhuis)

    Previous versions wouldn't error out on interruptions from the client. A break in the connection from the client throws the error ECONNRESET in Node. I believe this is intended functionality for Node, so the fix (at least for me) was to handle the error, which I believe you did in unCaught exceptions. Although I handle it in the net.socket handler.

    You can demonstrate this:

    Make a simple socket server and get Node v0.9.9 and v0.9.10.

    require('net')
        .createServer( function(socket) 
        {
               // no nothing
        })
        .listen(21, function()
         {
               console.log('Socket ON')
        })
    

    Start it up using v0.9.9 and then attempt to FTP to this server. I'm using FTP and port 21 only because I'm on Windows and have an FTP client, but no telnet client handy.

    Then from the client side, just break the connection. (I'm just doing Ctrl-C)

    You should see NO ERROR when using Node v0.9.9, and ERROR when using Node v.0.9.10 and up.

    In production, I use v.0.10. something and it still gives the error. Again, I think this is intended and the solution is to handle the error in your code.

    0 讨论(0)
提交回复
热议问题