Repository navigation
fix(client): select the default database in reset() - #3510
Merged
nkaradzhov merged 2 commits intoOct 14, 2026
Merged
Conversation
reset() assigned the default database to #selectedDB only after RESET and the handshake had run. The handshake reads #selectedDB, so it sent SELECT with the database chosen by select() right after RESET, and the connection stayed there while the client recorded the default. The next reconnect then moved it to the default without notice. Assign #selectedDB before the handshake, so it selects the database option, or stays on db 0 after RESET.
Codex Review SummaryThis 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.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 6ecc3f6. Configure here.
Cursor Bugbot pointed out that a select() sent before reset() resolves after reset() has recorded the default, so the client kept that db while RESET left the connection on the default one, and the next reconnect moved it. reset() now records the default once more after its commands settle. The new test fails without this line (1 !== 0).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
reset()says it selects the default DB, but afterselect()it leaves the connection on the selected one. It queues RESET and then runs the handshake, which sendsSELECT <#selectedDB>whenever#selectedDB !== 0(index.ts#L997-L999).#selectedDBonly goes back to thedatabaseoption (or 0) afterPromise.all(promises)(reset()), so the handshake selects the old db again right after RESET.So the client records the default while the connection stays on the old db, and the next reconnect moves it without notice. Sentinel leases hit this too:
releaseClientLeasecallsresetIfDirty(), which compares#selectedDBwith the default, so a lease that calledselect()is returned on the wrong db and looks clean.Probe against Redis 8.10.2 in Docker with the built client: select(1), reset(), CLIENT INFO, write a key. The option-db client has
database: 2. All 3 runs printed the same thing:A raw
sendCommand(['RESET'])leaves db 0 on both builds, so RESET itself works and the extra SELECT comes from the handshake.The fix assigns
#selectedDBbefore the handshake, and again afterPromise.allso aselect()still in flight whenreset()starts does not leave its db recorded. The handshake then selects thedatabaseoption, or sends no SELECT and RESET leaves db 0.Tests: 'should select the default database on reset' and 'should select the database option on reset', next to 'should remember selected db'. They fail on master (
1 !== 0,0 !== 2) and pass here.The client suite passes on Linux apart from 4 sentinel and multi-db specs that don't go through
reset(): 3 fail on master too and 1 is a before-each hook timeout that passes alone. eslint is clean on the changed files.Checklist
npm testpass with this change (including linting)?Note
Medium Risk
Changes connection database bookkeeping in reset() and handshake ordering; wrong behavior could misroute keys across DBs, but the change is narrow and covered by new integration tests.
Overview
Fixes
reset()leaving the Redis connection on the wrong logical database after a priorselect(). The post-RESEThandshake was reading stale#selectedDB, so it could issueSELECTfor the old index again while the client later recorded the default—breakingCLIENT INFO, reconnect behavior, and pool/sentinelresetIfDirty()checks.reset()now sets#selectedDBto the configured default (options.databaseor 0) before the handshake so the queuedSELECTmatches intent, and assigns it again afterPromise.allso aselect()still in flight when reset runs cannot permanently overwrite the default.Adds integration tests for default-db reset,
databaseoption on reset, and the in-flightselect()race.Reviewed by Cursor Bugbot for commit 95f3579. Bugbot is set up for automated code reviews on this repo. Configure here.