node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

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

Open HydraOrc opened this issue 1 year ago • 1 comments

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

HydraOrc avatar Jul 19 '24 14:07 HydraOrc

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)

leibale avatar Jul 22 '24 15:07 leibale

This issue has been automatically marked as stale due to inactivity. It will be closed in 30 days if no further activity occurs. If you believe this issue is still relevant, please add a comment to keep it open.

github-actions[bot] avatar Sep 21 '25 00:09 github-actions[bot]

This issue has been automatically closed due to inactivity. If you believe this issue is still relevant, please reopen it or create a new issue with updated information.

github-actions[bot] avatar Oct 22 '25 00:10 github-actions[bot]