python / python/cpython

[subinterpreters] Refleaks on Windows Under Specific Conditions

Ouverte
#117,936 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

OS-windows pending topic-subinterpreters type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
36k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

Bug description:

One of my recent PRs, gh-117662, led to one of the Windows refleak buildbots failing^1. I'm fairly confident the PR revealed an existing source of refleaks (albeit an unlikely one), rather that introducing new leaks. I have resolved the failures with gh-117913, but the underlying potential source of refleaks remains.

I've been able to reproduce the leak conditions, but only on Windows (for now?). The following conditions are necessary:

  • interpreter created using a config with check_multi_interp_extensions=False (e.g. "legacy")
  • interpreter imports test.support.os_helper
try:
    import _interpreters
except ModuleNotFoundError:
    import _xxsubinterpreters as _interpreters
config = _interpreters.new_config('legacy')  # critically, check_multi_interp_extensions=False
interpid = _interpreters.create(config)
_interpreters.exec(interpid, 'import test.support.os_helper')
_interpreters.destroy(interpid)
# leaks a bunch of objects

When the bug is triggered, around 190 objects are leaked. At first I thought this was interpreter finalization failing silently, but now I think it is something else.

I suspect the underlying problem relates to legacy extension modules. In the specific example above, I'm pretty sure the _ctypes module is leaking and ~190 is how many objects it holds, directly or indirectly. The main clue there is that, when I run ./python -v ..., the _ctypes module is never noted as destroyed, whereas all other modules are. As to test.support.os_helper, _ctypes is one of the modules that gets indirectly imported. All that said, I haven't been able to reproduce the leak if the subinterpreter above imports _ctypes instead.

Why did we only see this on Windows? The only clue I can think of is that on Windows stdlib extension modules are all turned into builtin modules, IIRC. The logic of the check_multi_interp_extensions check (via _PyImport_CheckSubinterpIncompatibleExtensionAllowed() in Python/import.c) is specific to extension modules, not builtin modules.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

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

Reproduisez la fuite Windows avec l’extrait _interpreters legacy-config fourni, puis commencez dans Python/import.c autour de _PyImport_CheckSubinterpIncompatibleExtensionAllowed(). Comparez le nettoyage des modules pour test.support.os_helper et _ctypes à l’aide de la sortie d’importation détaillée décrite dans le rapport. Le travail est terminé lorsque le reproducer ne provoque plus de fuite d’objets et que le comportement Windows concerné est couvert par un test de régression approprié.

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

Évaluation

Stack technique
python
Domaine
operating-systems
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
38/100

Recevez les nouvelles issues par e-mail

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