Skip to content

Cluster connect() never settles while one master is unreachable #3592

Done

Description

@maxymlyskov

Description

If one master can't be reached when cluster.connect() runs, the promise never settles. That happens with the default options (minimizeConnections: false). The cluster stays not ready, so every command fails with ClientOfflineError, including commands for shards that are up, and a second connect() throws Cluster already open. It stays that way after the cluster forgets the dead node and moves its slots to a live master, because discovery never runs again. Only a restart of the process, or that node coming back, gets it out.

Expected: connect() rejects, the way the sentinel client does since #3331 (#3066), so the app can retry or fail its startup. With minimizeConnections: true the same outage is fine: connect() resolves in about 15 ms and commands for the live shards work.

Steps: a 3-master cluster on 127.0.0.1:7001-7003 with cluster-require-full-coverage no, then redis-cli -p 7003 shutdown nosave, then:

import { createCluster } from "redis";

const cluster = createCluster({
  rootNodes: [{ url: "redis://127.0.0.1:7001" }],
});
cluster.on("error", () => {});

const start = Date.now();
let state = "pending";
cluster.connect().then(
  () => (state = `resolved after ${Date.now() - start} ms`),
  (err) => (state = `rejected: ${err.message}`)
);

await new Promise((r) => setTimeout(r, 10_000));
console.log("connect() after 10 s:", state);
console.log("isReady:", cluster.isReady);
// {b} hashes to slot 3300, on 7001, which is still up
await cluster.set("{b}key", "v").then(
  (r) => console.log("SET on the 7001 shard:", r),
  (e) => console.log("SET on the 7001 shard:", e.constructor.name, e.message)
);
await cluster.connect().catch((e) => console.log("second connect():", e.message));
process.exit(0);

The output is under Logs, the same on 3 of 3 runs.

#discover creates a client for every node in CLUSTER SLOTS and waits for all of their connect() calls:

