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

How can I prevent node process crash when request timeout? #5538

Open
Alivers opened this issue Feb 7, 2023 · 1 comment
Open

How can I prevent node process crash when request timeout? #5538

Alivers opened this issue Feb 7, 2023 · 1 comment
Labels
type::bug Used when reporting a bug

Comments

@Alivers
Copy link

Alivers commented Feb 7, 2023

Describe the issue

When the request timeout(10s), node Process crashed due to triggerUncaughtException but the catch block logged the error msg expectly (code as example code). Thanks for your reading and work.
When I upgrade axios to 1.3.2, the error was reproduced.

{"level":50,"time":1675693965785,"pid":42,"hostname":"api-7d5f5b8857-jpx52","context":"PythPriceFetcher","msg":"error occurs when getCoinPrice from pyth, pythPriceId=0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d, 
reason: {
\"message\":\"timeout of 10000ms exceeded\",
\"name\":\"Error\",
\"stack\":\"Error: timeout of 10000ms exceeded
 at createError (/workspace/api-service/srv/main.cjs:87147:20)
 at RedirectableRequest.handleRequestTimeout (/workspace/api-service/srv/main.cjs:87815:20)
at RedirectableRequest.emit (node:events:513:28)\n    
at Timeout.<anonymous> (/workspace/api-service/srv/main.cjs:64949:17)\n    
at listOnTimeout (node:internal/timers:569:17)\n    
at process.processTimers (node:internal/timers:512:7)\",
\"config\":{\"transitional\":{\"silentJSONParsing\":true,\"forcedJSONParsing\":true,\"clarifyTimeoutError\":false},\"transformRequest\":[null],\"transformResponse\":[null],\"timeout\":10000,\"xsrfCookieName\":\"XSRF-TOKEN\",\"xsrfHeaderName\":\"X-XSRF-TOKEN\",\"maxContentLength\":-1,\"maxBodyLength\":-1,\"headers\":{\"Accept\":\"application/json, text/plain, */*\",\"User-Agent\":\"axios/0.26.1\"},\"baseURL\":\"https://xc-mainnet.pyth.network\",\"params\":{\"ids\":[\"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d\"],\"verbose\":false},\"method\":\"get\",\"url\":\"/api/latest_price_feeds\",\"axios-retry\":{\"retryCount\":0,\"lastRequestTime\":1675693955711}},\"code\":\"ECONNABORTED\",\"status\":null}"}

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error: timeout of 10000ms exceeded
    at createError (/workspace/api-service/srv/main.cjs:87147:20)
    at RedirectableRequest.handleRequestTimeout (/workspace/api-service/srv/main.cjs:87815:20)
    at RedirectableRequest.emit (node:events:513:28)
    at Timeout.<anonymous> (/workspace/api-service/srv/main.cjs:64949:17)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function: httpAdapter],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 10000,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: [Function: validateStatus],
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.26.1'
    },
    baseURL: 'https://xc-mainnet.pyth.network',
    params: {
      ids: [
        '0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d'
      ],
      verbose: false
    },
    method: 'get',
    url: '/api/latest_price_feeds',
    'axios-retry': { retryCount: 0, lastRequestTime: 1675693955711 },
    data: undefined
  },
  code: 'ECONNABORTED',
    _currentUrl: 'https://xc-mainnet.pyth.network/api/latest_price_feeds?ids[]=0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d&verbose=false',
    _timeout: null,
    [Symbol(kCapture)]: false
  },
  response: undefined,
  isAxiosError: true,
  toJSON: [Function: toJSON]
}

Node.js v18.14.0
 ELIFECYCLE  Command failed with exit code 1.
 WARN   Local package.json exists, but node_modules missing, did you mean to install?
 ELIFECYCLE  Command failed with exit code 1.

Example Code

// constructor (It calls axios.create() with timeout config internally)
    this.aptosPriceConn = new AptosPriceServiceConnection(this.config.config.pythMainnetEndpoint, {
      timeout: 10000,
      httpRetries: 0,
    });

// use (Catch the exception)
this.aptosPriceConn
            .getLatestPriceFeeds([pythPriceId])
            .then((feeds) => {
              if (feeds && feeds.length > 0) {
                const priceCfg = feeds[0].getPriceUnchecked();

                const p = Big(priceCfg.price).mul(Big(10).pow(priceCfg.expo));

                if (p.gt(0)) {
                  this.logger.debug(`pyth, ${pythPriceId} price = ${p.toString()}`);
                  return p.toNumber();
                }
              }

              throw `can't fetch price from pyth, pythPriceId=${pythPriceId}`;
            })
            .catch((reason) => {
              this.logger.error(
                `error occurs when getCoinPrice from pyth, pythPriceId=${pythPriceId}, reason: %o`,
                reason,
              );
              throw reason;
            });

Expected behavior

  1. Catch the reject error to prevent node process from crash.

Axios Version

0.26.1

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

18.14.0

OS

Debian 10, amd64 arch

Additional Library Versions

No response

Additional context/Screenshots

No response

@ralph7777
Copy link

Looks like it is similar to this one but I don't see any resolution yet...:
#3396

@DavidJDallas DavidJDallas added the type::bug Used when reporting a bug label Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type::bug Used when reporting a bug
Projects
None yet
Development

No branches or pull requests

3 participants