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
Next Next commit
fix(cluster): end when quit() or disconnect() is called while reconne…
…cting

When every node connection is lost, the cluster goes to "reconnecting" with
an empty connection pool. quit() and disconnect() there set "disconnecting"
and wait for the pool's "drain", which an empty pool never emits, so the
cluster never reaches "end" and later commands stay queued. They now take
the branch "wait" already uses. On a cluster that is already "end", both
keep the status instead of moving it to "disconnecting" for good.
  • Loading branch information
maxymlyskov committed Oct 21, 2026
commit fad58d080b00546ff7181b9b2d460298f80bb4bd
13 changes: 9 additions & 4 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ class Cluster<
*/
disconnect(reconnect = false) {
const status = this.status;
this.setStatus("disconnecting");
if (status !== "end") {
this.setStatus("disconnecting");
}

if (!reconnect) {
this.manuallyClosing = true;
Expand All @@ -442,7 +444,8 @@ class Cluster<
this.shardedSubscribers.stop();
}

if (status === "wait") {
// No node is left to emit "drain" while reconnecting.
if (status === "wait" || (status === "reconnecting" && !reconnect)) {
this.setStatus("close");
this.handleCloseEvent();
} else {
Expand All @@ -455,7 +458,9 @@ class Cluster<
*/
quit(callback?: Callback<"OK">): Promise<"OK"> {
const status = this.status;
this.setStatus("disconnecting");
if (status !== "end") {
this.setStatus("disconnecting");
}

this.manuallyClosing = true;

Expand All @@ -471,7 +476,7 @@ class Cluster<
this.shardedSubscribers.stop();
}

if (status === "wait") {
if (status === "wait" || status === "reconnecting") {
const ret = asCallback(Promise.resolve<"OK">("OK"), callback);

// use setImmediate to make sure "close" event
Expand Down
70 changes: 70 additions & 0 deletions test/functional/cluster/disconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,74 @@ describe("disconnection", () => {
"Connection is closed."
);
});

const singleNodeSlots = [[0, 16383, ["127.0.0.1", 30000]]];
const trackStatusEvents = (cluster: Cluster) => {
const events: string[] = [];
for (const event of ["disconnecting", "close", "end"]) {
cluster.on(event, () => events.push(event));
}
return events;
};

for (const method of ["disconnect", "quit"] as const) {
it(`should end when ${method}() is called while reconnecting`, async () => {
const server = new MockServer(30000, () => {}, singleNodeSlots);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30000" }], {
clusterRetryStrategy: () => 1000,
});
await new Promise((resolve) => cluster.once("ready", resolve));

server.disconnect();
await new Promise((resolve) => cluster.once("reconnecting", resolve));
const events = trackStatusEvents(cluster);
await cluster[method]();
await new Promise((resolve) => setImmediate(resolve));

expect(events).to.eql(["disconnecting", "close", "end"]);
expect(cluster.status).to.equal("end");
await expect(cluster.get("foo")).to.be.rejectedWith(
"Connection is closed."
);
});

it(`should stay ended when ${method}() is called after end`, async () => {
new MockServer(30000, () => {}, singleNodeSlots);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30000" }]);
await new Promise((resolve) => cluster.once("ready", resolve));
cluster.disconnect();
await new Promise((resolve) => cluster.once("end", resolve));
const events = trackStatusEvents(cluster);

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

expect(events).to.eql([]);
expect(cluster.status).to.equal("end");
await expect(cluster.get("foo")).to.be.rejectedWith(
"Connection is closed."
);
});
}

it("should reconnect once when disconnect(true) is called while reconnecting", async () => {
const server = new MockServer(30000, () => {}, singleNodeSlots);
const clusterRetryStrategy = sinon.spy(() => 100);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30000" }], {
clusterRetryStrategy,
});
await new Promise((resolve) => cluster.once("ready", resolve));

const reconnecting = new Promise((resolve) =>
cluster.once("reconnecting", resolve)
);
await server.disconnectPromise();
await reconnecting;
new MockServer(30000, () => {}, singleNodeSlots);
cluster.disconnect(true);
await new Promise((resolve) => cluster.once("ready", resolve));

expect(clusterRetryStrategy.callCount).to.equal(1);
cluster.disconnect();
});
});
Loading