python / python/cpython

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

Aberta
#126,714 2 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

3.14 stdlib topic-subinterpreters type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece localizando a implementação do CPython e os testes de InterpreterPoolExecutor; em seguida, compare como os executores existentes lidam com alterações em sys.path do processo pai antes de os workers serem iniciados. Reproduza a falha de importação relatada adicionando um caminho antes de criar o executor. O trabalho estará concluído quando os workers herdarem esse caminho modificado sem exigir um initializer, com cobertura de regressão para o comportamento do Python 3.13 e posteriores.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.