Skip to content

Fix/live query record drop on subset unmount#1656

Open
jdday wants to merge 2 commits into
TanStack:mainfrom
jdday:fix/live-query-record-drop-on-unmount
Open

Fix/live query record drop on subset unmount#1656
jdday wants to merge 2 commits into
TanStack:mainfrom
jdday:fix/live-query-record-drop-on-unmount

Conversation

@jdday

@jdday jdday commented Jul 8, 2026

Copy link
Copy Markdown

🎯 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.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

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 fb9fa21 and 5707c50.

📒 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 name Status Explanation
Title check ✅ Passed The title clearly summarizes the live query record-drop fix on subset unmount.
Description check ✅ Passed The description follows the template and includes changes, testing, and release impact.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check 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.

@jdday jdday force-pushed the fix/live-query-record-drop-on-unmount branch from 775e89d to 66f109f Compare July 8, 2026 06:48
…w updates, as it was previously reading stale owners before any updates causing rows contained within 2 subsets to be removed on unmount
@jdday jdday force-pushed the fix/live-query-record-drop-on-unmount branch from 66f109f to fb9fa21 Compare July 8, 2026 06:49
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.

1 participant