python / python/cpython

import_importlib leaks module attr sets

未關閉
#157,230 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

tests topic-importlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先從 test_importlib.util.import_importlib() 開始,然後在 test_pickle 和 test_pickletool 中重現失敗。追蹤暫時匯入 importlib 如何變更 builtins.loader 和 builtins.spec;當原始值恢復且兩個測試都在沒有 PicklingError 的情況下通過時,即表示完成。一個相關的 PR gh-154276 表示工作已經在進行中。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend, testing-qa
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。