From 2cc3791ef11ccd1b1f211dd59ecb1eaa71446dc7 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 12 Oct 2026 20:15:06 -0400 Subject: [PATCH 1/2] fix: handle rejection from the db-restoring select on reconnectOnError --- lib/Redis.ts | 4 +- test/unit/reconnectOnError.ts | 83 +++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/unit/reconnectOnError.ts diff --git a/lib/Redis.ts b/lib/Redis.ts index 2da6717ca..39441a017 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -1336,7 +1336,9 @@ class Redis this.condition?.select !== item.select && item.command.name !== "select" ) { - this.select(item.select); + this.select(item.select).catch((err) => + this.silentEmit("error", err) + ); } // TODO // @ts-expect-error diff --git a/test/unit/reconnectOnError.ts b/test/unit/reconnectOnError.ts new file mode 100644 index 000000000..1de3bf787 --- /dev/null +++ b/test/unit/reconnectOnError.ts @@ -0,0 +1,83 @@ +import { expect } from "chai"; +import MockServer from "../helpers/mock_server"; +import Redis from "../../lib/Redis"; + +const READONLY_ERROR = "READONLY You can't write against a read only replica."; +const INVALID_DB_INDEX = "ERR DB index is out of range"; + +// The restoring `SELECT` is protocol-independent, but the connection setup +// around it is not: under RESP3 the client sends HELLO before anything else. +const PROTOCOLS = [3, 2] as const; + +PROTOCOLS.forEach((protocol, index) => { + describe(`reconnectOnError db restoration (RESP${protocol})`, () => { + const port = 17930 + index; + let savedListeners: any[] = []; + let unhandled: string[] = []; + + beforeEach(() => { + unhandled = []; + // test/helpers/global.ts installs an unhandledRejection listener that + // throws; record rejections here instead and restore it afterwards. + savedListeners = process.listeners("unhandledRejection"); + process.removeAllListeners("unhandledRejection"); + process.on("unhandledRejection", (reason) => { + unhandled.push(String(reason)); + }); + }); + + afterEach(() => { + process.removeAllListeners("unhandledRejection"); + for (const listener of savedListeners) { + process.on("unhandledRejection", listener); + } + }); + + it("surfaces a failing db-restoring SELECT as an error event", async () => { + // `get` fails on the first connection only, so reconnectOnError fires + // once; `select` fails from the reconnect onwards. + let connections = 0; + const server = new MockServer(port, (argv) => { + const name = String(argv[0]).toLowerCase(); + if (name === "info") { + return "# Server\r\nredis_version:7.0.0\r\n"; + } + if (name === "get" && connections < 2) { + return new Error(READONLY_ERROR); + } + if (name === "select" && connections >= 2) { + return new Error(INVALID_DB_INDEX); + } + return "OK"; + }); + server.on("connect", () => connections++); + + const redis = new Redis({ + port, + protocol, + lazyConnect: true, + retryStrategy: () => 40, + // 2 = reconnect and resend, the branch that restores the command's db. + reconnectOnError: (err: Error) => + err.message.startsWith("READONLY") ? 2 : false, + }); + const errors: string[] = []; + redis.on("error", (err: Error) => errors.push(err.message)); + await redis.connect(); + + // `select(2)` is sent before the `get` reply arrives, so the failed + // `get` (issued against db 0) needs its db restored before the resend. + await Promise.all([ + redis.get("foo").catch(() => {}), + redis.select(2).catch(() => {}), + ]); + await new Promise((resolve) => setTimeout(resolve, 500)); + + redis.disconnect(); + await server.disconnectPromise(); + + expect(unhandled).to.eql([]); + expect(errors).to.include(INVALID_DB_INDEX); + }); + }); +}); From bb87c3c75c006188f956537aeab0f342f4770b40 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Tue, 13 Oct 2026 06:26:44 -0400 Subject: [PATCH 2/2] test: cover the reconnectOnError select failure with one unit case Replace the mock-server test with a single case in test/unit/redis.ts that calls handleReconnection directly, as suggested in review. --- test/unit/reconnectOnError.ts | 83 ----------------------------------- test/unit/redis.ts | 18 ++++++++ 2 files changed, 18 insertions(+), 83 deletions(-) delete mode 100644 test/unit/reconnectOnError.ts diff --git a/test/unit/reconnectOnError.ts b/test/unit/reconnectOnError.ts deleted file mode 100644 index 1de3bf787..000000000 --- a/test/unit/reconnectOnError.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { expect } from "chai"; -import MockServer from "../helpers/mock_server"; -import Redis from "../../lib/Redis"; - -const READONLY_ERROR = "READONLY You can't write against a read only replica."; -const INVALID_DB_INDEX = "ERR DB index is out of range"; - -// The restoring `SELECT` is protocol-independent, but the connection setup -// around it is not: under RESP3 the client sends HELLO before anything else. -const PROTOCOLS = [3, 2] as const; - -PROTOCOLS.forEach((protocol, index) => { - describe(`reconnectOnError db restoration (RESP${protocol})`, () => { - const port = 17930 + index; - let savedListeners: any[] = []; - let unhandled: string[] = []; - - beforeEach(() => { - unhandled = []; - // test/helpers/global.ts installs an unhandledRejection listener that - // throws; record rejections here instead and restore it afterwards. - savedListeners = process.listeners("unhandledRejection"); - process.removeAllListeners("unhandledRejection"); - process.on("unhandledRejection", (reason) => { - unhandled.push(String(reason)); - }); - }); - - afterEach(() => { - process.removeAllListeners("unhandledRejection"); - for (const listener of savedListeners) { - process.on("unhandledRejection", listener); - } - }); - - it("surfaces a failing db-restoring SELECT as an error event", async () => { - // `get` fails on the first connection only, so reconnectOnError fires - // once; `select` fails from the reconnect onwards. - let connections = 0; - const server = new MockServer(port, (argv) => { - const name = String(argv[0]).toLowerCase(); - if (name === "info") { - return "# Server\r\nredis_version:7.0.0\r\n"; - } - if (name === "get" && connections < 2) { - return new Error(READONLY_ERROR); - } - if (name === "select" && connections >= 2) { - return new Error(INVALID_DB_INDEX); - } - return "OK"; - }); - server.on("connect", () => connections++); - - const redis = new Redis({ - port, - protocol, - lazyConnect: true, - retryStrategy: () => 40, - // 2 = reconnect and resend, the branch that restores the command's db. - reconnectOnError: (err: Error) => - err.message.startsWith("READONLY") ? 2 : false, - }); - const errors: string[] = []; - redis.on("error", (err: Error) => errors.push(err.message)); - await redis.connect(); - - // `select(2)` is sent before the `get` reply arrives, so the failed - // `get` (issued against db 0) needs its db restored before the resend. - await Promise.all([ - redis.get("foo").catch(() => {}), - redis.select(2).catch(() => {}), - ]); - await new Promise((resolve) => setTimeout(resolve, 500)); - - redis.disconnect(); - await server.disconnectPromise(); - - expect(unhandled).to.eql([]); - expect(errors).to.include(INVALID_DB_INDEX); - }); - }); -}); diff --git a/test/unit/redis.ts b/test/unit/redis.ts index 17f639e45..796c82589 100644 --- a/test/unit/redis.ts +++ b/test/unit/redis.ts @@ -287,4 +287,22 @@ describe("Redis", () => { command.verify(); }); }); + + describe("#handleReconnection", () => { + it("emits an error when the db-restoring select fails on reconnectOnError", async () => { + const redis = new Redis({ lazyConnect: true, reconnectOnError: () => 2 }); + const selectError = new Error("ERR DB index is out of range"); + sinon.stub(redis, "disconnect"); + sinon.stub(redis, "sendCommand"); + sinon.stub(redis, "select").rejects(selectError); + const emitted = new Promise((resolve) => redis.once("error", resolve)); + + redis.handleReconnection(new Error("READONLY"), { + command: new Command("get", ["foo"]), + select: 2, + }); + + expect(await emitted).to.equal(selectError); + }); + }); });