Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Reading a computed atom without subscribing can prevent garbage collection

Aperta
#372 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Sheraff ci sta già lavorando.

Dal 28/9/2026.

  • #373 di @Sheraff — aperta
  • #376 di @Sheraff — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react, typescript

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
900
Fork
116
Merge medio
2g 2h
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/store

Tutte le issue di TanStack/store

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.