From fad58d080b00546ff7181b9b2d460298f80bb4bd Mon Sep 17 00:00:00 2001 From: maxymlyskov Date: Wed, 21 Oct 2026 04:49:09 +0300 Subject: [PATCH 1/2] fix(cluster): end when quit() or disconnect() is called while reconnecting 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. --- lib/cluster/index.ts | 13 +++-- test/functional/cluster/disconnection.ts | 70 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 9bd0bd14c..28fd4fb60 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -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; @@ -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 { @@ -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; @@ -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 diff --git a/test/functional/cluster/disconnection.ts b/test/functional/cluster/disconnection.ts index df29d285a..6d9feefd8 100644 --- a/test/functional/cluster/disconnection.ts +++ b/test/functional/cluster/disconnection.ts @@ -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(); + }); }); From 1866bf159fe45e8608f6e55eea42294c573a9482 Mon Sep 17 00:00:00 2001 From: maxymlyskov Date: Wed, 21 Oct 2026 05:08:39 +0300 Subject: [PATCH 2/2] fix(cluster): only change the reconnecting case Keep the after-"end" status change out of this PR, and trim the tests to one per code path: disconnect() and quit() while reconnecting, and disconnect(true) still waiting for its pending reconnect. --- lib/cluster/index.ts | 8 +--- test/functional/cluster/disconnection.ts | 54 ++++++------------------ 2 files changed, 14 insertions(+), 48 deletions(-) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 28fd4fb60..f1099dc84 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -424,9 +424,7 @@ class Cluster< */ disconnect(reconnect = false) { const status = this.status; - if (status !== "end") { - this.setStatus("disconnecting"); - } + this.setStatus("disconnecting"); if (!reconnect) { this.manuallyClosing = true; @@ -458,9 +456,7 @@ class Cluster< */ quit(callback?: Callback<"OK">): Promise<"OK"> { const status = this.status; - if (status !== "end") { - this.setStatus("disconnecting"); - } + this.setStatus("disconnecting"); this.manuallyClosing = true; diff --git a/test/functional/cluster/disconnection.ts b/test/functional/cluster/disconnection.ts index 6d9feefd8..5e724f72f 100644 --- a/test/functional/cluster/disconnection.ts +++ b/test/functional/cluster/disconnection.ts @@ -74,18 +74,11 @@ describe("disconnection", () => { ); }); - 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) { + ["disconnect", "quit"].forEach((method) => { it(`should end when ${method}() is called while reconnecting`, async () => { - const server = new MockServer(30000, () => {}, singleNodeSlots); + const server = new MockServer(30000, () => {}, [ + [0, 16383, ["127.0.0.1", 30000]], + ]); const cluster = new Cluster([{ host: "127.0.0.1", port: "30000" }], { clusterRetryStrategy: () => 1000, }); @@ -93,52 +86,29 @@ describe("disconnection", () => { 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); + it("should not retry again when disconnect(true) is called while reconnecting", async () => { + const server = new MockServer(30000, () => {}, [ + [0, 16383, ["127.0.0.1", 30000]], + ]); + const clusterRetryStrategy = sinon.spy(() => 1000); 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); + server.disconnect(); + await new Promise((resolve) => cluster.once("reconnecting", resolve)); cluster.disconnect(true); - await new Promise((resolve) => cluster.once("ready", resolve)); expect(clusterRetryStrategy.callCount).to.equal(1); cluster.disconnect();