ioredis icon indicating copy to clipboard operation
ioredis copied to clipboard

Unhandled 'error' event

Open HelloKevinTian opened this issue 4 years ago • 7 comments

Hi,I am trying to connect redis cluster by ioredis.

node version: 10.19.0 ioredis: 4.16.3

I met "Unhandled 'error' event" error message and my application crashed when I disconnect from the network.

Actually I have listened the "error event" in my code,but I still met the error.

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: connect ENETUNREACH 192.168.1.71:7000 - Local (0.0.0.0:50955)
    at internalConnect (net.js:882:16)
    at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
    at defaultTriggerAsyncIdScope (net.js:972:9)
    at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

My code is as follows.


const IORedis = require('ioredis');

let cluster = new IORedis.Cluster([{
    host: '192.168.1.71,
    port: 7000
}, {
    host: '192.168.1.71',
    port: 7001
}, {
    host: '192.168.1.71',
    port: 7002
}], {
    clusterRetryStrategy: function() {
        let delay = Math.min(2000 + times * 50, 10000);
        return delay;
    }
});

cluster..on('connect', function() {
    console.log(`Redis Cluster connected `);
});

cluster.on('error', function(err) {
    console.error('Redis Cluster error', err);
});

HelloKevinTian avatar Apr 23 '20 03:04 HelloKevinTian

It might be because ioredis will try to connect upon using new IORedis.Cluster() by the point you're adding a listener the error event might have already been emitted.

You could try adding lazyConnect: true to the options, adding the listeners and then doing an start to see if that's the problem

osukaa avatar May 02 '20 01:05 osukaa

I'm using lazyConnect: true and I noticed, if the connection is already established and I then close the Redis server, ioredis keeps spamming unhandled error event even though I have a listener on error

Ponjimon avatar May 07 '20 10:05 Ponjimon

Also having this issue, but with lazyConnect: true already set

JustTNE avatar Jun 01 '20 12:06 JustTNE

I'm using lazyConnect: true and I noticed, if the connection is already established and I then close the Redis server, ioredis keeps spamming unhandled error event even though I have a listener on error

I also have this problem!!

ywave620 avatar Jul 16 '20 04:07 ywave620

Does adding a disconnect to the error handler fix the issue?

This should work, I also fixed a few typos.

const IORedis = require('ioredis');

let cluster = new IORedis.Cluster([{
    host: '192.168.1.71',
    port: 7000
}, {
    host: '192.168.1.71',
    port: 7001
}, {
    host: '192.168.1.71',
    port: 7002
}], {
    clusterRetryStrategy: function() {
        let delay = Math.min(2000 + times * 50, 10000);
        return delay;
    }
});

cluster.on('connect', function() {
    console.log(`Redis Cluster connected `);
});

cluster.on('error', function(err) {
    cluster.disconnect();
    console.error('Redis Cluster error', err);
});

OmgImAlexis avatar Oct 07 '20 08:10 OmgImAlexis

have you all solved this issue? I am having the same issue. Although I try to catch the error with on('error'), the whole node application exited when the connection is unsuccessful. Would be nice to have some workaround for this :D thanks!

Update: I tried to wrap the whole connection code in try catch and it is also not working..

Aung-Myint-Thein avatar Jan 28 '21 10:01 Aung-Myint-Thein

Can you see if my PR #1501 helps you? I think #1500 may be a similar issue.

tomaratyn avatar Feb 02 '22 03:02 tomaratyn