Skip to content

fix(query-core): reattach 'MutationObserver' to its mutation in 'onSubscribe' - #11172

Open
lazerg wants to merge 1 commit into
TanStack:mainfrom
lazerg:fix/issue-11171-mutation-observer-resubscribe
Open

fix(query-core): reattach 'MutationObserver' to its mutation in 'onSubscribe'#11172
lazerg wants to merge 1 commit into
TanStack:mainfrom
lazerg:fix/issue-11171-mutation-observer-resubscribe

Conversation

@lazerg

@lazerg lazerg commented Aug 12, 2026

Copy link
Copy Markdown

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

@coderabbitai

coderabbitai Bot commented Aug 12, 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 Plus

Run ID: 27e2591d-13ea-419e-b46c-5560cabde254

📥 Commits

Reviewing files that changed from the base of the PR and between 159982c and 3b9a125.

📒 Files selected for processing (3)
  • .changeset/khaki-hoops-shake.md
  • packages/query-core/src/__tests__/mutationObserver.test.tsx
  • packages/query-core/src/mutationObserver.ts

📝 Walkthrough

Walkthrough

MutationObserver now 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

Layer / File(s) Summary
Restore mutation attachment
packages/query-core/src/mutationObserver.ts
MutationObserver.onSubscribe() re-registers the current mutation for the first listener and refreshes the current result.
Validate resubscription results
packages/query-core/src/__tests__/mutationObserver.test.tsx, .changeset/khaki-hoops-shake.md
Tests verify successful results after resubscription during in-flight and settled mutations. The changeset records a patch release for @tanstack/query-core.

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

Possibly related PRs

  • TanStack/query#11120: Modifies MutationObserver.onSubscribe() and related tests to reconnect the current mutation on resubscription.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary fix: reattaching MutationObserver during onSubscribe.
Description check ✅ Passed The description covers the change, motivation, testing, checklist, release impact, and linked issue required by the template.
Linked Issues check ✅ Passed The implementation and tests directly address issue #11171 by restoring MutationObserver updates after resubscription.
Out of Scope Changes check ✅ Passed The changeset, implementation update, and regression tests are all directly related to the linked issue and stated objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

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.

useMutation freezes at pending forever when React re-subscribes during an in-flight mutation (<Activity> hide/show)

1 participant