Skip to content

fix(client): select the default database in reset() - #3510

Merged
nkaradzhov merged 2 commits into
redis:masterfrom
maxymlyskov:fix/select-default-database-in-reset
Oct 14, 2026
Merged

nkaradzhov merged 2 commits into
redis:masterfrom
maxymlyskov:fix/select-default-database-in-reset

Conversation

@maxymlyskov

@maxymlyskov maxymlyskov commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

reset() says it selects the default DB, but after select() it leaves the connection on the selected one. It queues RESET and then runs the handshake, which sends SELECT <#selectedDB> whenever #selectedDB !== 0 (index.ts#L997-L999). #selectedDB only goes back to the database option (or 0) after Promise.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: releaseClientLease calls resetIfDirty(), which compares #selectedDB with the default, so a lease that called select() 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:

### base run 1 MODE=reset
after reset: server-side db 1 (expected 0 )
  h2a:reset:k found in db 1
### base run 1 MODE=option-db
after reset: server-side db 1 (expected 2 )
### fix run 1 MODE=reset
after reset: server-side db 0 (expected 0 )
  h2a:reset:k found in db 0
### fix run 1 MODE=option-db
after reset: server-side db 2 (expected 2 )

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 #selectedDB before the handshake, and again after Promise.all so a select() still in flight when reset() starts does not leave its db recorded. The handshake then selects the database option, 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

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

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 prior select(). The post-RESET handshake was reading stale #selectedDB, so it could issue SELECT for the old index again while the client later recorded the default—breaking CLIENT INFO, reconnect behavior, and pool/sentinel resetIfDirty() checks.

reset() now sets #selectedDB to the configured default (options.database or 0) before the handshake so the queued SELECT matches intent, and assigns it again after Promise.all so a select() still in flight when reset runs cannot permanently overwrite the default.

Adds integration tests for default-db reset, database option on reset, and the in-flight select() race.

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

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.
@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 ⚠️ Failed 2026-10-13T20:55:15.001167Z 6ecc3f6 PR opened
ℹ️ 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.

@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 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 6ecc3f6. Configure here.

Comment thread packages/client/lib/client/index.ts Resolved
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).

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, LGTM.

@nkaradzhov
nkaradzhov merged commit 690ac8a into redis:master Oct 14, 2026
14 of 15 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