Express: Error: connect ECONNRESET

Created on 2 Jun 2017  路  10Comments  路  Source: expressjs/express

I have an express server running on localhost through app.listen().
It serves images through app.get('imageId'). The images are read from a DB.
I have a client application firing 400+ requests at the same time through http.get()

After the first 300+ requests, I get this error on the client side for each subsequent request:
Error: connect ECONNRESET 127.0.0.1:8080
There is no stack or any further detail.

On the server side, I have these error handlers, but none of them print anything:

server.on('error', function(err){
    console.error('on error handler');
    console.error(err);
});
server.on('clientError', function(err){
    console.error('on clientError handler');
    console.error(err);
});
process.on('uncaughtException', function(err) {
    console.error('process.on handler');
    console.error(err);
});

My guess is that the server is not able to handle all the requests at once and it starts rejecting the connections until it frees resources, but haven't found a way to prove it.

Is there a way to specify the number of connections on the server?
Am I doing something wrong on the server side to handle the error?

Most helpful comment

Ugh, wish this were addressed somewhere. I've got unit tests randomly failing with ECONNRESET and the more functionality I add to the server, the higher frequency of the failures.

All 10 comments

Check your code for an error piping a stream to res. I've run into the same issue and that was the cause. Hope this helps.

Also, this error might be better for stackoverflow.

Thanks @michaelBenin,

Is there a way to set the number of max connections in express?

Hi @RussellRC all the app.listen() is is a short-cut to the Node.js HTTP server creation + listen. Do you see the same behavior if you replace your app.listen(port) with the following?

require('http').createServer(app).listen(port)

If so, this is likely an issue with Node.js directly.

For max connections, as far as I know, there is no maximum defined to even change.

You can set global max sockets in http and https packages. I could help you debug if I could see the middleware. Could you show us some of the controllers/middleware/route handlers used? There is a broken pipe somewhere though. Try using a try catch around some of your stream logic.

Hi @dougwilson. I tried what you said and I get the same error.

On the other hand, I created a client on Java that does the same as my client on NodeJS: fires 400+ concurrent requests, each one getting a different image from the server, and it worked just fine, so now I suspect the problem is on my NodeJS client.

Here is a snippet of what I am doing on the client:

        var srvUrl = url.parse(imgUrl);
        var options = {
                hostname: srvUrl.hostname,
                port: srvUrl.port,
                path: srvUrl.path,
                timeout: 60000
        };

        http.get(options, function(response) {
            var outFile = fs.createWriteStream(outFileName, {flags: 'w', encoding: 'base64'});
            response.pipe(outFile);

            outFile.on('error', function(error) {
                console.log("pipe error on", outFileName);
            });
        }).on('error', function(e) {
            console.error("Got error for '" + imgUrl + "' :" + e);
        }).end();

The error I get (Error: connect ECONNRESET 127.0.0.1:8080) comes from http.get().on('error'), not form the pipe.

@RussellRC are you able to reproduce this with an invalid imgUrl? Try passing something you know will fail. Like I said above, I spent a good amount of time dealing with the same issue with node streams on a few projects.

response.pipe(outFile); with an invalid url or a url that doesn't exist will result with the original error reported, the ECONNRESET. Can you try wrapping response.pipe(outFile); in a try catch? I don't think this is a problem with express.

@michaelBenin I did what you suggested and still got the same error, which makes sense because it is happening on the http.get() method, not on the pipe.

However, one thing I just noticed is that the first time I run my script, a few calls on http.get() fail, but on subsequent runs of the script, sometimes all the requests go through. So I agree that this is not a problem with express but rather how http.get() works... I'll do some more digging, but for now I think we can close this issue.

Thanks a lot for the help.

Ugh, wish this were addressed somewhere. I've got unit tests randomly failing with ECONNRESET and the more functionality I add to the server, the higher frequency of the failures.

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.

Got this error too, I found why and it's about the port too.
This can happen if you are using a node script watcher like nodemon or a process manager such as pm2

  • For nodemon, the previous instance of the script could not be totally "killed" before the new one starts, then you will get 2 servers listening on the same port.
  • For pm2, I've just forgotten to close an instance before starting a new one, that's kinda the same thing.

So, if you don't have a port forwarding issue, check for your server instances or if the port isn't taken by another program 馃槈

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndrewEQ picture AndrewEQ  路  4Comments

extensionsapp picture extensionsapp  路  3Comments

dmaks9 picture dmaks9  路  3Comments

jefflage picture jefflage  路  4Comments

zackarychapple picture zackarychapple  路  3Comments