nodejs / nodejs/node

AsyncLocalStorage is captured on the first console.log which prevents garbage collection of objects in the storage

Ouverte
#48,651 4 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

async_hooks
Langage dominant
JavaScript
Étoiles
122k
Forks
37.3k
Merge moyen
4 j 2 h
PR mergées (30 j)
283

Description

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).

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par exécuter la reproduction fournie avec node --expose-gc et examinez la gestion de async_hooks de la ressource TTY créée par le premier console.log. Suivez la manière dont cette ressource capture l’async scope actif ; le travail est considéré comme terminé lorsque l’objet peut être récupéré, que le console.log externe s’exécute en premier ou non, avec un test de régression pour les deux cas.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript
Domaine
backend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Active
Clarté
Plutôt claire
Accessibilité débutants
52/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.