Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Reading a computed atom without subscribing can prevent garbage collection

オープン
#372 コメント 0 件 リアクション 0 件 担当者 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
平均マージ
2日 2時間
マージ済み PR(30日)
6

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/store のほかの issue

TanStack/store の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。