fix(query-core): reattach 'MutationObserver' to its mutation in 'onSubscribe' #11172
main fromrefs/pull/11172/head·merged by TkDodo View on GitHub🎯 Changes#
MutationObserver.onUnsubscribe() detaches the observer from its running mutation once the last listener goes away, and nothing ever put it back when a listener subscribed again. React runs that unsubscribe/resubscribe cycle on a component that keeps its state: <Activity mode="hidden"> and re-suspending boundaries in production, StrictMode in development. When it happens mid-mutation the mutation still settles normally in the MutationCache, but the detached observer never hears about it, so getCurrentResult() keeps handing its frozen pending snapshot to useSyncExternalStore. isPending stays true forever and the cache and the UI disagree permanently.
onSubscribe() now re-adds the observer to its current mutation and refreshes the snapshot, which is what QueryObserver already does for queries. Two cases are covered in mutationObserver.test.tsx: resubscribing while the mutation is still running, and resubscribing after it already settled.
Fixes #11171
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
Local verification detail: both new tests fail on main (frozen at status: 'pending') and pass with the fix; pnpm run test:pr is green across 96 projects.
🚀 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
- Fixed mutation results remaining stuck in a pending state after subscriptions are removed and restored.
- Resubscribing now correctly reconnects to the active mutation and receives its final result, including successful completions.
Tests
- Added coverage for resubscribing during in-progress and completed mutations.
- 1
TkDodo approved these changes
Note
GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.
View your CI Pipeline Execution ↗ for commit 130f8de8ba218cfea4ba2eb6b28df4d9eef07a7c
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 4m 59s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 1m 52s View ↗
☁️ Nx Cloud last updated this comment at
2026-08-18 08:08:43UTCNote
GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.
More templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-preact-simple
- @tanstack/query-example-lit-basic
- @tanstack/query-example-lit-pagination
- @tanstack/query-example-lit-ssr
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-eslint-plugin-demo
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-optimistic-updates
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-offline
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@11172@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@11172@tanstack/lit-query
npm i https://pkg.pr.new/@tanstack/lit-query@11172@tanstack/preact-query
npm i https://pkg.pr.new/@tanstack/preact-query@11172@tanstack/preact-query-devtools
npm i https://pkg.pr.new/@tanstack/preact-query-devtools@11172@tanstack/preact-query-persist-client
npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@11172@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@11172@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@11172@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@11172@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@11172@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@11172@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@11172@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@11172@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@11172@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@11172@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@11172@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@11172@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@11172@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@11172@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@11172@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@11172@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@11172@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@11172@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@11172commit:
130f8deTkDodo merged this18c1c1e
TkDodo closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
b40b321e-8e7f-4ede-ad0c-8c8c76252533📥 Commits
Reviewing files that changed from the base of the PR and between 6e3d521fe54f78624e53c1c6f9cadd678504bee8 and 130f8de8ba218cfea4ba2eb6b28df4d9eef07a7c.
📒 Files selected for processing (3)
.changeset/khaki-hoops-shake.mdpackages/query-core/src/__tests__/mutationObserver.test.tsxpackages/query-core/src/mutationObserver.ts🚧 Files skipped from review as they are similar to previous changes (3)
Included review availability: Your plan includes up to 10 reviews per rolling hour; 3 remain after this review.
📝 Walkthrough
Walkthrough#
MutationObservernow reattaches its current mutation when a listener subscribes again. Tests cover in-flight and settled mutations, and a patch changeset documents the behavior.Changes#
Mutation observer resubscription
packages/query-core/src/mutationObserver.tsMutationObserver.onSubscribe()re-registers the current mutation for the first listener and refreshes the current result.packages/query-core/src/__tests__/mutationObserver.test.tsx,.changeset/khaki-hoops-shake.md@tanstack/query-core.Estimated code review effort: 2 (Simple) | ~10 minutes
Merge Risk: ⚪ Minimal · up to
130f8This localized fix reconnects mutation observers after resubscription and adds coverage for the affected lifecycle cases; no actionable merge-blocking risk remains beyond normal checks and review.
Sequence Diagram(s)#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#11171by restoring MutationObserver updates after resubscription during or after a mutation.✨ 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