python / python/cpython

import_importlib leaks module attr sets

Abierto
#157,230 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

tests topic-importlib 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:

Our CI (https://github.com/facebookincubator/cinder/actions/runs/29382592631/job/87249270535) was failing on test_pickle and test_pickletool with:

PicklingError: Can't pickle
<class 'importlib._bootstrap.BuiltinImporter'>:
it's not the same object as
importlib._bootstrap.BuiltinImporter

I believe this is happening because we run multiple test modules in the same interpreter, so the following sequence happens:

  • test_importlib.util.import_importlib() imports a source copy of importlib while blocking _frozen_importlib. During this import, importlib._bootstrap._setup() initializes import metadata on existing built-in modules. If builtins.__loader__ or builtins.__spec__ was originally absent, the source copy installs its own BuiltinImporter.
  • Although import_fresh_module() restores sys.modules, it does not restore attributes mutated on existing module objects. Consequently, builtins.__loader__ continues to reference the temporary source BuiltinImporter, while importlib._bootstrap.BuiltinImporter resolves to the restored frozen class.
  • Pickle serializes classes by module and qualified name and verifies that the resolved global is the same object. The two BuiltinImporter class objects therefore cause the identity check to fail.

To fix this we need to snapshot __loader__ and __spec__ before importing importlib and then restore them to their original values after.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-154276

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

Comienza con test_importlib.util.import_importlib() y luego reproduce el fallo en test_pickle y test_pickletool. Rastrea cómo la importación temporal de importlib cambia builtins.loader y builtins.spec; el trabajo está terminado cuando se restauran los valores originales y ambas pruebas pasan sin el PicklingError. Un PR vinculado, gh-154276, indica que el trabajo ya está en curso.

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

Evaluación

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.