Skip to content

fix(client): empty the client-side cache when SELECT is issued, not on its reply - #3661

Closed
Cdineshreddy12 wants to merge 1 commit into
redis:masterfrom
Cdineshreddy12:fix/csc-select-race
Closed

Cdineshreddy12 wants to merge 1 commit into
redis:masterfrom
Cdineshreddy12:fix/csc-select-race

Conversation

@Cdineshreddy12

@Cdineshreddy12 Cdineshreddy12 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Cache keys do not include the database, so a cacheable read issued after select(db) but before its reply was served the previous database's cached value: handleCache() consults the cache synchronously at command time, while the empty-on-database-change ran only once the SELECT reply arrived (#setSelectedDB).

await client.get('key');  // caches key = db-0 value
client.select(1);         // issued, reply not yet back
await client.get('key');  // served the stale db-0 value from cache

This empties the cache the moment SELECT is issued, and only when the database actually changes. #setSelectedDB still empties it on the reply for the pipeline/MULTI SELECT paths, which cannot invalidate at issue time. Selecting the already-selected database leaves the cache intact.

Same cross-database concern as #3641 (which empties the cache on a database change) — this closes the residual window where a read races the in-flight SELECT.

Added a unit test (never-connected client) asserting the cache clears synchronously at issue time rather than on the reply.


Note

Medium Risk
Changes client-side caching correctness around database switches; limited scope but can affect apps that interleave SELECT with cached reads.

Overview
Fixes a stale client-side cache race: cache keys omit the database index, so a cacheable read issued after select(newDb) but before the SELECT reply could still return a value cached from the previous database.

SELECT now calls invalidate(null) synchronously when the target database differs from the current one, before sendCommand runs. Selecting the database that is already active leaves the cache unchanged. #setSelectedDB on the reply still clears the cache for pipeline/MULTI SELECT paths that cannot invalidate at issue time.

Adds a unit test using a never-connected client to assert the cache is empty immediately after select() is issued, without waiting for a reply.

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

…n its reply

Cache keys do not include the database, so a cacheable read issued after
select(db) but before its reply was served the previous database's cached
value: handleCache() consults the cache synchronously at command time, while
#setSelectedDB — which empties the cache on a database change — only ran once
the SELECT reply arrived.

Empty the cache the moment SELECT is issued, only when the database actually
changes. #setSelectedDB still empties it on the reply for the pipeline/MULTI
SELECT paths, which cannot invalidate at issue time. Selecting the
already-selected database leaves the cache intact.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T14:12:26.022784Z f0bb6b2 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.

@nkaradzhov

Copy link
Copy Markdown
Collaborator

Closing this. The race only shows up when a read is sent before select() has resolved, and we have no report of it in a real application. If you hit it in a real use case, please open an issue that describes it, and we can take it from there.

@nkaradzhov nkaradzhov closed this Nov 23, 2026
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