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

Unhandled 'error' event #1110

Open
HelloKevinTian opened this issue Apr 23, 2020 · 7 comments
Open

Unhandled 'error' event #1110

HelloKevinTian opened this issue Apr 23, 2020 · 7 comments

Comments

@HelloKevinTian
Copy link

HelloKevinTian commented Apr 23, 2020

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);
});
@osukaa
Copy link

osukaa commented May 2, 2020

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

@Ponjimon
Copy link

Ponjimon commented May 7, 2020

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

@JustTNE
Copy link

JustTNE commented Jun 1, 2020

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

@ywave620
Copy link

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!!

@OmgImAlexis
Copy link

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);
});

@Aung-Myint-Thein
Copy link

Aung-Myint-Thein commented Jan 28, 2021

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..

@tomaratyn
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants