question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redis hangs indefinitely on connection lost

See original GitHub issue

Here is my code sample:

const redis = require('redis');

const client = redis.createClient({
    url: '127.0.0.1',
    password: 'password',
    disableOfflineQueue: true,
    socket: {
        timeout: 1000,
        connectTimeout: 1000,
        tls: true
    },
});

client.on('error', (err) => console.log('Redis Client Error', err));

// Connection is on here
client.connect().then(async () => {
    // I disconnect the network here and expected to get an error and handle it
    const value = await client.get('key'); // <--- application hangs here
    // this code has never been executed
    await client.quit();
    console.log(value);
}).catch(err => console.log(err));

My application hangs indefinitely if connection lost for some reasons and I want to handle this case. I try to catch every possible errors but it doesn’t help - application hangs on get() method and nothing happens.

So I just need to handle redis error on lost connection to be able to log this error and move next in my code.

Environment:

  • Node.js Version: v16.13.0
  • Redis Server Version: 6.0.5
  • Node Redis Version: 4.2.0
  • Platform: Unknown

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:23

github_iconTop GitHub Comments

1reaction
leibalecommented, Nov 22, 2022

@GledsonAfonso thanks for summarizing it 😃

just one note, I would suggest using the default reconnectStrategy instead of passing a custom one (retries => Math.min(retries * 50, 500))

1reaction
GledsonAfonsocommented, Nov 16, 2022

@GledsonAfonso have you set disableOfflineQueue to true?

Thanks for the reply and sorry for my late reply, I just manage to test this today. It didn’t work, though. Here’s the code from my Redis client:

    _client = createClient({
      legacyMode: true,
      disableOfflineQueue: true,
      socket: {
        port: environment.redisPort,
        host: environment.redisHost
      }
    });
    
    await _client.connect();

The way I’m testing this is through Docker Compose. I’m starting the Redis server along with an API that needs to connect to it and then I disconnect the Redis server and watch the logs from the API. When this happens, it fails and the application is successfully closed, but when I start it without starting the Redis server first, it gives me this log:

ConnectionTimeoutError: Connection timeout
    at Socket.<anonymous> (/app/node_modules/@redis/client/dist/lib/client/socket.js:168:124)
    at Object.onceWrapper (node:events:641:28)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at Socket._onTimeout (node:net:522:8)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)

And the API doesn’t fail, allowing the application to hang infinitely whenever some data are requested.

What I needed was that the application to throw some error when data are requested, instead of just hanging like that. I need this because the API needs to redirect the request to S3 storage whenever this problem happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Faulty Redis configuration hangs indefinitely when trying to ...
Hi,. I stumbled upon this error when testing our configurations for connecting to Redis Cache. If I provide a faulty configuration, ...
Read more >
python - Celery + Redis - .get() hangs indefinitely after running ...
My suspicion is that this has something to do with the Redis broker. The output of CLIENT LIST in redis-cli shows a large...
Read more >
Idle Redis connections eventually begin to hang, high ... - Fly.io
It seems when connecting to Redis (from Python aioredis), an idle connection will eventually begin to hang, seemingly after a delay of at ......
Read more >
Make sure Redis client library reconnects after Redis failures ...
Redis client is not timing out commands. When redis server keeps the connection open but doesn't send response (if you put the server...
Read more >
ioredis - npm
When a new Redis instance is created, a connection to Redis will be ... be lost forever if the user doesn't call redis.connect()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found