From 28fa7d7675ca34ed39d4e601424375b464413e69 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 14 Jun 2021 23:20:47 +0200 Subject: [PATCH 1/3] bpo-44422: Fix threading.enumerate() reentrant call The threading.enumerate() function now uses a reentrant lock to prevent a hang on reentrant call. --- Lib/threading.py | 7 +++++-- .../next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst diff --git a/Lib/threading.py b/Lib/threading.py index 6c3d49c2d52679..2bb98cd52fead4 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -775,8 +775,11 @@ class BrokenBarrierError(RuntimeError): def _newname(name_template): return name_template % _counter() -# Active thread administration -_active_limbo_lock = _allocate_lock() +# Active thread administration. +# +# bpo-44422: Use a reentrant lock to allocate reentrant calls to functions like +# threading.enumerate(). +_active_limbo_lock = RLock() _active = {} # maps thread id to Thread object _limbo = {} _dangling = WeakSet() 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 00000000000000..bb13667e1faa7d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst @@ -0,0 +1,2 @@ +The :func:`threading.enumerate` function now uses a reentrant lock to +prevent a hang on reentrant call. From adb0a4dd58c69f40b724ed9e6c3f9f3b3a82cb50 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 15 Jun 2021 10:40:45 +0200 Subject: [PATCH 2/3] Fix typo --- Lib/threading.py | 2 +- .../next/Library/2021-06-14-23-28-17.bpo-44422.BlWOgv.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/threading.py b/Lib/threading.py index 2bb98cd52fead4..feeb4fba450d9f 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -777,7 +777,7 @@ def _newname(name_template): # Active thread administration. # -# bpo-44422: Use a reentrant lock to allocate reentrant calls to functions like +# 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 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 index bb13667e1faa7d..09bace01fc7794 100644 --- 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 @@ -1,2 +1,3 @@ The :func:`threading.enumerate` function now uses a reentrant lock to prevent a hang on reentrant call. +Patch by Victor Stinner. From 20a0d9ae3a7afd03497352c0940e311303c3071b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 15 Jun 2021 15:11:17 +0200 Subject: [PATCH 3/3] Fix _after_fork() Issue spotted by Irit. --- Lib/threading.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/threading.py b/Lib/threading.py index feeb4fba450d9f..766011fa0312b3 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1567,7 +1567,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 = {}