From 1cd3eb9a8ae86402944b96673acb02d72046e557 Mon Sep 17 00:00:00 2001 From: berce <75228263+bercedev@users.noreply.github.com> Date: Thu, 22 Oct 2026 21:45:33 +0300 Subject: [PATCH] fix: end when quit() or disconnect() is called while reconnecting --- lib/Redis.ts | 8 +++++++- test/functional/disconnection.ts | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/Redis.ts b/lib/Redis.ts index 136ffc15e..f8b49a1d5 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -457,7 +457,13 @@ class Redis clearTimeout(this.reconnectTimeout); this.reconnectTimeout = null; } - if (this.status === "wait") { + if (this.status === "reconnecting" && !reconnect) { + // The socket already closed and its "close" ran before the retry was + // scheduled, so nothing would ever emit "end": end it here. + this.manuallyClosing = false; + this.setStatus("end"); + this.flushQueue(new Error(CONNECTION_CLOSED_ERROR_MSG)); + } else if (this.status === "wait") { eventHandler.closeHandler(this)(); } else { this.connector.disconnect(); diff --git a/test/functional/disconnection.ts b/test/functional/disconnection.ts index 602c8fdcd..387cdb176 100644 --- a/test/functional/disconnection.ts +++ b/test/functional/disconnection.ts @@ -1,8 +1,10 @@ import Redis from "../../lib/Redis"; import * as sinon from "sinon"; -import { expect } from "chai"; +import { expect, use } from "chai"; import MockServer from "../helpers/mock_server"; +use(require("chai-as-promised")); + describe("disconnection", () => { afterEach(() => { sinon.restore(); @@ -26,6 +28,24 @@ describe("disconnection", () => { }); }); + ["disconnect", "quit"].forEach((method) => { + it(`should end when ${method}() is called while reconnecting`, async () => { + const server = new MockServer(30001); + const redis = new Redis({ port: 30001, retryStrategy: () => 1000 }); + await new Promise((resolve) => redis.once("ready", resolve)); + + server.disconnect(); + await new Promise((resolve) => redis.once("reconnecting", resolve)); + await redis[method](); + await new Promise((resolve) => setImmediate(resolve)); + + expect(redis.status).to.equal("end"); + await expect(redis.get("foo")).to.be.rejectedWith( + "Connection is closed." + ); + }); + }); + it("should clear all timers on server exits", (done) => { const server = new MockServer(30000);