Skip to content

fix: handle rejection from the db-restoring select on reconnectOnError - #2229

Merged
PavelPashov merged 2 commits into
redis:mainfrom
sprayberry-code:fix/reconnect-select-rejection
Oct 13, 2026
Merged

PavelPashov merged 2 commits into
redis:mainfrom
sprayberry-code:fix/reconnect-select-rejection

Conversation

@askalf

@askalf askalf commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Small, isolated bug fix with a focused test, submitted under the carve-out in .github/CONTRIBUTING.md ("Small, isolated bug fixes where the cause and solution are clear and a focused test demonstrates the fix"). Happy to open an issue first if you'd prefer that route.

Summary

  • When reconnectOnError returns 2, handleReconnection reconnects and resends the failed command. If the command was issued against a different db, it first calls this.select(item.select) to restore it.
  • That promise is fire-and-forget. If the restoring SELECT is rejected after the reconnect, the rejection is unhandled even when the client has an error listener, which Node's default --unhandled-rejections=throw mode turns into an uncaught exception.
  • The other places that restore state on reconnect already route the rejection to the client: the offline-queue replay and readyHandler in lib/redis/event_handler.ts (fix: handle rejections from auto-resubscribe and select on reconnect #2187) use .catch((err) => self.silentEmit("error", err)).

Fix

The this.select(item.select) call in handleReconnection gets the same .catch((err) => this.silentEmit("error", err)). The resend after it is unchanged. 3 insertions, 1 deletion in lib/Redis.ts.

Test

test/unit/reconnectOnError.ts: one case under RESP3 and RESP2, built like test/unit/resubscribe.ts from #2187 (MockServer, with the global unhandledRejection listener swapped for a recorder). get fails with READONLY on the first connection while select(2) is in flight, so reconnectOnError returns 2 and the client restores the get's db before resending it; the mock rejects SELECT from the second connection on.

$ TS_NODE_TRANSPILE_ONLY=true NODE_ENV=test ./node_modules/.bin/mocha --no-experimental-strip-types "test/helpers/*.ts" test/unit/reconnectOnError.ts

# lib/Redis.ts at main (33d98e2)
  0 passing (1s)
  2 failing
  1) reconnectOnError db restoration (RESP3)
       surfaces a failing db-restoring SELECT as an error event:
      AssertionError: expected [ Array(1) ] to deeply equal []
      + expected - actual
      -[
      -  "ReplyError: ERR DB index is out of range"
      -]
      +[]
  # RESP2 fails the same way

# this branch
  reconnectOnError db restoration (RESP3)
    ✔ surfaces a failing db-restoring SELECT as an error event (624ms)
  reconnectOnError db restoration (RESP2)
    ✔ surfaces a failing db-restoring SELECT as an error event (590ms)
  2 passing (1s)

prettier --check passes on the new test file and lib/Redis.ts. eslint on lib/Redis.ts reports the same 18 warnings as main and no errors.

AI assistance: the bug was traced and the fix and test were drafted with AI tooling in my workflow; the test, prettier and eslint were executed as pasted above. I'm responsible for the change and will handle review feedback.


Note

Low Risk
Narrow reconnect error-handling change aligned with existing select().catch patterns; no auth or data-path behavior change beyond surfacing failures as error events.

Overview
When reconnectOnError returns 2, the client reconnects and resends the failed command; if the command was on a non-default DB, handleReconnection first calls this.select(item.select) to restore it.

That SELECT was invoked without handling rejection, so a failed restore could become an unhandled promise rejection even when the client has an error listener. The PR adds the same .catch((err) => this.silentEmit("error", err)) pattern already used in offline-queue replay.

A unit test stubs a failing select during handleReconnection and asserts the error is emitted on the client.

Reviewed by Cursor Bugbot for commit bb87c3c. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T10:29:12.327319Z bb87c3c New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down! This is the last bare this.select(...) in lib/, and the .catch((err) => this.silentEmit("error", err)) matches the other restore paths, so the fix looks good to merge once the test is slimmed down (see inline comment).

Comment thread test/unit/reconnectOnError.ts Outdated
Comment on lines +1 to +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);
});
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we replace this file with a single case in test/unit/redis.ts that calls handleReconnection directly? The behavior doesn't depend on the protocol or a real socket, and test/helpers/global.ts already fails the run on unhandled rejections, so we don't need the mock server, the RESP2/RESP3 loop, the unhandledRejection swap, or the 500ms sleep.

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);
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in bb87c3c: the file is gone and the case is in test/unit/redis.ts as you wrote it. It fails with the unhandled rejection on the old lib/Redis.ts and passes with the fix; the unit suite is 517 passing.

Replace the mock-server test with a single case in test/unit/redis.ts that
calls handleReconnection directly, as suggested in review.

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PavelPashov
PavelPashov merged commit 1b744ca into redis:main Oct 13, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants