python / python/cpython

import_importlib leaks module attr sets

Aperta
#157,230 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

tests topic-importlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con test_importlib.util.import_importlib(), quindi riproduci il fallimento in test_pickle e test_pickletool. Traccia come l’importazione temporanea di importlib modifica builtins.loader e builtins.spec; il lavoro è completato quando i valori originali vengono ripristinati ed entrambi i test passano senza il PicklingError. Una PR collegata, gh-154276, indica che il lavoro è già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend, testing-qa
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.