Skip to content

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

Open
@jdnichollsc

Description

@jdnichollsc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions