redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.85k stars 1.87k forks source link

client.connect() does not throw error on non-existing or closed host:port pair #2797

Open HydraOrc opened 1 month ago

HydraOrc commented 1 month ago

Description

I was debugging why my pub/sub system does not work and then realised that some ports might be closed. To verify that, I have passed totally random host and port to my connection, and it said CONNECTED!!! Expected behavious is that it should not allow you to "fake-connect" to any host and throw an error on timeout! .on('connect') callback IS called, and does not contain any error... .on('error') callback is not called AT ALL...

const sub = createClient(redisOptions);

sub.on('error', cb);
sub.on('connect', () => {
  // this is executed without any error, error is expected
});

await sub.connect();

console.log('CONNECTED'); - Outputs on non-existing host, error is expected

Node.js Version

18.20.2

Redis Server Version

6.0.16

Node Redis Version

4.6.10

Platform

Ubuntu

Logs

No response

leibale commented 1 month ago

I just ran this script:

import { createClient } from 'redis';

await createClient({ host: '127.0.0.1', port: 6379 })
  .on('error', err => console.error(err))
  .on('connect', () => console.log('connected'))
  .connect();

and this is what I'm getting:

Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1602:16)
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
}

by default the client will try to reconnect forever, but you can change that using socket.reconnectStrategy (see https://github.com/redis/node-redis/blob/master/docs/client-configuration.md#reconnect-strategy)