Skip to content

fix: keep the current db when Redis rejects a SELECT - #2308

Merged
PavelPashov merged 2 commits into
redis:mainfrom
Johannes7549:fix/restore-db-after-rejected-select
Nov 24, 2026
Merged

PavelPashov merged 2 commits into
redis:mainfrom
Johannes7549:fix/restore-db-after-rejected-select

Conversation

@Johannes7549

@Johannes7549 Johannes7549 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2307

When Redis rejects a SELECT (e.g. an out-of-range index), condition.select kept the rejected db. The connection itself stayed on the previous db, but the next reconnect selected the rejected db, failed again and left the new connection on db 0, so later commands ran against db 0.

As discussed in the issue, condition.select is still updated when the SELECT is sent, so commands issued right after select() keep tracking the new db. If Redis answers with an error reply (ReplyError), it is set back to the previous db, unless a later SELECT has changed it in the meantime. Other rejections (commandTimeout, a closed connection) keep the current behaviour, since the SELECT may still have run there. The connect-time SELECT from the db option is unchanged.

The new functional test in test/functional/select.ts rejects a SELECT on a client created with db: 2, forces a reconnect and checks with CLIENT INFO that the connection is still on db 2. It fails on main and passes with the fix.


Note

Medium Risk
Touches core sendCommand db tracking and reconnect behavior, but the rollback is narrow (Redis error replies only) and covered by a new test.

Overview
Fixes incorrect database tracking when Redis rejects a SELECT (e.g. invalid index). The client still optimistically updates condition.select when the command is sent, but on a ReplyError it rolls back to the previous db so reconnect handshakes and queued commands target the db the connection actually uses.

A functional test covers an out-of-range select(), checks condition.select stays on the configured db, and verifies after reconnect via CLIENT INFO that the connection remains on db 2.

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

condition.select was updated when a SELECT was sent and kept when Redis rejected it, so the next reconnect selected the rejected db, failed again and left the new connection on db 0: later commands ran against db 0 instead of the db the client was on. Restore the previous db when the SELECT gets an error reply and no later SELECT changed it.

Closes redis#2307
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T10:48:28.809561Z 31bcb19 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cac13bbb25

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/Redis.ts
Comment on lines +780 to +781
if (err instanceof ReplyError && this.condition.select === db) {
this.condition.select = previousDb;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve newer SELECTs that target the same database

When two SELECTs have the same parsed index, this equality check cannot detect the later command. For example, from db 2, issuing redis.select("3.0") followed by redis.select(3) without awaiting the first sends an invalid integer followed by a valid SELECT. Both are tracked as 3, so the first error rolls condition.select back to 2, and the later successful reply never restores it. The connection is then on db 3, but reconnecting silently switches subsequent reads and writes to db 2. Track SELECT ordering or identity, including same-index calls, rather than using database equality to authorize rollback.

Useful? React with 👍 / 👎.

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.

Good catch, reproduced: from db 2, select("3.0") followed by select(3) left the client tracking db 2 while the connection was on db 3, and the next reconnect moved it to db 2. Fixed in 379df36: every SELECT sent is counted (including one of the tracked db), and a rejected SELECT only restores the previous db when no later SELECT was sent. Added a functional test for this case.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread lib/Redis.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 379df36dd4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/Redis.ts Outdated
this.selectsSent === selectsSent &&
this.connectionEpoch === connectionEpoch
) {
this.condition.select = previousDb;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Notify select listeners when rolling back the tracked database

For callers that mirror the selected database using the documented select event (README.md:973), this assignment restores the DB silently after the send path has already emitted the rejected index. Starting on DB 2, a rejected select(100000) followed by a successful select(2) now produces only the event for 100000: the second SELECT emits nothing because condition.select is already 2. Before this change, the explicit reselect emitted 2, so listeners can now remain stale even after recovery. Reconcile the optimistic event when rolling back rather than changing the tracked DB without notifying listeners.

Useful? React with 👍 / 👎.

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.

Right, the rollback changed the tracked db without telling select listeners, so starting on db 2, a rejected select(100000) followed by select(2) only emitted 100000. Fixed in 31bcb19: restoring the previous db now emits select with it, and there is a functional test that checks the events are [100000, 2].

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread lib/Redis.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31bcb191b2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread lib/Redis.ts
const db = parseInt(command.args[0], 10);
const selectsSent = ++this.selectsSent;
if (this.condition.select !== db) {
const previousDb = this.condition.select;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore a confirmed database after overlapping SELECT failures

When multiple SELECTs are outstanding and both fail, previousDb can contain an earlier rejected target rather than the server's actual database. For example, from db 2, issue select("3.0") followed by select("2.0") before either reply. Redis rejects both, but the counter suppresses the first rollback and the second restores 3, even though the connection never left db 2. The next reconnect then silently switches subsequent reads and writes to db 3; before this change, this sequence retained db 2. Reconcile failed SELECTs in order or track a confirmed database instead of restoring an unconfirmed optimistic value.

Useful? React with 👍 / 👎.

@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.

@Johannes7549 Thanks for the quick follow-ups! Could you drop the last two commits (379df36 and 31bcb19) and keep the PR at cac13bb with its single test? That's the approach agreed in #2307, and it fixes the reported case.

The later commits go beyond that scope. They handle cases that only show up in contrived sequences, such as several un-awaited select() calls in flight with strings like "3.0". We haven't had any reports of these. Each round adds new state and new edge cases, which makes the code harder to follow for no clear gain. No need to cover overlapping invalid SELECTs either.

@Johannes7549

Copy link
Copy Markdown
Contributor Author

Done, the PR is back to cac13bb with the single test. Thanks for the review!

@Johannes7549
Johannes7549 force-pushed the fix/restore-db-after-rejected-select branch from 31bcb19 to cac13bb Compare November 24, 2026 13:11
Without it, the SELECT went through the offline queue and the handshake
left the connection on db 0, so the test never covered the reported case.

@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, LGTM

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 001c335. Configure here.

Comment thread lib/Redis.ts Resolved
Comment thread lib/Redis.ts Resolved
@PavelPashov
PavelPashov merged commit 0516749 into redis:main Nov 24, 2026
22 checks passed
github-actions Bot pushed a commit that referenced this pull request Dec 2, 2026
## [6.1.1](v6.1.0...v6.1.1) (2026-12-02)

### Bug Fixes

* exclude all subscriber-mode commands from auto pipelines ([#2311](#2311)) ([466f6be](466f6be))
* keep the current db when Redis rejects a SELECT ([#2308](#2308)) ([0516749](0516749)), closes [#2307](#2307)
* reject length-prefixed strings that are not followed by CRLF ([#2300](#2300)) ([77f88ee](77f88ee)), closes [#1172](#1172)
* **types:** accept binary keys in scan streams ([#2296](#2296)) ([1c1fd3c](1c1fd3c))
* **types:** keep redis-errors out of public error declarations ([#2322](#2322)) ([754d967](754d967))
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 6.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A rejected SELECT is replayed on every reconnect and leaves the client on db 0

2 participants