[Python-checkins] bpo-44422: Fix threading.enumerate() reentrant call (GH-26727) (GH-26738)

vstinner webhook-mailer at python.org
Tue Jun 15 12:30:36 EDT 2021


https://github.com/python/cpython/commit/8fe57aacc7bf9d9af84803b69dbb1d66597934c6
commit: 8fe57aacc7bf9d9af84803b69dbb1d66597934c6
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: vstinner <vstinner at python.org>
date: 2021-06-15T18:30:26+02:00
summary:

bpo-44422: Fix threading.enumerate() reentrant call (GH-26727) (GH-26738)

The threading.enumerate() function now uses a reentrant lock to
prevent a hang on reentrant call.
(cherry picked from commit 243fd01047ddce1a7eb0f99a49732d123e942c63)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst
M Lib/threading.py

diff --git a/Lib/threading.py b/Lib/threading.py
index 702acaa005430..a49775e6b1773 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -750,8 +750,11 @@ class BrokenBarrierError(RuntimeError):
 def _newname(template="Thread-%d"):
     return template % _counter()
 
-# Active thread administration
-_active_limbo_lock = _allocate_lock()
+# Active thread administration.
+#
+# bpo-44422: Use a reentrant lock to allow reentrant calls to functions like
+# threading.enumerate().
+_active_limbo_lock = RLock()
 _active = {}    # maps thread id to Thread object
 _limbo = {}
 _dangling = WeakSet()
@@ -1474,7 +1477,7 @@ def _after_fork():
     # by another (non-forked) thread.  http://bugs.python.org/issue874900
     global _active_limbo_lock, _main_thread
     global _shutdown_locks_lock, _shutdown_locks
-    _active_limbo_lock = _allocate_lock()
+    _active_limbo_lock = RLock()
 
     # fork() only copied the current thread; clear references to others.
     new_active = {}
diff --git a/Misc/NEWS.d/next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst b/Misc/NEWS.d/next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst
new file mode 100644
index 0000000000000..09bace01fc779
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst
@@ -0,0 +1,3 @@
+The :func:`threading.enumerate` function now uses a reentrant lock to
+prevent a hang on reentrant call.
+Patch by Victor Stinner.



More information about the Python-checkins mailing list