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
fix(cluster): finish closing when connect() is aborted during startup…
… node resolution

When the status changes while connect() is resolving the startup nodes,
connect() rejects before its "close" listeners are attached and with an
empty pool, so nothing moves the cluster out of "close" or
"disconnecting". Handle that case explicitly so the cluster reconnects or
emits "end", and pending commands are rejected.
  • Loading branch information
Duwab committed Oct 2, 2026
commit e284166092ddab4e77c8080749844a086c31ff91
22 changes: 22 additions & 0 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class Cluster<
this.status
);
reject(new RedisError("Connection is aborted"));
this.handleAbortedConnect();
return;
}
this.connectionPool.reset(nodes);
Expand Down Expand Up @@ -1049,6 +1050,27 @@ 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:
* - "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".
*/
private handleAbortedConnect(): void {
if (
this.status === "disconnecting" &&
!this.connectionPool.getNodes().length
) {
this.setStatus("close");
}
if (this.status === "close") {
this.handleCloseEvent();
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

/**
* Called when closed to check whether a reconnection should be made
*/
Expand Down
30 changes: 29 additions & 1 deletion test/functional/cluster/dnsLookup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import MockServer from "../../helpers/mock_server";
import { Cluster } from "../../../lib";
import { expect } from "chai";
import { expect, use } from "chai";

use(require("chai-as-promised"));

describe("cluster:dnsLookup", () => {
it("resolve hostnames to IPs", (done) => {
Expand Down Expand Up @@ -111,4 +113,30 @@ describe("cluster:dnsLookup", () => {
done();
});
});

["disconnect", "quit"].forEach((method) => {
it(`ends when ${method}() is called while resolving the startup nodes`, async () => {
const slotTable = [[0, 16383, ["127.0.0.1", 30001]]];
new MockServer(30001, () => {}, slotTable);

let resolveLookup: () => void;
const cluster = new Cluster([{ host: "a.com", port: "30001" }], {
dnsLookup(_, callback) {
resolveLookup = () => callback(null, "127.0.0.1");
},
});
const pending = cluster.get("foo");
const end = new Promise((resolve) => cluster.once("end", resolve));

await new Promise((resolve) => setImmediate(resolve));
cluster[method]();
resolveLookup();

await end;
expect(cluster.status).to.eql("end");
await expect(pending).to.be.rejectedWith(
"None of startup nodes is available"
);
});
});
});