From e41b22c2b75f3b040c6b9196f509f37f1556eca4 Mon Sep 17 00:00:00 2001 From: maxymlyskov Date: Mon, 19 Oct 2026 17:28:58 +0300 Subject: [PATCH] fix(cluster): stay ended when nodes removed by disconnect() end late disconnect() empties the connection pool before the node connections have closed. Each node's later "end" then finds the pool empty and emits "drain", and every drain set the cluster status to "close". The first one closes and ends the cluster, the rest move the ended cluster back to "close", where sendCommand() queues commands instead of rejecting them, so they never settle. Ignore a drain once the cluster has ended. A drain in any other status still closes the cluster, so quit(), reconnects and the aborted-connect path from #2217 are unchanged. --- lib/cluster/index.ts | 3 +++ test/functional/cluster/disconnection.ts | 28 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 98bb18989..47cc79573 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -250,6 +250,9 @@ class Cluster< this.emit("+node", redis); }); this.connectionPool.on("drain", () => { + if (this.status === "end") { + return; + } this.setStatus("close"); }); this.connectionPool.on("nodeError", (error, key) => { diff --git a/test/functional/cluster/disconnection.ts b/test/functional/cluster/disconnection.ts index 40e33e01d..df29d285a 100644 --- a/test/functional/cluster/disconnection.ts +++ b/test/functional/cluster/disconnection.ts @@ -1,8 +1,10 @@ import * as sinon from "sinon"; -import { expect } from "chai"; +import { expect, use } from "chai"; import { Cluster } from "../../../lib"; import MockServer from "../../helpers/mock_server"; +use(require("chai-as-promised")); + describe("disconnection", () => { afterEach(() => { sinon.restore(); @@ -47,4 +49,28 @@ describe("disconnection", () => { server.disconnect(); }); + + it("should stay ended when the nodes end after disconnect", async () => { + const slotTable = [ + [0, 8000, ["127.0.0.1", 30001]], + [8001, 16383, ["127.0.0.1", 30002]], + ]; + new MockServer(30001, () => {}, slotTable); + new MockServer(30002, () => {}, slotTable); + + const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }]); + await new Promise((resolve) => cluster.once("ready", resolve)); + const nodesEnded = cluster + .nodes() + .map((node) => new Promise((resolve) => node.once("end", resolve))); + + cluster.disconnect(); + await Promise.all(nodesEnded); + await new Promise((resolve) => setImmediate(resolve)); + + expect(cluster.status).to.equal("end"); + await expect(cluster.get("foo")).to.be.rejectedWith( + "Connection is closed." + ); + }); });