Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js HTTP GET “ECONNRESET” Error on read

I've read all the related issues on SO and GitHub about this error and none of them seem to address this situation.

When I run the following code:

response = await axios.get('http://localhost:8082/panda, {
  httpAgent: new http.Agent({ keepAlive: true, keepAliveMsecs: 10000 })
});

I get the following error:

{ Error: socket hang up
    at createHangUpError (_http_client.js:345:15)
    at Socket.socketOnEnd (_http_client.js:437:23)
    at emitNone (events.js:110:20)
    at Socket.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1059:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9) code: 'ECONNRESET' }
{ Error: read ECONNRESET
    at _errnoException (util.js:1019:11)
    at TCP.onread (net.js:608:25)
  code: 'ECONNRESET',
  errno: 'ECONNRESET',
  syscall: 'read',
  etc...

There's no stack trace beyond the onread call so it's unclear how I can get any additional information beyond the ECONNRESET error code (-54) passed to the onread method in net.js

This issue happens with every request - it is not intermittent.

A few observations:

  • when making the same request from chrome or postman the request does NOT fail
  • attempting to reproduce a successful request from Chrome, by using the same headers, fails
  • setting the accept header to use 'gzip', etc.. does not help - I have tried all the recommendations including some weird ones like setting the content length and adding a body to the request despite this being a GET request
    • the error always appears in net.js, but happens with both the request and axios libraries

Here's the interesting part - I can only seem to reproduce this problem reliably when I am hosting the server locally. I am able to reach the dev instance or run the server in vagrant, via a docker container, without issue.

I am running macOS Sierra 10.12.6 if that makes any difference. The server is written in Java and uses the Spring framework. Here is the RestTemplate configuration I'm using for HTTP calls:

@Bean
public RestTemplate restTemplate() {
    //request timeout
    int timeout = 5000;

    //Connection Pooling factory with timeouts to prevent disastrous request responses
    HttpComponentsClientHttpRequestFactory cf = new HttpComponentsClientHttpRequestFactory();
    cf.setReadTimeout(timeout);
    cf.setConnectTimeout(timeout);
    cf.setConnectionRequestTimeout(timeout);

    return new RestTemplate(cf);
}

I've tried messing around with these settings with no luck.

Any ideas?

like image 780
Jordan Avatar asked Oct 10 '17 11:10

Jordan


2 Answers

I have the same issue. The cause was in my proxy settings. I make proxy-port localhost:8080 to my localhost:3000. That's why all API requests from my NodeJS app to localhost:3000 returned "ECONNRESET" Error. When I delete proxy-port, the issue was resolved. Check your net settings. Maybe it's redirecting problem

like image 148
user12851272 Avatar answered Nov 08 '22 19:11

user12851272


This was a known issue with NodeJS and has been resolved. Please see http.Agent: idle sockets throw unhandled ECONNRESET for details.

Also ensure you have enabled the correct security settings when opening a port on MacOS

like image 21
Ben Crowhurst Avatar answered Nov 08 '22 18:11

Ben Crowhurst