Repository navigation
fix(client): empty the client-side cache when the selected database changes - #3641
Conversation
…hanges Cache keys are built from command arguments only and invalidation messages carry just the key name, so after select() a cached read kept serving the previous database's value until something wrote that key name. A new #setSelectedDB helper records the database and empties the cache when the value changed; it is used by select(), SELECT in a pipeline and SELECT in multi(). reset() always empties the cache, even when the database does not change, because RESET turns client tracking off on the server, so entries cached before it would never get invalidated. Raw sendCommand(['SELECT', ...]) is out of scope, as discussed in redis#3631. Fixes redis#3631
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: e4598a4c15
ℹ️ 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".
| // RESET turns client tracking off on the server, so entries cached before it | ||
| // would never get invalidated: always empty the cache, even when the | ||
| // selected database does not change. | ||
| this._self.#clientSideCache?.invalidate(null, this._self._clientId); |
There was a problem hiding this comment.
Invalidate pending loads before clearing the shared cache
When two RESP3 clients share BasicPooledClientSideCache, calling reset() on A while B has a GET in flight can now poison the cache even though both remain on DB 0. invalidate(null) calls clear(false), which drops the maps without invalidating the pending entry object. If B's delayed read reply and a concurrent write's invalidation are decoded together afterwards, the invalidation cannot find the removed index; handleCache() then re-inserts the obsolete reply because cacheEntry.validate() still succeeds. A local RESP3 transport repro returned fresh data on the parent commit versus stale cache hits here, potentially indefinitely with ttl: 0. Invalidate outstanding entries or use a cache-generation guard before clearing.
Useful? React with 👍 / 👎.
A load in flight while clear() runs holds its promise entry, and handleCache() re-validates that entry before saving the loaded value, so a reply from before the clear could re-insert its value into the emptied cache. Invalidating the entries before dropping the maps makes the revalidation fail, the same way per-key invalidation already does.
|
Good catch from the bot — clear() dropped the maps but left in-flight promise entries valid, so a load racing the clear could re-insert its stale value afterwards (handleCache re-validates the entry before saving). Pushed a commit that invalidates the entries before dropping the maps, with a unit test that fails without it. That window predates this PR (any server-initiated null invalidation cleared the same way), but this PR adds callers, so fixing it here. |
Fixes #3631
Implements what @nkaradzhov laid out in the issue: empty the cache when the selected database changes, instead of putting the database in the cache key or rejecting SELECT.
#setSelectedDBhelper that records the db and, only when the value actually changed, callsthis.#clientSideCache?.invalidate(null, this._clientId)— so selecting the already-selected database keeps the cacheselect(), the pipeline path and themulti()pathreset()always empties the cache, even when the database doesn't change, since RESET turns client tracking off server-side and entries cached before it would never get invalidatedsendCommand(['SELECT', ...])left out of scope as discussedTests: the repro from the issue as a spec plus the pipeline,
multi()andreset()variants @nkaradzhov asked for, and one check that selecting the current database doesn't drop the cache. One heads-up: I couldn't run the spec suite locally — the test harness spawns containers with--network host, which doesn't expose ports to the host on Docker for Mac, and times out before any test runs (pre-existing specs do the same on my machine). I verified the change against a real Redis 8 instead by running the issue's repro script plus the pipeline/multi/reset variants against the patched source: before the fix the secondget()returnsvalue in db 0with a cache hit, after it returnsvalue in db 1, and the cache size drops to 0 on pipeline-SELECT, multi-SELECT and reset(). Happy to adjust if CI turns anything up.Note
Medium Risk
Changes core client-side caching behavior on SELECT and RESET paths; incorrect invalidation could cause stale reads or extra cache misses, but scope is limited to CSC users on RESP3.
Overview
Fixes #3631 by clearing the client-side cache when the active Redis database changes, since cache keys omit the DB and server invalidations only name keys.
A new
#setSelectedDBpath records the selected DB and calls full cache invalidation only when the DB actually changes—wired intoselect(), successfulSELECTin pipelines, andSELECTinsidemulti(). Re-selecting the same database leaves the cache intact.reset()always empties the cache becauseRESETdisables server tracking, so pre-reset entries would never invalidate.BasicClientSideCache.clear()now invalidates every entry before dropping map state, so an in-flight load cannot passvalidate()after a clear and write a stale value back.New specs cover
clear()vs in-flight promises andselect/ pipeline / multi / reset` database-change behavior.Reviewed by Cursor Bugbot for commit b3641a7. Bugbot is set up for automated code reviews on this repo. Configure here.