'Nodejs - Axios - Timeouts lead to memory leak

I'm running an app which basically:

Receives a request through Express.js Send multiples requests to various endpoints Once those endpoints responses, we compute a response and send it to the client. I'm using Axios Instances per endpoints to send the requests.

const axios = require('axios');

const Agent = require('agentkeepalive');

const { HttpsAgent } = Agent;

const httpKeepAliveAgent = new Agent({
  maxSockets: 100,
  maxFreeSockets: 10,
  timeout: 60000, // active socket keepalive for 60 seconds
  freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
});

const httpsKeepAliveAgent = new HttpsAgent({
  maxSockets: 100,
  maxFreeSockets: 10,
  timeout: 60000, // active socket keepalive for 60 seconds
  freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
});

const createAxiosInstance = () => axios.create({
  httpAgent: httpKeepAliveAgent,
  httpsAgent: httpsKeepAliveAgent,
  maxRedirects: 10,
});

I send requests to third party endpoint using the following

const fakeServer = require('../test/fake-server');
const logger = require('../utils/logger');

const { LOG_VERBOSE } = process.env;

// let promiseCount = 0;

module.exports = async (axiosInstance, ssp, payload, endpoint, method, timeout, headers) => {
  const cmd = process.env.NODE_ENV === 'test' ? fakeServer : axiosInstance;
  const start = Date.now();
  const config = {
    ssp,
    url: endpoint,
    method,
    timeout,
    headers: {
      'Content-Type': 'application/json; charset=utf-8;',
      Accept: 'application/json',
    },
    data: payload,
  };
  if (headers !== undefined && typeof headers === 'object') {
    // eslint-disable-next-line no-return-assign
    Object.keys(headers).forEach((key) => config.headers[key] = headers[key]);
  }
  try {
    const response = await cmd(config);
    return {
      ssp,
      uri: config.url,
      requestbody: payload,
      requestheaders: config.headers,
      responsebody: response.data,
      status: response.status,
      responsetimemillis: Date.now() - start,
    };
  } catch (error) {
    if (LOG_VERBOSE === 'true') logger.error(`Error on ${ssp} call: ${error.message}`);
    let responsebody;
    let status;
    if (error.response === undefined) {
      responsebody = error.code;
      status = error.code;
    } else {
      responsebody = error.response.data ? error.response.data : error.message;
      status = error.response.status;
    }
    return {
      ssp,
      uri: config.url,
      requestbody: payload,
      requestheaders: config.header,
      responsebody,
      status,
      responsetimemillis: Date.now() - start,
    };
  }
};

The issues is that when I get timeouts or 400 error, there might happen some memory leaks, I can't get a successful linked connection, and so at the end the app crashes...

Any ideas?



Solution 1:[1]

https://github.com/axios/axios/issues/2783

maxRedirects may be causing this issue, try setting lower redirects or 0, as the resolved issue suggests.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0xckylee