microsoft / typed-rest-client

Node Rest and Http Clients with typings for use with TypeScript
Other
675 stars 118 forks source link

Stuck in an infinite loop in HttpClient.js when the server response with ECONNREFUSED #247

Closed darrenyung closed 3 years ago

darrenyung commented 3 years ago
  1. Please check our current Issues to see if someone already reported this https://github.com/Microsoft/typed-rest-client/issues
  2. Take a look at our Instructions for logging issues https://github.com/Microsoft/typed-rest-client/blob/master/CONTRIBUTING.md#instructions-for-logging-issues

Environment

Node version: 12.16.1 Npm version: 6.13.4 OS and version: macOS Big Sur 11.1 typed-rest-client version: 1.8.1

Issue Description

Inside the httpClient.js, the request was stuck in an infinite loop when response returns with error code (ECONNREFUSED) and matches in the NetworkRetryErrors array.

while (numTries < maxTries) {
                try {
                    response = yield this.requestRaw(info, data);
                } catch (err) {
                    if (err && err.code && NetworkRetryErrors.indexOf(err.code) > -1) {
                        continue;
                    }
                    throw err;
                }
               ......
}

Expected behaviour

Should it increment the numTries counter and terminate if it reaches the threshold and/or throw an error? Otherwise, the event could be stuck for a long time until the opposing service comes back online.

Actual behaviour

Process is hung without any response or doesn't end after going beyond the timeout value set.

Steps to reproduce

  1. Start 2 microservices.
  2. Make a request to the other microservice that response back with ECONNREFUSED. (Its a scenario to simulate my problem in my production env)

Logs

Exception thrown within HttpClient.js

{
  err: Error: connect ECONNREFUSED 127.0.0.1:8003
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
    errno: 'ECONNREFUSED',
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 8003
  }
}