相关文章推荐
爱看球的伤疤  ·  Jetlinks - ...·  4 天前    · 
爱看球的伤疤  ·  from ...·  3 月前    · 
爱看球的伤疤  ·  vue.js - vuejs plugin ...·  7 月前    · 
爱看球的伤疤  ·  javax.net.ssl.SSLHands ...·  9 月前    · 
爱看球的伤疤  ·  Class ...·  10 月前    · 
独立的眼镜  ·  如何连接Babelfish for RDS ...·  58 分钟前    · 
发财的蛋挞  ·  Microsoft Azure Data ...·  58 分钟前    · 
冷冷的投影仪  ·  Secure an ASP.NET ...·  1小时前    · 
不羁的生姜  ·  PSPSDK 开发的时候出现 ...·  1小时前    · 
儒雅的投影仪  ·  Perl 包和模块 | ·  3 小时前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 forever and it restarts itself immediately.

The problem is that restarting Express kicks my users out of their rooms and nobody wants that.

The web server is proxied by HAProxy. There are no socket stability issues, just using websockets and flashsockets transports. I cannot reproduce this on purpose.

This is the error with Node v0.10.11 :

    events.js:72
            throw er; // Unhandled 'error' event
    Error: read ECONNRESET     //alternatively it s a 'write'
        at errnoException (net.js:900:11)
        at TCP.onread (net.js:555:19)
    error: Forever detected script exited with code: 8
    error: Forever restarting script for 2 time

EDIT (2013-07-22)

