python / python/cpython

Broken circular import between `threading` and `_threading_local`

Ouverte
#156,341 11 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

stdlib type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Lisez Lib/threading.py et Lib/_threading_local.py aux points d’importation et de création d’objets référencés. Reproduisez l’échec en supprimant _thread._local avant d’importer threading, puis examinez l’issue associée #141460 et la PR liée gh-157042. Le travail est considéré comme terminé lorsque l’importation circulaire est résolue et que le comportement de fallback démontré est couvert par des tests de régression.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
backend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
30/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.