Broken circular import between `threading` and `_threading_local`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
The threading module defines local as follows:
But _threading_local also uses threading as follows:
This was fine at first, since threading never used thread-local data (the import was near the end). However, this changed in gh-114424:
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read Lib/threading.py and Lib/_threading_local.py at the referenced import and object-creation points. Reproduce the failure by deleting _thread._local before importing threading, then review related issue #141460 and linked PR gh-157042. Done means the circular import is resolved and the demonstrated fallback behavior is covered by regression testing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100