Reading a computed atom without subscribing can prevent garbage collection
I maintainer di solito rispondono entro 1 giorno
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
- Ambito
- frontend, performance
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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di TanStack/store
-
useSelector wraps selected objects in $state, causing state_proxy_equality_mismatch and defeating identity comparisonForse già presa @IdrisCelik l’ha presa 22 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
`useSelector` triggers `state_proxy_equality_mismatch` on every store update in svelte-storeForse già presa @seongwon030 l’ha presa 136 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
TanStack/store#322 · 2 commenti · 5 reazioni ·
I maintainer di solito rispondono entro 1 giorno
-
Missing atom docsForse già presa @looooown2006 l’ha presa 150 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
TanStack/store#299 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Type error in `useStore` when upgrading from 1.28.0 to 1.28.4Forse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
TanStack/store#323 · 2 commenti · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di TanStack/store
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
solana-foundation/solana-com#2245 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
`document.cookie` with `max-age=0` does not delete the cookieForse già presa @BartInTheField l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
capricorn86/happy-dom#2460 ·
I maintainer di solito rispondono entro 2 giorni