python / python/cpython

Broken circular import between `threading` and `_threading_local`

未关闭
#156,341 11 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

The threading module defines local as follows:

https://github.com/python/cpython/blob/ee521e8ac19ad012ebc4e1b3e71b369988a9b9f8/Lib/threading.py#L67-L70

But _threading_local also uses threading as follows:

https://github.com/python/cpython/blob/ee521e8ac19ad012ebc4e1b3e71b369988a9b9f8/Lib/_threading_local.py#L120

This was fine at first, since threading never used thread-local data (the import was near the end). However, this changed in gh-114424:

https://github.com/python/cpython/blob/ee521e8ac19ad012ebc4e1b3e71b369988a9b9f8/Lib/threading.py#L1540-L1542

The _threading_local.local object depends on threading.current_thread(), and this dependency occurs when the object is created:

https://github.com/python/cpython/blob/ee521e8ac19ad012ebc4e1b3e71b369988a9b9f8/Lib/_threading_local.py#L93-L96
https://github.com/python/cpython/blob/ee521e8ac19ad012ebc4e1b3e71b369988a9b9f8/Lib/_threading_local.py#L42-L46

The object is created (and local is imported) before current_thread() is defined. As a result, there is the broken circular import, which can be reproduced as follows:

>>> import _thread
>>> del _thread._local
>>> import threading
Traceback (most recent call last):
  File "/home/user/.local/share/mise/installs/python/3.14.7/lib/python3.14/threading.py", line 67, in <module>
    from _thread import _local as local
ImportError: cannot import name '_local' from '_thread' (unknown location)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    import threading
  File "/home/user/.local/share/mise/installs/python/3.14.7/lib/python3.14/threading.py", line 69, in <module>
    from _threading_local import local
  File "/home/user/.local/share/mise/installs/python/3.14.7/lib/python3.14/_threading_local.py", line 120, in <module>
    from threading import current_thread, RLock
ImportError: cannot import name 'current_thread' from partially initialized module 'threading' (most likely due to a circular import) (/home/user/.local/share/mise/installs/python/3.14.7/lib/python3.14/threading.py)

Since the issue has not been reported before, this raises a legitimate question: is the fallback still relevant?

Related: #141460.

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-157042

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读所引用的导入和对象创建位置中的 Lib/threading.py 和 Lib/_threading_local.py。在导入 threading 前删除 _thread._local,以复现该故障,然后查看相关 issue #141460 和链接的 PR gh-157042。循环导入得到解决,并且演示的 fallback 行为由回归测试覆盖,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。