Fixes #11171
Problem#
When the last listener unsubscribes, MutationObserver.onUnsubscribe() removes the observer from its current mutation — but resubscribing never adds it back. React legitimately tears down and re-establishes the store subscription of a mounted component that keeps its state (<Activity mode="hidden">, re-suspending <Suspense> boundaries, StrictMode in dev).
If that happens while a mutation is in flight, the mutation settles against an empty observer list, and the observer keeps serving its frozen pending snapshot from getCurrentResult() forever:
isPending stays true → submit buttons using disabled={isPending} stay disabled until remount
- callbacks passed to
mutate(vars, { onSuccess }) never fire
- devtools/
MutationCache show success while the hook shows pending
Fix#
Mirror QueryObserver.onSubscribe(): when the first listener subscribes, re-attach the observer to its current mutation (Mutation.addObserver is already idempotent) and refresh the result snapshot in case the mutation settled while detached.
Tests#
Two regression tests in mutationObserver.test.tsx, driving subscribe/unsubscribe the way useSyncExternalStore does:
- unsubscribe + resubscribe while the mutation is in flight → observer reaches
success and notifies the new listener
- mutation settles while fully unsubscribed → resubscribing refreshes the snapshot to the final state
Both fail on main and pass with the fix. Full query-core (627) and react-query (577) suites pass.
Summary by CodeRabbit#
Bug Fixes
- Mutation status and results now refresh correctly when listeners unsubscribe and later resubscribe.
- In-progress mutations remain tracked across subscription changes.
- Mutations that finish while no listeners are present now report their final success state when observed again.
Tests
- Added coverage for mutation observer behavior across unsubscribe and resubscribe cycles.
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:
377ea04e-9e0b-45f6-a739-ef44e9c79882📥 Commits
Reviewing files that changed from the base of the PR and between 279bb5508b81ce0d88b8f0f72441d96abf7ca03c and 969cdeb4ff926c33986f90b0f47f05fca901ef7d.
📒 Files selected for processing (2)
packages/query-core/src/__tests__/mutationObserver.test.tsxpackages/query-core/src/mutationObserver.tsIncluded review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
MutationObserver now reattaches to its current mutation and refreshes its result after resubscription. Tests cover mutations completing during and after unsubscribed periods.
Changes#
Mutation observer recovery
packages/query-core/src/mutationObserver.ts,packages/query-core/src/__tests__/mutationObserver.test.tsxMutationObserver.onSubscribe()reattaches the first listener to the current mutation and refreshes the current result. Tests cover in-flight mutations and mutations that settle while unsubscribed.Estimated code review effort: 2 (Simple) | ~10 minutes
Merge Risk: ⚪ Minimal · up to
969cdThis localized change re-attaches mutation observation across subscription resubscription and adds regression coverage; no actionable merge-blocking risk remains beyond normal checks and review.
Suggested reviewers:
sukvvon🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#11171and covering both required resubscription scenarios with tests.#11171.✨ 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