Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clarify testing logic
  • Loading branch information
Duwab committed Oct 5, 2026
commit 789865b82074382a4a1ea1df233d2c79f33c54af
9 changes: 5 additions & 4 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,12 +1052,13 @@ class Cluster<

/**
* Called when connect() gives up because the status changed while it was
* resolving the startup nodes. Its "close" listeners are not set yet and the
* pool is empty, so nothing else would move the cluster out of that status:
* resolving the startup nodes. connect() has not registered its "close"
* listeners yet, so in two cases nothing else would move the cluster out of
* its current status:
* - "close": the late "end" of a node removed by a previous disconnection
* made the pool emit "drain";
* - "disconnecting": disconnect() or quit() was called, and no node is left
* to emit "drain".
* - "disconnecting" with an empty pool: disconnect() or quit() was called,
* and no node is left to emit "drain".
*/
private handleAbortedConnect(): void {
if (
Expand Down
75 changes: 44 additions & 31 deletions test/functional/cluster/lateNodeEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,68 @@ function waitFor(cluster: Cluster, event: string, ms: number) {
}

describe("cluster:late node end", () => {
it("should reconnect when a node ends while resolving the startup nodes", async () => {
const slotTable = [
[0, 8000, ["127.0.0.1", 30001]],
[8001, 16383, ["127.0.0.1", 30002]],
];
const PORT_NODE_1 = 30001;
const PORT_NODE_2 = 30002;
const slotTable = [
[0, 8000, ["127.0.0.1", PORT_NODE_1]],
[8001, 16383, ["127.0.0.1", PORT_NODE_2]],
];

// The first ready check reports a failed cluster (e.g. during a failover),
// so ioredis calls `disconnect(true)`. The node that answers it does not
// answer the FIN: its `end` only comes when ioredis destroys the socket,
// after `disconnectTimeout`.
it("should reconnect when a node ends while resolving the startup nodes", async () => {
// [Setup] Configure Redis servers (2 mock nodes)
// The first ready check fails, and the server keeps that socket half-open
let readyChecks = 0;
let slowPort: number | undefined;
const handler = (argv, c) => {
const redisServerHandler = (argv, c) => {
if (argv[0] === "cluster" && argv[1].toLowerCase() === "info") {
readyChecks += 1;
readyChecks++;
// First ready check → failure
if (readyChecks === 1) {
slowPort = c.localPort;
c.allowHalfOpen = true;
return "cluster_state:fail";
}
// Other ready checks → success
return "cluster_state:ok";
}
if (argv[0] === "get") {
return "bar";
}
};
new MockServer(30001, handler, slotTable);
new MockServer(30002, handler, slotTable);
new MockServer(PORT_NODE_1, redisServerHandler, slotTable);
new MockServer(PORT_NODE_2, redisServerHandler, slotTable);

// The lookup of the reconnection lasts until the slow node has ended.
// [Setup] Configure DNS lookup
// The lookup made by the reconnection only resolves once the slow node has ended.
let lookups = 0;
let slowNodeEndedDuringLookup = false;
const cluster = new Cluster([{ host: "redis.test", port: 30001 }], {
redisOptions: { disconnectTimeout: DISCONNECT_TIMEOUT },
dnsLookup(hostname, callback) {
lookups += 1;
if (lookups !== 2) {
callback(null, "127.0.0.1");
return;
}
const onNodeEnd = (redis) => {
if (redis.options.port !== slowPort) return;
cluster.removeListener("-node", onNodeEnd);
slowNodeEndedDuringLookup = true;
setImmediate(() => callback(null, "127.0.0.1"));
};
cluster.on("-node", onNodeEnd);
},
});
const cluster = new Cluster(
[
{
host: "redis.test", // Use a hostname instead of an IP to force one dnsLookup() per connect()
port: PORT_NODE_1,
},
],
{
redisOptions: { disconnectTimeout: DISCONNECT_TIMEOUT },
dnsLookup(hostname, callback) {
lookups++;

// Only the 2nd lookup resolves late (after the slow node has ended)
if (lookups === 2) {
const onNodeEnd = (redis) => {
if (redis.options.port !== slowPort) return;
cluster.removeListener("-node", onNodeEnd);
slowNodeEndedDuringLookup = true;
setImmediate(() => callback(null, "127.0.0.1"));
};
cluster.on("-node", onNodeEnd);
} else {
callback(null, "127.0.0.1");
}
},
}
);

// Sent during the incident: queued until the cluster is ready.
const pending = cluster.get("foo");
Expand Down