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

TypeError: Cannot read properties of undefined (reading 'address') when subscribing to channels in Redis Cluster #2904

Open
jdnichollsc opened this issue Mar 1, 2025 · 1 comment
Labels

Comments

@jdnichollsc
Copy link

jdnichollsc commented Mar 1, 2025

Description

When attempting to subscribe to Redis channels in a Redis Cluster environment, the client throws the following error:

Cluster issue for channel: general:{So11111111111111111111111111111111111111112}:{4yrHms7ekgTBgJg77zJ33TsWrraqHsCXDtuSZqUsuGHb}. This is a known issue with Redis Cluster Pub/Sub.
[Redis Subscribe - Cluster Issue] Error: TypeError: Cannot read properties of undefined (reading 'address')
    at RedisClusterSlots._RedisClusterSlots_initiatePubSubClient (/Users/jdnichollsc/dev/print-world/print.lol-backend/node_modules/@redis/client/dist/lib/cluster/cluster-slots.js:392:23)
    at RedisClusterSlots.getPubSubClient (/Users/jdnichollsc/dev/print-world/print.lol-backend/node_modules/@redis/client/dist/lib/cluster/cluster-slots.js:133:118)
    at Commander.SUBSCRIBE (/Users/jdnichollsc/dev/print-world/print.lol-backend/node_modules/@redis/client/dist/lib/cluster/index.js:151:78)
    at RedisPubSubManager.subscribe (/Users/jdnichollsc/dev/print-world/print.lol-backend/apis/ws-ui/src/RedisPubSubManager.ts:129:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at ChannelSubscriptionManager.handleChannelSubscription (/Users/jdnichollsc/dev/print-world/print.lol-backend/apis/ws-ui/src/ChannelSubscriptionManager.ts:119:9)

This error occurs in the RedisClusterSlots._RedisClusterSlots_initiatePubSubClient method when the client tries to create a dedicated Pub/Sub client for the cluster. The issue appears to be that the client is unable to properly initialize the Pub/Sub client for the cluster, possibly due to an undefined node reference.

Environment

  • Redis Server Version: 7.x (Cluster mode enabled)
  • Node Redis Version: 1.6.0
  • Node.js Version: v20.10.0
  • Platform: MacOS, Apple M2 Max

Steps to Reproduce

  1. Set up a Redis Cluster with multiple nodes (in my case, nodes on ports 7100-7105)
  2. Create a Redis client using the node-redis library and connect to the cluster
  3. Attempt to subscribe to a channel using the subscribe method
  4. The error occurs during the subscription process

Code Example

import { createCluster } from 'redis';

const redisClient = createCluster({
  rootNodes: [
    { url: 'redis://localhost:7100' },
    { url: 'redis://localhost:7101' },
    // ... other nodes
  ]
});

// This works fine
await redisClient.publish('trade:{So11111111111111111111111111111111111111112}:{4yrHms7ekgTBgJg77zJ33TsWrraqHsCXDtuSZqUsuGHb}', JSON.stringify({ data: 'test' }));

// This fails with the error
await redisClient.subscribe('trade:{So11111111111111111111111111111111111111112}:{4yrHms7ekgTBgJg77zJ33TsWrraqHsCXDtuSZqUsuGHb}', (message) => {
  console.log(message);
});

Expected Behavior

The client should successfully subscribe to the channel and receive messages published to it.

Actual Behavior

The client throws the error TypeError: Cannot read properties of undefined (reading 'address') when attempting to subscribe.

Additional Context

  • PUBLISH operations work correctly on the same channels
  • Using the Redis CLI directly to subscribe to these channels works without issues
  • The error specifically occurs in the cluster client's Pub/Sub initialization logic

This appears to be a bug in how the Redis Cluster client handles Pub/Sub subscriptions, possibly related to how it manages node connections for Pub/Sub operations.

@jdnichollsc jdnichollsc added the Bug label Mar 1, 2025
@asynchroza
Copy link

asynchroza commented Mar 14, 2025

Are you certain that your cluster is configured correctly? The library doesn’t perform any preliminary checks to verify whether a node is set as cluster-enabled or that your cluster was set up correctly. To confirm if your configuration is being recognized, you can easily validate it by adding a breakpoint or a console log after line 390 in /@redis/client/dist/lib/cluster/cluster-slots.js to log the master and replica nodes. They shouldn't be empty.

Here's the code with which I got it working:

import { createCluster } from 'redis';

const cluster = createCluster({
    rootNodes: [
        {
            url: 'redis://127.0.0.1:6379'
        },
        {
            url: 'redis://127.0.0.1:6380'
        },
        {
            url: 'redis://127.0.0.1:6381'
        },
    ],
    // useReplicas: true
});

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

await cluster.connect();

const newCluster = cluster.duplicate();

await cluster.subscribe('trade:{So11111111111111111111111111111111111111112}:{4yrHms7ekgTBgJg77zJ33TsWrraqHsCXDtuSZqUsuGHb}', (message) => {
    console.log(message);
});

await newCluster.connect();
await newCluster.publish('trade:{So11111111111111111111111111111111111111112}:{4yrHms7ekgTBgJg77zJ33TsWrraqHsCXDtuSZqUsuGHb}', JSON.stringify({ data: 'test' }));

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

No branches or pull requests

2 participants