#1647·ioredis

Connection is not closed when `Error: getaddrinfo ENOTFOUND` is thrown

Author: rajgaur98Created Sep 6, 2022Updated Sep 6, 2026
Labelsstale

I was getting unexpected results when using the blpop command, so I debugged a bit and found this issue. When there is an Error: getaddrinfo ENOTFOUND error, ioredis tries to reconnect, and when it reconnects it creates a new connection instead of reusing the old one. So now I have two open client connections, one before the error and one after the error. I found this by using client list command in redli cli.

image

Now when the list I am waiting for using blpop is filled, it pops the element and returns to the first/old connection, which is not usable anymore in my code and gets lost (doesn't give any error and doesn't even execute it, the element is just lost). Again when an element is entered in the list, now the second/new connection picks up that element and executes the given code.

So the problem here is that the first element in the list after a reconnect gets lost because of the old connection.

There is another issue with this, when I kill my terminal where I am running the Nodejs code, the second/new connection also gets killed, but the first/old connection is not killed as I can still see it in the client list.

Here is my configuration and code

const redis = new Redis({
  port: process.env.REDIS_PORT,
  host: process.env.REDIS_HOST,
  connectionName: process.env.REDIS_USERNAME,
  password: process.env.REDIS_PASSWORD,
  connectTimeout: 30000,
  tls: {},
  keepAlive: 1000,
  enableOfflineQueue: true,
  retryStrategy(times) {
    const delay = Math.min(times * 50, 2000);
    return delay;
  },
});
const scheduler = async () => {
  try {
    const newProcess = await redis.blpop("puppeteer", 0);
    const reservationObj = JSON.parse(newProcess[1]);
    await someAsynFunction();
  } catch (err) {
    console.error(err);
  }
  await scheduler();
};

scheduler();

I am expecting that on Error: getaddrinfo ENOTFOUND error, either the existing connection gets reused or if a new connection is created, the old connection should be deleted.

PS: I am trying to build a Job Queue using the above code