Skip to content
Source
Sign inSign up
TanStack/store

fix(store): clean up dependencies of unobserved computed atoms on get() to avoid memory leak #380

Closedgrzdev(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/380/headopened

Summary

Reading a computed atom once via .get() without an active subscription causes its source atoms to retain a strong reference in their subscriber list (subs).

If the computed atom is never subscribed to (such as during a component render before an aborted or suspended render cycle), alien-signals never invokes unwatched() because atom.subs was never populated. As a result, the source atom holds a strong reference to the discarded computed atom indefinitely, preventing garbage collection and leaking any state captured in the getter closure.

Changes

  • When a computed atom evaluates via atom.get() outside of an active reactive computation (activeSub === undefined), if atom.subs === undefined (no active observers), clean up its dependencies via purgeDeps(atom) and reset its flags to MUTABLE | DIRTY.
  • When an active subscriber or computation effect reads the atom, the dependency link is preserved normally.
  • If the computed atom is read again later or subscribed to, it recomputes and re-establishes dependency links.
  • Add regression unit test in packages/store/tests/observer-isolation.test.ts.
  • Add changeset for @tanstack/store.

Summary by CodeRabbit

  • Bug Fixes
    • Reading computed values outside an active reactive subscription no longer causes unnecessary dependency tracking to remain in memory. This reduces memory retention for one-off reads, while subsequent reads continue to reflect changes to the source value. Computed values that are actively subscribed continue to behave as before.
    1. 🦋 Changeset detected

      Latest commit: 95639772e53506721726205a0454887118e466d5

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 9 packages
      NameType
      @tanstack/storePatch
      @tanstack/angular-storePatch
      @tanstack/lit-storePatch
      @tanstack/octane-storePatch
      @tanstack/preact-storePatch
      @tanstack/react-storePatch
      @tanstack/solid-storePatch
      @tanstack/svelte-storePatch
      @tanstack/vue-storePatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: defaults
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 2b0b3c52-8842-4216-b2d8-66ff9ee20384
      📥 Commits

      Reviewing files that changed from the base of the PR and between c1a40d47e2c5e8a1ca30225771870bad8aacc0c2 and 95639772e53506721726205a0454887118e466d5.

      📒 Files selected for processing (3)
      • .changeset/unwatched-atom-cleanup.md
      • packages/store/src/atom.ts
      • packages/store/tests/observer-isolation.test.ts

      Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


      📝 Walkthrough

      Walkthrough

      Computed atom reads without an active subscriber now clear dependencies when the atom has no subscribers. A regression test checks that the source remains unsubscribed and the derived value updates after a source change. A patch changeset records the cleanup.

      Changes

      Unobserved computed atom cleanup

      Layer / File(s)Summary
      Computed atom read cleanup
      packages/store/src/atom.ts, packages/store/tests/observer-isolation.test.ts, .changeset/unwatched-atom-cleanup.md
      When a computed atom has no active subscriber and no subscribers of its own, the read path clears its dependency tail, resets its flags, and purges its dependencies. The regression test checks that the source has no subscriber collection before or after an update and that the derived value changes from 1 to 11. The changeset records a patch release.

      Priority: ⬇️ Low

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

      Change: Bug fix

      Suggested reviewers: sheraff

      Merge Risk: ⚪ Minimal · up to 95639

      The cleanup avoids retaining dependencies after unobserved reads while preserving correct values on later reads and subscriptions. No specific merge-blocking risk remains.

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 …Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely identifies the main store fix: cleaning up dependencies of unobserved computed atoms on get() to prevent a memory leak.
      Description check✅ PassedThe description explains the problem, root cause, implementation, expected behavior, regression test, and changeset. It does not use the exact template headings or provide explicit checklist confirmat…
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      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.
    3. grzdev(GitHub profile, opens in a new tab)commented

      Closing in favor of existing open work in https://github.com/TanStack/store/pull/376.

Sign in to join this conversation.