Reading a computed atom without subscribing can prevent garbage collection
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 58/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript
- 领域
- frontend, performance
调研方向
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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/store 的其他 Issue
-
useSelector wraps selected objects in $state, causing state_proxy_equality_mismatch and defeating identity comparison可能已有人在做 @IdrisCelik 于 23 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
`useSelector` triggers `state_proxy_equality_mismatch` on every store update in svelte-store可能已有人在做 @seongwon030 于 137 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
TanStack/store#322 · 2 条评论 · 5 个 reaction ·
维护者通常 1 天内回复
-
Missing atom docs可能已有人在做 @looooown2006 于 151 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 45/100
维护者通常 1 天内回复
-
Type error in `useStore` when upgrading from 1.28.0 to 1.28.4可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 3/5 1-2 天 新手友好度 52/100
TanStack/store#323 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
相似的 Issue
-
bot:ai-assisted status:untriaged
难度 2/5 1-3 小时 新手友好度 85/100
midnightntwrk/midnight-js#1424 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 68/100
mksglu/context-mode#1268 ·
维护者通常 5 天内回复
-
[bug] Setup fails with "Cannot find matching keyid" when an older Node's corepack is on PATH可能已有人在做 @EyalPoly 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
MystenLabs/MemWal#1124 · 2 条评论 ·
维护者通常 1 天内回复
-
Edit:未关闭check:failed streams:edit
难度 2/5 1-3 小时 新手友好度 60/100
维护者通常 1 天内回复