promises.push(this.#createNodeClient(node));

await Promise.all(promises);

With the default reconnectStrategy, a node client's connect() keeps retrying a refused address and never rejects:

} while (this.#isOpen && !this.#isReady);

So the Promise.all waits as long as that node is down. rediscover() waits the same way, and since every MOVED shares #runningRediscoverPromise, a new node that can't be reached would leave later rediscovers waiting too. I have only reproduced the connect() case. #2477 and the last comment on #1782 (replicas on a port the app can't reach) look like the same thing.

Proposed change: the same approach as #3331. In #discover, race each connect it waits on (the node clients and the pub/sub client) against that client's first error. On failure, destroy the clients it created and reject, so connect() can be called again. rediscover() would get the same treatment. Since this turns a hang into a rejection, I'd like to know if this is the behaviour you want before opening the PR. The change is in cluster-slots.ts, and I can open a PR with a failing test in cluster-slots.spec.ts that maps one master to an address with no server on it.

Node.js Version

v22.23.3

Redis Server Version

8.10.2

Node Redis Version

redis 6.4.0-beta.0 (master 2df73c8)

Platform

Linux

Logs

connect() after 10 s: pending
isReady: false
SET on the 7001 shard: ClientOfflineError The client is offline
second connect(): Cluster already open

Activity

  1. junaidkhan80121 commented on Nov 8, 2026

    @junaidkhan80121
    Contributor

    I'd like to work on this.

  2. nkaradzhov commented on Nov 9, 2026

    @nkaradzhov
    Collaborator

    Thanks. Rejecting would fix connect(), but the same #discover also runs at runtime, after a MOVED and on background topology refreshes. There, failing on the first node error would fail every redirected command, including ones for healthy shards, and leave the slot map half updated. So we'd rather not go that way.

    The goal: one unreachable node should cost only its own shard. The direction we'd take:

    1. #discover installs the new slot map and does not wait for node connections (the node clients and the pub/sub client). connect() then behaves the way it does with minimizeConnections: true.
    2. A command for a node that has not connected yet goes into that node client's queue, so the normal command timeout applies. Today it waits on the node's connectPromise in nodeClient(), where no timeout applies.
    3. A node that was never ready also counts for topologyRefreshOnReconnectionAttemptStrategy. Today the wasReady check in #createClient skips it, so after a failover its slots never move to the new master. This also means updating "post-ready" in docs/clustering.md and in the option's JSDoc.

    Tests we'd want:

    • One master on a dead port: connect() resolves, a command for a live shard succeeds, and a command for the dead shard rejects on timeout.
    • The same setup, then a failover: a refresh moves the dead shard's slots, and commands for them succeed.
    • A MOVED where the new topology names a node that can't be reached: the redirected command succeeds, and later MOVEDs still work.

    A PR along these lines is welcome.

  3. Cdineshreddy12 commented on Nov 19, 2026

    @Cdineshreddy12
    Contributor

    I traced this and your three-point direction lines up with what I see in cluster-slots.ts:

    Root cause is the await Promise.all(promises) at the end of #discover — with minimizeConnections: false, #initiateSlotNode eagerly pushes each node's connect() into promises, so one unreachable master makes that Promise.all (and therefore connect()) hang until the node's reconnectStrategy gives up or it comes back. minimizeConnections: true skips the eager connects, which is why it doesn't repro there.

    On your plan:

    1. Not awaiting the node/pub-sub connects in #discover (installing the slot map first) is the key change, and it unifies the two modes — connect() then resolves on topology, not on every node being reachable. The one thing to preserve is that #discover still awaits #getShards and #resetSlots before returning true, so the slot map is in place before any command routes.

    2. For routing a command to a not-yet-connected node through the node client's queue (so the command timeout applies) instead of awaiting connectPromise: this is the same no-timeout-on-connectPromise shape I just flagged on fix(client): retry a node connect cut short by a failover #3628's retry path — so fixing it here would cover that retry too. Worth keeping the two together.

    3. The wasReady skip in #createNodeClient (if (!wasReady) return) is what currently stops a never-ready node from counting for topologyRefreshOnReconnectionAttemptStrategy — so a node that failed its initial connect never triggers the refresh that would move its slots. Dropping that guard (plus the docs/clustering.md + JSDoc "post-ready" wording) is needed for your second test scenario to pass.

    I've been working in this file recently (#3642, and the review on #3628 above) and this is squarely the kind of cluster-connect correctness I'm enjoying. @junaidkhan80121 — are you still on this? If not, @nkaradzhov I'd be glad to take it along the lines above, with the three test scenarios you listed.

  4. nkaradzhov commented on Nov 19, 2026

    @nkaradzhov
    Collaborator

    @Cdineshreddy12 go ahead, it's yours. @junaidkhan80121's PR was closed, so this is open.

    Please base it on #3628 once that merges, since point 2 replaces the same connectPromise wait in nodeClient().

    One correction to my earlier comment: "post-ready" is only in the option's JSDoc (cluster/index.ts). docs/clustering.md just says "a node reconnection attempt", so update it to say a node that never connected counts too.

  5. Cdineshreddy12 commented on Nov 19, 2026

    @Cdineshreddy12
    Contributor

    Thanks, and thanks for merging #3642. I'll take this.

    Plan on my side:

    • Points 1 (install the slot map without waiting on the node/pub-sub connects) and 3 (count a never-connected node for the reconnection strategy, plus the docs/JSDoc wording) are independent of fix(client): retry a node connect cut short by a failover #3628, so I'll get those ready first.
    • For point 2 I'll wait for fix(client): retry a node connect cut short by a failover #3628 to merge and base on it, since it reworks the same connectPromise wait in nodeClient() — routing the command into the node client's queue there instead, so the command timeout applies.
    • Docs: noted — docs/clustering.md says "a node reconnection attempt", I'll update it to make clear a node that never connected counts too, and align the option's JSDoc in cluster/index.ts.

    Tests: I'll add the three scenarios you listed (dead-port master → connect() resolves + live-shard ok + dead-shard rejects on timeout; same + failover moves the slots; a MOVED naming an unreachable node). Heads-up that I can't run the cluster suite locally (the docker harness uses --network host, which doesn't map ports on Docker for Mac), so I'll lean on CI for the integration tests and flag point 2 for your review.

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