python / python/cpython

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

Abierto
#126,714 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

3.14 stdlib topic-subinterpreters type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza localizando la implementación de CPython y las pruebas de InterpreterPoolExecutor, y después compara cómo los ejecutores existentes gestionan los cambios en sys.path del proceso padre antes de que se inicien los workers. Reproduce el fallo de importación informado añadiendo una ruta antes de crear el ejecutor. Se considera terminado cuando los workers hereden esa ruta modificada sin requerir un initializer, con cobertura de regresión para el comportamiento de Python 3.13 y posteriores.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.