fix: invalidate stale predicate-scoped cache entries on manual-sync writes#1497
Open
marbemac wants to merge 1 commit intoTanStack:mainfrom
Open
fix: invalidate stale predicate-scoped cache entries on manual-sync writes#1497marbemac wants to merge 1 commit intoTanStack:mainfrom
marbemac wants to merge 1 commit intoTanStack:mainfrom
Conversation
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
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.
🎯 Changes
Fixes a cache-poisoning bug in
@tanstack/query-db-collection(syncMode: 'on-demand') where any manual-sync write (writeInsert/writeUpdate/writeDelete/writeUpsert/writeBatch) overwrote every cache entry under the collection'squeryKeywith the full post-writesyncedDatasnapshot — regardless of the predicate that originally produced each entry.For stale entries (no live observer) within
gcTime, this stamped them with rows that didn't satisfy their originalwhere. A subsequent cache-hit re-subscribe re-applied those wrong rows viaapplySuccessfulResult, the per-subscriptionwherefilter discarded them, and the subscriber received[]for predicates whose matching rows still existed in the source. In our app this surfaced as "not found" errors on rows that demonstrably existed in Postgres, with no exception thrown anywhere in tanstack-db.Fix: stale cache entries are now invalidated (
removeQueries) instead of overwritten, so the next subscribe re-runsqueryFnagainst the source of truth. Active entries continue to receive the full snapshot — predicate scoping for the consumer is enforced downstream bysubscribeChanges'swhere. The original ghost-row protection is preserved because deleted rows no longer reappear from a stale snapshot.Three new tests under
Predicate-scoped cache invalidation on manual writescover the headline bug, the positive case (newly inserted matching rows visible on re-subscribe), and overlapping predicates (a write into a broader active scope must not poison a narrower stale entry). Verified by stashing the src fix that two of the three tests fail without it.✅ Checklist
pnpm test.🚀 Release Impact