Repository navigation
Description
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
junaidkhan80121 commentedon Nov 8, 2026
I'd like to work on this.
nkaradzhov commentedon Nov 9, 2026
Thanks. Rejecting would fix
connect(), but the same#discoveralso runs at runtime, after aMOVEDand 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:
#discoverinstalls 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 withminimizeConnections: true.connectPromiseinnodeClient(), where no timeout applies.topologyRefreshOnReconnectionAttemptStrategy. Today thewasReadycheck in#createClientskips it, so after a failover its slots never move to the new master. This also means updating "post-ready" indocs/clustering.mdand in the option's JSDoc.Tests we'd want:
connect()resolves, a command for a live shard succeeds, and a command for the dead shard rejects on timeout.MOVEDwhere the new topology names a node that can't be reached: the redirected command succeeds, and laterMOVEDs still work.A PR along these lines is welcome.
Cdineshreddy12 commentedon Nov 19, 2026
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— withminimizeConnections: false,#initiateSlotNodeeagerly pushes each node'sconnect()intopromises, so one unreachable master makes thatPromise.all(and thereforeconnect()) hang until the node'sreconnectStrategygives up or it comes back.minimizeConnections: trueskips the eager connects, which is why it doesn't repro there.On your plan:
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#discoverstillawaits#getShardsand#resetSlotsbefore returningtrue, so the slot map is in place before any command routes.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-connectPromiseshape 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.The
wasReadyskip in#createNodeClient(if (!wasReady) return) is what currently stops a never-ready node from counting fortopologyRefreshOnReconnectionAttemptStrategy— so a node that failed its initial connect never triggers the refresh that would move its slots. Dropping that guard (plus thedocs/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.
nkaradzhov commentedon Nov 19, 2026
@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
connectPromisewait innodeClient().One correction to my earlier comment: "post-ready" is only in the option's JSDoc (
cluster/index.ts).docs/clustering.mdjust says "a node reconnection attempt", so update it to say a node that never connected counts too.Cdineshreddy12 commentedon Nov 19, 2026
Thanks, and thanks for merging #3642. I'll take this.
Plan on my side:
connectPromisewait innodeClient()— routing the command into the node client's queue there instead, so the command timeout applies.docs/clustering.mdsays "a node reconnection attempt", I'll update it to make clear a node that never connected counts too, and align the option's JSDoc incluster/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; aMOVEDnaming 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.fix(cluster): bound the wait for a connecting node by the command tim…
fix(cluster): keep the pub/sub resubscribe awaited during a refresh
test(cluster): keep discovery off the blocked port in the redis#3592 …
fix(cluster): don't hang connect() or commands on an unreachable node (…