python / python/cpython

Broken circular import between `threading` and `_threading_local`

Đang mở
#156,341 11 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Đọc Lib/threading.py và Lib/_threading_local.py tại các điểm import và tạo đối tượng được tham chiếu. Tái hiện lỗi bằng cách xóa _thread._local trước khi import threading, sau đó xem xét issue liên quan #141460 và PR được liên kết gh-157042. Được coi là hoàn thành khi import vòng lặp được giải quyết và hành vi fallback đã được minh họa được bao phủ bằng các bài kiểm thử hồi quy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.