fix(svelte-query): untrack observer subscription #11542
main fromrefs/pull/11542/head·opened Fixes #11541
🎯 Changes#
For queries without cached data, queryFn executes synchronously inside the subscription $effect (onSubscribe → shouldFetchOnMount → fetch). Any reactive state read by queryFn before its first await was therefore recorded as a dependency of that effect. Writes to that state tore the observer down (cancelling the in-flight fetch) and re-subscribed — and since the query stays dataless, every re-subscription starts a new fetch: an unbounded teardown/refetch loop. See #11541 for the full mechanism and the exact set of conditions that sustain it.
The fix keeps the observer read tracked, but runs the subscription itself inside untrack():
$effect(() => {
const o = observer
const unsubscribe = isRestoring.current
? () => undefined
: untrack(() => o.subscribe(() => update(createResult())))
return unsubscribe
})
Note: the naive untrack(() => observer.subscribe(...)) is not enough — it also untracks the observer read, so changing queries would no longer re-subscribe. The existing test “should track queries added to an initially empty array” catches that case, which is why the observer read stays outside the untrack().
Applied to both createBaseQuery.svelte.ts (also covers createInfiniteQuery) and createQueries.svelte.ts.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Prevented reactive state updates during an in-flight query from unnecessarily restarting subscriptions or cancelling fetches.
- Query functions that read state before their first asynchronous wait now complete reliably without repeated refetches.
Tests
- Added regression coverage for single-fetch behavior and successful completion in both individual and multiple-query scenarios.
- 1
gzuuus(GitHub profile, opens in a new tab) linked this to fix #11541
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
b74bcc53-e2d1-41a7-a2b3-236bac9fc3b0📥 Commits
Reviewing files that changed from the base of the PR and between d346e85f0144eb9b991e6e1665b22f4574de9751 and 23136f400857a9a031b45771bc09f8b4fc77c8a5.
📒 Files selected for processing (5)
.changeset/svelte-query-untrack-subscribe.mdpackages/svelte-query/src/createBaseQuery.svelte.tspackages/svelte-query/src/createQueries.svelte.tspackages/svelte-query/tests/createQueries/createQueries.svelte.test.tspackages/svelte-query/tests/createQuery/createQuery.svelte.test.tsIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
The Svelte Query subscription effects now execute observer subscriptions inside
untrack(). Observer reads remain tracked. Regression tests cover reactive state access during asynchronous query functions, and a patch changeset records the fix.Changes#
Svelte Query subscription fix
packages/svelte-query/src/createBaseQuery.svelte.ts,packages/svelte-query/src/createQueries.svelte.tssubscribeinsideuntrack().packages/svelte-query/tests/createQuery/createQuery.svelte.test.ts,packages/svelte-query/tests/createQueries/createQueries.svelte.test.ts,.changeset/svelte-query-untrack-subscribe.md@tanstack/svelte-query.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
sukvvonMerge Risk: ⚪ Minimal · up to
23136The subscription change preserves query reactivity and cleanup while preventing reactive query-function reads from causing refetch loops. No actionable merge-blocking risk remains.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
untrack().#11541].createBaseQuery.svelte.tsreadsobserverintoobefore it runso.subscribe(...)insideuntrack(). The observer read remains tracked, so …#11541]. The pull request changes only observer subscription tracking for the affected Svelte Query paths. N…✨ Finishing Touches
🧪 Generate unit tests (beta)
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