Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: read ECONNRESET #147

Closed
Gcaufy opened this issue Aug 20, 2016 · 14 comments
Closed

Error: read ECONNRESET #147

Gcaufy opened this issue Aug 20, 2016 · 14 comments

Comments

@Gcaufy
Copy link

Gcaufy commented Aug 20, 2016

I have a the code like this.

var lists = [.......];  // There are 78 items in the array
let execFunc = (i) => {
    if (i === lists.length)
        return;
    rp.get('http://xxxx/get/' + lists[i]).then((d) => {
        execFunc(i + 1);
    });
};
execFunc(0);

and I get the error:

Unhandled rejection RequestError: Error: read ECONNRESET
    at new RequestError (/Users/apple/mine/code/project/chong_detection/node_modules/request-promise-core/lib/errors.js:14:15)
    at Request.plumbing.callback (/Users/apple/mine/code/project/chong_detection/node_modules/request-promise-core/lib/plumbing.js:87:29)
    at Request.RP$callback [as _callback] (/Users/apple/mine/code/project/chong_detection/node_modules/request-promise-core/lib/plumbing.js:46:31)
    at self.callback (/Users/apple/mine/code/project/chong_detection/node_modules/request/request.js:187:22)
    at emitOne (events.js:90:13)
    at Request.emit (events.js:182:7)
    at Request.onRequestError (/Users/apple/mine/code/project/chong_detection/node_modules/request/request.js:813:8)
    at emitOne (events.js:90:13)
    at ClientRequest.emit (events.js:182:7)
    at Socket.socketErrorListener (_http_client.js:306:9)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at emitErrorNT (net.js:1249:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

No idea what's wrong with it.

@analog-nico
Copy link
Member

You have two issues you have to look at separately:

  1. Error: read ECONNRESET is the primary cause and says that your connection/request got cancelled on the TCP/IP level. This may have many reasons but basically your request is configured correctly. It might just be the server you are sending the requests to which is resetting the connection. All you can do then is retrying the request. It might also be the general configuration of node.js but for that you would need to dig into the docs of the http module. Under the hood this module is used to send requests. Also see How to set max concurrent connections and a queue? #133 about the HttpAgent.
  2. Unhandled rejection RequestError is an error that occurred afterwards because you are not catching the first error. What you need to do is the following – which is standard promise usage:
rp.get('http://xxxx/get/' + lists[i])
    .then((d) => {
        execFunc(i + 1);
    })
    .catch((err) => {
        // Handle the error here
    });

Hope this helps!

@analog-nico
Copy link
Member

Hi @Gcaufy , I think it is safe to close this issue now. Let me know if you have more questions.

@Gcaufy
Copy link
Author

Gcaufy commented Mar 23, 2017

Sorry, forget to close this issue. Seems I had fixed it, but I don't remember how. -.-!

@analog-nico
Copy link
Member

No problem. :)

@Anaphase
Copy link

Anaphase commented Dec 4, 2017

For people who stumble upon this issue looking for a solution, try adding the Connection: 'keep-alive' header to the request. I was having issues with a stupid server always instantly closing connections unless this header was specified.

@Fire-Brand
Copy link

@Anaphase adding Connection: 'keep-alive' header to the request it didn't work me, any other ideas?

@Anaphase
Copy link

Anaphase commented Jan 4, 2018

¯\_(ツ)_/¯ could be any number of issues, try your luck on stack overflow?

@Fire-Brand
Copy link

Fire-Brand commented Jan 7, 2018

I did, but could find anything that worked...

At the end that was just an issue on my local environment (mac), when it was up on the server (Ubuntu) everything worked fine.

@ankitappuria
Copy link

ankitappuria commented Feb 24, 2018

I am also getting the above issue. please anybody can help me how they solved it?

@Getitdan
Copy link

Getitdan commented May 15, 2018

Just to add to @analog-nico's answer:

rp.get('http://xxxx/get/' + lists[i])
    .then((d) => {
        execFunc(i + 1);
    })
    .catch((err) => {
        if(err.message === 'read ECONNRESET'){
            // wait and rerun, or handle connection and rerun
        } else {
            throw err;
        }
    });

@cybairfly
Copy link

cybairfly commented Nov 2, 2018

This issue was in my case caused by this missing request option: gzip: true

@mitchmike
Copy link

I had the same error. The cause for me was over-aggressive scraping of a site, hitting it with too many requests in a short space of time.
I found the below article very helpful. Limiting maximum concurrent sockets slows down your requests and makes your scraping a bit more gentle/responsible.
https://www.gregjs.com/javascript/2015/how-to-scrape-the-web-gently-with-node-js/

@LuisSevillano
Copy link

LuisSevillano commented Dec 13, 2019

@mitchmike and @cybairfly thank you very much for your response! It took me hours to find a solution!

@thisisthemurph
Copy link

thisisthemurph commented Jan 3, 2020

I found that the solution for me was including headers.
I used something like:

let options = {
  uri: baseurl,
  headers: {
    Connection: 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants