Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Reading a computed atom without subscribing can prevent garbage collection

未关闭
#372 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Sheraff 已经在做这个了。

开始于 2026年9月28日。

  • #373 来自 @Sheraff —— 未关闭
  • #376 来自 @Sheraff —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
58/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript

调研方向

Start with the dependency-tracking logic in atom.ts and run the supplied repro.mjs with Node's --expose-gc flag. Trace how a computed atom read by get() is connected to its source without a subscription. Done means dropped, never-subscribed computed atoms can be collected while the source remains in use, with the reported behavior covered by a regression test.

由索引模型根据 Issue 内容生成。

描述

A computed atom can stay in memory after the application has finished using it. Reading it once with .get() is enough: its source keeps a reference to it, even if nothing ever subscribes to it.

This matters in React because a component can read an atom during render, then suspend before React subscribes. If that render is discarded, there is no subscription to clean up. Repeated abandoned renders can accumulate unused atoms and anything their getters capture.

Reproduction: @tanstack/[email protected], Node 24.8.0, macOS. Save as repro.mjs and run node --expose-gc repro.mjs.

import { createAtom } from '@tanstack/store'

const source = createAtom(0)

function createAndDiscard() {
  const derived = createAtom(() => source.get() + 1)
  derived.get() // Read once; never subscribe.
  return new WeakRef(derived) // Observe it without keeping it alive.
}

const refs = Array.from({ length: 100 }, createAndDiscard)

// Give garbage collection several chances to release the unused atoms.
for (let i = 0; i < 10; i++) {
  await new Promise(resolve => setTimeout(resolve, 0))
  global.gc()
}

const remaining = refs.filter(ref => ref.deref() !== undefined).length
console.log(remaining) // Expected: 0. Actual: 100.
console.log(source.get()) // The source is still in use.

Removing derived.get() makes all 100 atoms collectible. Calling derived.subscribe(() => {}).unsubscribe() after the read also allows all 100 to be collected.

The cause appears to be the dependency tracking in atom.ts: reading the computed atom connects it to its source. Unsubscribing removes that connection, but an atom that was never subscribed has no corresponding cleanup.

Expected: once the application drops a computed atom, its source should not keep it alive solely because it was read earlier.

AI disclosure: This report was investigated, reproduced, and written by OpenAI Codex (AI), and filed at the user's request.

主要语言
TypeScript
星标
900
派生
116
平均合并
1 天 21 小时
30 天内合并 PR
7

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/store 的其他 Issue

查看 TanStack/store 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。