Skip to content

Cluster node client stays cached after terminal reconnectStrategy failure, and clearing it on failure leaks clients #3396

Closed

Description

@kyungseopk1m

Description

Summary

RedisClusterSlots#createNodeClient assigns node.client before connect() resolves. If connect() rejects because the socket's reconnectStrategy gives up (returns false or an Error), the client stays cached with isOpen === false. nodeClient() only creates a new client when node.client is falsy, so every later call for that node returns the same dead client instead of retrying.

// packages/client/lib/cluster/cluster-slots.ts
#createNodeClient(node, readonly) {
  const client = node.client = this.#createClient(node, readonly);
  return node.connectPromise = client.connect()
    .finally(() => node.connectPromise = undefined);
}

The only place that clears node.client is the topology cleanup loop in #discover, and it only runs for addresses that have left the cluster topology entirely. A node that is still part of the topology but whose client has terminally failed is never cleared.

Reproduction

With [email protected], reaching into cluster-slots.js to call nodeClient() directly against a closed port (no server needed to reproduce):

const RedisClusterSlots = require('@redis/client/dist/lib/cluster/cluster-slots.js').default;

const slots = new RedisClusterSlots({
  rootNodes: [{ socket: { host: '127.0.0.1', port: 39999 } }],
  defaults: {
    socket: {
      port: 39999,
      reconnectStrategy: (retries, cause) => new Error('giving up after first failure'),
    },
  },
}, () => {});

const node = { id: 'node-a', host: '127.0.0.1', port: 39999, address: '127.0.0.1:39999', readonly: false };

await slots.nodeClient(node).catch(err => console.log('call #1 rejected:', err.message));
console.log('node.client cached:', !!node.client, 'isOpen:', node.client.isOpen);

const before = node.client;
const result = await slots.nodeClient(node).catch(() => node.client);
console.log('call #2 returned the same dead client, no reconnect attempted:', result === before);

Output:

call #1 rejected: giving up after first failure
node.client cached: true isOpen: false
call #2 returned the same dead client, no reconnect attempted: true

reconnectStrategy: retries => retries > N ? new Error(...) : delay is a common production setting, this is not an edge case.

Why clearing node.client on failure makes it worse

The obvious patch is to clear node.client in a .catch() on the connect promise. That fixes the symptom above, but a terminally-failed client can't be disposed of cleanly. RedisSocket#destroy() throws ClientClosedError when isOpen is already false, and RedisClient#destroy() calls this.#socket.destroy() before unregistering from the metrics registry and clearing credentials (packages/client/lib/client/index.ts, destroy()). So on a terminal-failed client, destroy() throws before any of that runs.

With the stock code this is a one-client leak: the dead client stays cached forever, so it only leaks once. If node.client is cleared on failure instead, every retry creates a brand new client and abandons the previous one, still registered, still holding its socket-level error/ready/end/__MOVED listeners, without ever reaching cleanup.

Measured with OpenTelemetry enabled (ClientRegistry.init()), same node, 6 calls to nodeClient():

Baseline, stock cluster-slots.ts:

call #1: registry size = 1
call #2: registry size = 1
call #3: registry size = 1
call #4: registry size = 1
call #5: registry size = 1
call #6: registry size = 1
unique clientIds registered: 1
destroy() on the cached dead client threw: ClientClosedError - The client is closed

With node.client = undefined added in the .catch():

call #1: registry size = 1
call #2: registry size = 2
call #3: registry size = 3
call #4: registry size = 4
call #5: registry size = 5
call #6: registry size = 6
unique clientIds registered: 6

Each abandoned client still has its error, ready, end and __MOVED listeners attached, so it also keeps a strong reference to the cluster instance.

What's needed

The stuck-cache problem needs a disposal path for a terminal-failed client that does not throw, so metrics unregistration, credential cleanup and listener teardown can still run regardless of isOpen. destroy() currently assumes the client was open at some point. Curious what direction you'd want here before anyone builds a fix that trades one leak for a worse one.

Node.js Version

22.16.0

Redis Server Version

n/a, reproduces without a server (connect fails against a closed local port)

Node Redis Version

6.2.1

Platform

macOS (Darwin 24.6.0, arm64)

Logs

See reproduction output above.

Activity

  1. nkaradzhov commented on Aug 14, 2026

    @nkaradzhov
    Collaborator

    @kyungseopk1m thanks for the detailed report. I opened a PR for this

  2. added a commit that references this issue on Aug 14, 2026
    eb12966
  3. added a commit that references this issue on Aug 15, 2026
    37a2692
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions