summaryrefslogtreecommitdiffstats
path: root/Lib/threading.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-09-27 21:09:00 (GMT)
committerGitHub <noreply@github.com>2021-09-27 21:09:00 (GMT)
commit95d31370829b7d729667588e0a9943217401ea5b (patch)
tree25ca05eebdc3372c579135c533b5601d27552d85 /Lib/threading.py
parent20f439b6b9e1032930a31b88694ab9f37a09e6b4 (diff)
downloadcpython-95d31370829b7d729667588e0a9943217401ea5b.zip
cpython-95d31370829b7d729667588e0a9943217401ea5b.tar.gz
cpython-95d31370829b7d729667588e0a9943217401ea5b.tar.bz2
bpo-1596321: Fix threading._shutdown() for the main thread (GH-28549)
Fix the threading._shutdown() function when the threading module was imported first from a thread different than the main thread: no longer log an error at Python exit.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r--Lib/threading.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/Lib/threading.py b/Lib/threading.py
index 1c74a8d..9b0419c 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -1517,20 +1517,29 @@ def _shutdown():
global _SHUTTING_DOWN
_SHUTTING_DOWN = True
- # Main thread
- tlock = _main_thread._tstate_lock
- # The main thread isn't finished yet, so its thread state lock can't have
- # been released.
- assert tlock is not None
- assert tlock.locked()
- tlock.release()
- _main_thread._stop()
# Call registered threading atexit functions before threads are joined.
# Order is reversed, similar to atexit.
for atexit_call in reversed(_threading_atexits):
atexit_call()
+ # Main thread
+ if _main_thread.ident == get_ident():
+ tlock = _main_thread._tstate_lock
+ # The main thread isn't finished yet, so its thread state lock can't
+ # have been released.
+ assert tlock is not None
+ assert tlock.locked()
+ tlock.release()
+ _main_thread._stop()
+ else:
+ # bpo-1596321: _shutdown() must be called in the main thread.
+ # If the threading module was not imported by the main thread,
+ # _main_thread is the thread which imported the threading module.
+ # In this case, ignore _main_thread, similar behavior than for threads
+ # spawned by C libraries or using _thread.start_new_thread().
+ pass
+
# Join all non-deamon threads
while True:
with _shutdown_locks_lock: