Skip to content
Source
Sign inSign up
TanStack/db

Fix/live query record drop on subset unmount #1656

Closedjdday(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1656/headopened

🎯 Changes

When a row was first inserted by subset query A, write({ type: 'insert' }) cleared the row's pending metadata. setPersistedOwners was called before write, so the ownership it set was immediately overwritten by the insert's metadata-delete.

When a later live query called getHydratedOwnedRowsForQueryBaseline, it read the stale persisted state and overwrote the in-memory rowToQueries map, dropping query A's hash from the owner set. When that query was GC'd, it found zero remaining owners and deleted the row — removing it from unrelated list views.

✅ Checklist

  • [ x ] I have tested this code locally with pnpm test.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Notes

Currently facing the below issue with react-db and I believe the below re-ordering of lines fixes it for myself but need to understand the impact elsewhere or if there is a better place to add this fix.

  • Live query A contains a list of rows
  • Live query B contains a subset of those rows with a different where clause
  • When Live Query B unmounts then the rows are removed from the collection completely even with Live Query A still mounted and requires those rows.,

Any new live queries seem to take ownership of existing rows when mounting. Meaning that when the new live query unmounts it causes that record to be removed from the collection even if other useLiveQueries still should have that row existing.

I am unsure if this is a similar impact across the other vue/svelte db packages I am currently only using react-db so any advise if this change would negatively impact other areas would be helpful.

One thing noticed - my team have observed that the row will re-appear in the correct live query if we force a re-render during local development

Summary by CodeRabbit

  • Bug Fixes
    • Corrected record syncing to ensure existing rows receive the appropriate update behavior rather than being skipped.
    • Fixed ownership tracking to re-apply ownership after writes, improving consistency and preventing unintended row deletion during sibling live query garbage collection.
  • Tests
    • Added coverage confirming that unmounting a narrowly filtered useLiveQuery does not remove records from another still-mounted, unfiltered list useLiveQuery.
    1. Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 8f2c6426-543f-446f-b2f6-d1e4a9d48fa8

      📥 Commits

      Reviewing files that changed from the base of the PR and between fb9fa21fcfcb2a3cab87f2c8b0dc356d2971730a and 5707c505418d17816cf56012a082a7dcda1e9802.

      📒 Files selected for processing (1)
      • .changeset/evil-maps-shine.md
      ✅ Files skipped from review due to trivial changes (1)
      • .changeset/evil-maps-shine.md

      📝 Walkthrough

      Walkthrough

      The PR reorders row tracking and ownership persistence in applySuccessfulResult, and adds a regression test showing that unmounting a narrow useLiveQuery does not remove records from a still-mounted broad query.

      Changes

      Query Collection Ownership Fix

      Layer / File(s)Summary
      Row writes and ownership persistence
      packages/query-db-collection/src/query.ts, .changeset/evil-maps-shine.md
      applySuccessfulResult now adds the row before write selection, chooses insert versus update from current synced state and prior ownership, re-reads persisted owners after the write, and the changeset records the minor release and fix note.
      Concurrent useLiveQuery regression test
      packages/react-db/tests/useLiveQuery.test.tsx
      Adds a test that mounts broad and narrow queries over one collection, unmounts the narrow query, waits for GC, and verifies the broad query still retains all items.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Possibly related PRs

      • TanStack/db#1626: Similar ownership-metadata handling around inserts and persisted row state.

      Suggested reviewers: kevin-dp

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly summarizes the live query record-drop fix on subset unmount.
      Description check✅ PassedThe description follows the template and includes changes, testing, and release impact.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. Closing as superseded after RFC #1657 Phase 0 review. The immediate persisted-metadata clobber described here was addressed more fundamentally by #1626, and #1664 has since refactored the ownership paths touched by this branch. The added React test uses mockSyncCollectionOptions and does not exercise query-db ownership. Broader ownership, provider-lifetime, and destructive-diff semantics should be handled by RFC #1657 PR-5's coverage registry rather than landing this partial ownership expansion. A genuine query-db-level red/green regression test can still be harvested separately if it exposes remaining behavior. Thank you for the investigation and contribution.

Sign in to join this conversation.