AsyncLocalStorage is captured on the first console.log which prevents garbage collection of objects in the storage
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 122k
- Fork
- 37.3k
- Merge medio
- 4g 2h
- PR unite (30g)
- 283
Descrizione
Version
v20.3.0
Platform
Linux 5.19.0-46-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
async_hooks
What steps will reproduce the bug?
Run the following with node --expose-gc:
import { AsyncLocalStorage } from "node:async_hooks";
const storage = new AsyncLocalStorage();
const finalizer = new FinalizationRegistry((token) => {
console.log("COLLECTED", token);
});
const wr = new WeakRef({});
finalizer.register(wr.deref(), "obj");
// Whether COLLECTED is printed depends on whether this is present or not
// console.log("OUTER LOG");
storage.run({ obj: wr.deref() }, () => {
console.log(storage.getStore()?.obj);
});
await new Promise((resolve) => setTimeout(resolve, 1000));
gc();
How often does it reproduce? Is there a required condition?
This happens consistently.
What is the expected behavior? Why is that the expected behavior?
The object stored in the WeakRef should be collected when gc() is hit, but whether this happens depends on whether the line:
// console.log("OUTER LOG");
is commented or not.
What do you see instead?
The object is not collected if the first console.log happens under the storage.run callback.
Additional information
For whatever reason wherever the first console.log happens this is where the async resource for the TTY is made. This causes objects in the AsyncLocalStorage to be eternally referenced and never eligible for garbage collection.
To allow GC of values in AsyncLocalStorage, the fix here would be to simply create the TTY's async resource using the root's async scope rather than whatever happens to be active.
(I can't see large reason why the current behaviour would be desirable as it just makes GC of values dependent on first console.log. If anyone needed such behaviour legitimately it'd make more sense to expose an initTTY function or similar that captures the async scope at that time).
Guida per i contributori
Apri 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.
Direzione di ricerca
Inizia eseguendo la riproduzione fornita con node --expose-gc e analizza la gestione di async_hooks della risorsa TTY creata dal primo console.log. Traccia il modo in cui tale risorsa cattura l’async scope attivo; il lavoro è considerato completato quando l’oggetto può essere raccolto indipendentemente dal fatto che il console.log esterno venga eseguito per primo, con un test di regressione per entrambi i casi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 52/100