Repository navigation
fix: keep the current db when Redis rejects a SELECT - #2308
Conversation
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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| if (err instanceof ReplyError && this.condition.select === db) { | ||
| this.condition.select = previousDb; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
💡 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".
| this.selectsSent === selectsSent && | ||
| this.connectionEpoch === connectionEpoch | ||
| ) { | ||
| this.condition.select = previousDb; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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].
There was a problem hiding this comment.
💡 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".
| const db = parseInt(command.args[0], 10); | ||
| const selectsSent = ++this.selectsSent; | ||
| if (this.condition.select !== db) { | ||
| const previousDb = this.condition.select; |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
@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.
|
Done, the PR is back to cac13bb with the single test. Thanks for the review! |
31bcb19 to
cac13bb
Compare
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.
Reviewed by Cursor Bugbot for commit 001c335. Configure here.
## [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))
|
🎉 This PR is included in version 6.1.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |

Closes #2307
When Redis rejects a
SELECT(e.g. an out-of-range index),condition.selectkept 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.selectis still updated when theSELECTis sent, so commands issued right afterselect()keep tracking the new db. If Redis answers with an error reply (ReplyError), it is set back to the previous db, unless a laterSELECThas changed it in the meantime. Other rejections (commandTimeout, a closed connection) keep the current behaviour, since theSELECTmay still have run there. The connect-timeSELECTfrom thedboption is unchanged.The new functional test in
test/functional/select.tsrejects aSELECTon a client created withdb: 2, forces a reconnect and checks withCLIENT INFOthat the connection is still on db 2. It fails on main and passes with the fix.Note
Medium Risk
Touches core
sendCommanddb 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 updatescondition.selectwhen the command is sent, but on aReplyErrorit 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(), checkscondition.selectstays on the configured db, and verifies after reconnect viaCLIENT INFOthat 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.