Added both socket.io client error handler and the uncaught exception handler. Seems that this one catches the error:

    process.on('uncaughtException', function (err) {
      console.error(err.stack);
      console.log("Node NOT Exiting...");

So I suspect it's not a Socket.io issue but an HTTP request to another server that I do or a MySQL/Redis connection. The problem is that the error stack doesn't help me identify my code issue. Here is the log output:

    Error: read ECONNRESET
        at errnoException (net.js:900:11)
        at TCP.onread (net.js:555:19)

How do I know what causes this? How do I get more out of the error?

Ok, not very verbose but here's the stacktrace with Longjohn:

    Exception caught: Error ECONNRESET
    { [Error: read ECONNRESET]
      code: 'ECONNRESET',
      errno: 'ECONNRESET',
      syscall: 'read',
      __cached_trace__:
       [ { receiver: [Object],
           fun: [Function: errnoException],
           pos: 22930 },
         { receiver: [Object], fun: [Function: onread], pos: 14545 },
         { receiver: [Object],
           fun: [Function: fireErrorCallbacks],
           pos: 11672 },
         { receiver: [Object], fun: [Function], pos: 12329 },
         { receiver: [Object], fun: [Function: onread], pos: 14536 } ],
      __previous__:
       { [Error]
         id: 1061835,
         location: 'fireErrorCallbacks (net.js:439)',
         __location__: 'process.nextTick',
         __previous__: null,
         __trace_count__: 1,
         __cached_trace__: [ [Object], [Object], [Object] ] } }

Here I serve the flash socket policy file:

    net = require("net")
    net.createServer( (socket) =>
      socket.write("<?xml version=\"1.0\"?>\n")
      socket.write("<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\n")
      socket.write("<cross-domain-policy>\n")
      socket.write("<allow-access-from domain=\"*\" to-ports=\"*\"/>\n")
      socket.write("</cross-domain-policy>\n")
      socket.end()
    ).listen(843)

Can this be the cause?

@GottZ maybe this can help (spoke to someone working within node js) gist.github.com/samsonradu/1b0c6feb438f5a53e30e. I ll deploy the socket.error handler today and let you know. – Samson Jun 25, 2013 at 7:45 @Gottz the socket.error handles doesn t help, but process.on('uncaughtException') catches the error. Here is the console.log of the error: { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } – Samson Jul 3, 2013 at 20:37 ECONNRESET could be from network problem. As you know it is impossible to catch all the exceptions when testing. Some will show up on your production server. You will have to make your server robust. You can handle the session deletion by using Redis as storage. It makes your sessions persist even after your node server goes down. – user568109 Jul 8, 2013 at 5:32 You have at-least one TCP socket listening that does not have the handler set. So now it's time to check where that one is :D – Moss Jul 10, 2013 at 12:34

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 its 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.

It doesn't have to mean 'abruptly closed'. It usually results from writing to a connection which the peer had already closed normally. That will cause it to issue an RST. – user207421 Apr 6, 2014 at 23:50 @EJP There was a good reason why I wrote “abruptly”. The error (not warning) states the connection was reset by peer. An existing connection was forcibly closed by the remote peer. A forced close is abrupt since unexpected! (This normally results if peer application on remote machine is suddenly stopped, machine is rebooted, or peer application used a "hard close" on the remote socket. This error may also result if a connection was broken due to "keep-alive" activity detecting a failure while one or more operations are in progress… these operations and subsequent operations will fail.) – e-sushi Apr 7, 2014 at 9:12 I get this error thrown when I batch send around 100 API calls near concurrently from the browser (Chrome) for testing. I imagine that Chrome must then become overloaded and kill some of the connections... @Samson - what is wrong with processing each request in its own domain and catching domain errors without restarting the server? – supershnee Jul 10, 2014 at 8:12 @supershnee You should almost always restart your server after an uncaught exception since your data, application, and node.js itself is in an unknown state. Continuing after an exception puts your data a risk. If you want to find out more, check out Node's docs on process or Node's docs on domains. – c1moore Aug 31, 2015 at 0:10

A simple tcp server I had for serving the flash policy file was causing this. I can now catch the error using a handler:

# serving the flash policy file
net = require("net")
net.createServer((socket) =>
  //just added
  socket.on("error", (err) =>
    console.log("Caught flash policy server socket error: ")
    console.log(err.stack)
  socket.write("<?xml version=\"1.0\"?>\n")
  socket.write("<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\n")
  socket.write("<cross-domain-policy>\n")
  socket.write("<allow-access-from domain=\"*\" to-ports=\"*\"/>\n")
  socket.write("</cross-domain-policy>\n")
  socket.end()
).listen(843)
                Is there anything wrong with the code? Should I have checked if the socket is writable before writing?
– Samson
                Jul 23, 2013 at 8:38
                Doh, didn't see that you already found the solution before I posted pretty much the same thing :) As to your question though, even if you check that the socket is writable, it may not be when you write to it microseconds later and would still throw an error, so this is "the way" to be sure.
– Joachim Isaksson
                Jul 23, 2013 at 9:10
                ok, and is there a safe way out if this? like socket.close() inside the error handler? because i think my CPU load is increasing after these errors (not sure)
– Samson
                Jul 23, 2013 at 9:49
                I've always called socket.destroy() in the error handler to make sure. Sadly I can't find documentation whether it is required, but it does not emit an error to do so.
– Joachim Isaksson
                Jul 23, 2013 at 10:03

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.

    Thanks, I nailed it myself! It s important not to let errors propagate to uncaughtException because it renders the whole app unstable. E.g. after catching around 10 ECONNRESET errors the server sometimes became unresponsive (just froze and didn t handle any connections) – Samson Jul 23, 2013 at 8:57 Also knew about the node version change that didn t suppress the error any more, but seeing so many issues showing up and being solved each version I d rather go for the latest one. I m using V0.10.13 now btw – Samson Jul 23, 2013 at 8:59 weird that a new answer to this old question should pop up as I'm looking -- but this is great, thanks – Semicolon Jun 29, 2015 at 18:41

    I also get ECONNRESET error during my development, the way I solve it is by not using nodemon to start my server, just use "node server.js" to start my server fixed my problem.

    It's weird, but it worked for me, now I never see the ECONNRESET error again.

    Any idea how you came up with this solution? You just randomly tried it. This helped me too. – Riza Khan Sep 5, 2020 at 22:35 This isn't a solution but rather a quickfix which will breaks things without throwing an error. – Nishant Ghodke Feb 12, 2018 at 6:52 I believe for me the problem was it timing out after a period of 5 minutes or so, would it stilll be a problem? – imatwork Jul 23, 2020 at 20:39

    Yes, your serving of the policy file can definitely cause the crash.

    To repeat, just add a delay to your code:

    net.createServer( function(socket) 
        for (i=0; i<1000000000; i++) ;
        socket.write("<?xml version=\"1.0\"?>\n");
    

    … and use telnet to connect to the port. If you disconnect telnet before the delay has expired, you'll get a crash (uncaught exception) when socket.write throws an error.

    To avoid the crash here, just add an error handler before reading/writing the socket:

    net.createServer(function(socket)
        for(i=0; i<1000000000; i++);
        socket.on('error', function(error) { console.error("error", error); });
        socket.write("<?xml version=\"1.0\"?>\n");
    

    When you try the above disconnect, you'll just get a log message instead of a crash.

    And when you're done, remember to remove the delay.

    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;
                    Great call, but maxConnections default value is Infinity. This would be only the case (as you said) if you have explicitly overridden that value.
    – Gajus
                    Jul 15, 2019 at 8:12
    

    ECONNRESET occurs when the server side closes the TCP connection and your request to the server is not fulfilled. The server responds with the message that the connection, you are referring to a invalid connection.

    Why the server sends a request with invalid connection?

    Suppose you have enabled a keep-alive connection between client and server. The keep-alive timeout is configured to 15 seconds. This means that if keep-alive is idle for 15 seconds, it will send connection close request. So after 15 seconds, server tells the client to close the connection. BUT, when server is sending this request, client is sending a new request which is already on flight to the server end. Since this connection is invalid now, server will reject with ECONNRESET error. So the problem occurs due to fewer requests to the server end. So please disable keep-alive and it will work fine.

    Can this happen the other way around? I mean, the client closed the connection but the server is trying to push data using the same connection? In this scenario do we get the same error on the server side log? – vikas kv May 17, 2022 at 9:48

    I had this Error too and was able to solve it after days of debugging and analysis:

    my solution

    For me VirtualBox (for Docker) was the Problem. I had Port Forwarding configured on my VM and the error only occured on the forwarded port.

    general conclusions

    The following observations may save you days of work I had to invest:

  • For me the problem only occurred on connections from localhost to localhost on one port. -> check changing any of these constants solves the problem.
  • For me the problem only occurred on my machine -> let someone else try it.
  • For me the problem only occurred after a while and couldn't be reproduced reliably
  • My Problem couldn't be inspected with any of nodes or expresses (debug-)tools. -> don't waste time on this
  • -> figure out if something is messing around with your network (-settings), like VMs, Firewalls etc., this is probably the cause of the problem.

    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.

    This one worked for me, my code which was working fine few hours back suddenly stopped working , turns out, the network change caused the problem – Aklank Jain Apr 12, 2018 at 4:39 For me two, thanks :) I was connected to a very poor wifi (but didn't know), I switch to another wifi extender to solve it. – Adrien V Dec 15, 2021 at 13:19
  • Turning off my wifi/ethernet connection and turn on.
  • I typed: npm update in terminal to update npm.
  • I tried to log out from the session and log in again
  • After that I tried the same npm command and the good thing was it worked out. I wasn't sure it is that simple.

    I am using CENTOS 7

    I just figured this out, at least in my use case.

    I was getting ECONNRESET. It turned out that the way my client was set up, it was hitting the server with an API call a ton of times really quickly -- and it only needed to hit the endpoint once.

    When I fixed that, the error was gone.

    I had the same issue and it appears that the Node.js version was the problem.

    I installed the previous version of Node.js (10.14.2) and everything was ok using nvm (allow you to install several version of Node.js and quickly switch from a version to another).

    It is not a "clean" solution, but it can serve you temporarly.

    Try adding these options to socket.io:

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

    I hope this will help you !

    Node JS socket is non-blocking io. Consider using a non-blocking io connection from other sources. For instance, if you use a blocking Java socket with node it will only work for a few seconds after which the error will be served. Mitigate this by implementing a non-blocking connection I.e. socketchannel with the selector.

    First I run my app I got ECONNRESET after that I got error like ECONNREFUSED . I had faced both of this problem while running my node app.For both of the Problem, I found that this was occuring because of not starting the wampserver.I am using mysql database in my app for getting the data with the help of wampserver. I resolve this by starting the wampserver and then after running my node app. It works fine.You can use node or nodemon for running the node application It's not the problem in my case.

    Few options I tried and worked as a temporary solutions

  • If using node, try to switch between different node versions using node use #version#. Worked for me
  • Try switching internet connection
  • Getting read ECONNRESET at TCP.onStreamRead error, while making connection with AZURE server See more linked questions
     
    推荐文章