python / python/cpython

InterpreterPoolExecutor workers do not inherit modifications made to sys.path before starting.

Ouverte
#126,714 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

3.14 stdlib topic-subinterpreters type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

Bug description:

All existing executors will reflect modifications to sys.path in the child thread/process/whatever that gets started. However the new InterpreterPoolExecutor is not copying this behavior, leading to unexpected import errors when for example running sub-intepreters inside of pytest, as the test directory is added to the sys.path in the parent interpreter but will be lost in the sub-interpreter.

This is easily worked around by using an initializer:

pool = InterpreterPoolExecutor(
        max_workers=queue.concurrency,
        initializer=on_initialize_worker,
        initargs=(sys.path,),
 )
 
 def on_initialize_worker(parent_sys_path: list[str]):
        """
        This method is called in each worker before it begins running jobs.
        It can be used to perform any necessary setup, such as loading NLTK
        datasets or calling ``django.setup()``.

        By default, it replaces the running job's ``sys.path`` with the workers.
        """
        # Unlike all other executors, the InterpreterPoolExecutor does not
        # automatically inherit the parent process's sys.path. This is a
        # workaround to ensure that the worker has the same sys.path as the
        # parent process or tests will fail.
        sys.path = parent_sys_path

This behavior makes sense for sub-interpreters themselves, but probably not for the InterpreterPoolExecutor.

cc. @ericsnowcurrently

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux

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 localiser l’implémentation CPython et les tests de InterpreterPoolExecutor, puis comparez la manière dont les exécuteurs existants gèrent les modifications de sys.path du processus parent avant le démarrage des workers. Reproduisez l’échec d’importation signalé en ajoutant un chemin avant de créer l’exécuteur. Le travail est terminé lorsque les workers héritent de ce chemin modifié sans nécessiter d’initializer, avec une couverture de régression pour le comportement de Python 3.13 et des versions ultérieures.

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

Évaluation

Stack technique
python
Domaine
backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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