Skip to content
Source
Sign inSign up
TanStack/query

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

Mergedlazerg(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/11172/headmerged 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. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      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: 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.md
      • packages/query-core/src/__tests__/mutationObserver.test.tsx
      • packages/query-core/src/mutationObserver.ts
      🚧 Files skipped from review as they are similar to previous changes (3)
      • packages/query-core/src/mutationObserver.ts
      • packages/query-core/src/tests/mutationObserver.test.tsx
      • .changeset/khaki-hoops-shake.md

      Included review availability: Your plan includes up to 10 reviews per rolling hour; 3 remain after this review.


      📝 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

      Merge Risk: ⚪ Minimal · up to 130f8

      This 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)

      sequenceDiagram
        participant Subscriber
        participant MutationObserver
        participant Mutation
        Subscriber->>MutationObserver: subscribe
        MutationObserver->>Mutation: reattach current mutation
        MutationObserver->>MutationObserver: refresh current result
        Mutation->>MutationObserver: notify completion
        MutationObserver->>Subscriber: deliver successful result
      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary fix: reattaching MutationObserver to its mutation during onSubscribe.
      Description check✅ PassedThe description covers the cause, fix, tests, verification, checklist, release impact, and linked issue.
      Linked Issues check✅ PassedThe implementation and tests address issue #11171 by restoring MutationObserver updates after resubscription during or after a mutation.
      Out of Scope Changes check✅ PassedThe source change, regression tests, and changeset are directly related to the linked issue and stated pull request objectives.
      Docstring Coverage✅ PassedNo 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

      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.
    2. TkDodo approved these changes

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

    4. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 130f8de8ba218cfea4ba2eb6b28df4d9eef07a7c

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded4m 59sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 52sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-08-18 08:08:43 UTC

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

    6. More templates

      @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@11172

      commit: 130f8de

    7. TkDodo merged this18c1c1e

    8. TkDodo closed this

Sign in to join this conversation.