diff --git a/backport-37788-Fix-reference-leak-when-Thread-is-never-joined.patch b/backport-37788-Fix-reference-leak-when-Thread-is-never-joined.patch new file mode 100644 index 0000000000000000000000000000000000000000..cd6982404abd26acaf00e0cc5a49ba858da242c4 --- /dev/null +++ b/backport-37788-Fix-reference-leak-when-Thread-is-never-joined.patch @@ -0,0 +1,98 @@ +From 35a20de92f749a96315fb875bc7bdb7460ce9ca9 Mon Sep 17 00:00:00 2001 +From: Antoine Pitrou +Date: Sat, 15 May 2021 11:51:20 +0200 +Subject: [PATCH] bpo-37788: Fix reference leak when Thread is never joined + (GH-26103) (GH-26142) + +When a Thread is not joined after it has stopped, its lock may remain in the _shutdown_locks set until interpreter shutdown. If many threads are created this way, the _shutdown_locks set could therefore grow endlessly. To avoid such a situation, purge expired locks each time a new one is added or removed.. +(cherry picked from commit c10c2ec7a0e06975e8010c56c9c3270f8ea322ec) + +Co-authored-by: Antoine Pitrou + +Automerge-Triggered-By: GH:pitrou +--- + Lib/test/test_threading.py | 8 ++++++++ + Lib/threading.py | 19 ++++++++++++++++++- + .../2021-05-13-19-07-28.bpo-37788.adeFcf.rst | 1 + + 3 files changed, 27 insertions(+), 1 deletion(-) + create mode 100644 Misc/NEWS.d/next/Library/2021-05-13-19-07-28.bpo-37788.adeFcf.rst + +diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py +index 36d9fbb..ac0b50e 100644 +--- a/Lib/test/test_threading.py ++++ b/Lib/test/test_threading.py +@@ -757,6 +757,14 @@ class ThreadTests(BaseTestCase): + # Daemon threads must never add it to _shutdown_locks. + self.assertNotIn(tstate_lock, threading._shutdown_locks) + ++ def test_leak_without_join(self): ++ # bpo-37788: Test that a thread which is not joined explicitly ++ # does not leak. Test written for reference leak checks. ++ def noop(): pass ++ with support.wait_threads_exit(): ++ threading.Thread(target=noop).start() ++ # Thread.join() is not called ++ + + class ThreadJoinOnShutdown(BaseTestCase): + +diff --git a/Lib/threading.py b/Lib/threading.py +index b961456..a28baa9 100644 +--- a/Lib/threading.py ++++ b/Lib/threading.py +@@ -733,12 +733,27 @@ _active_limbo_lock = _allocate_lock() + _active = {} # maps thread id to Thread object + _limbo = {} + _dangling = WeakSet() ++ + # Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown() + # to wait until all Python thread states get deleted: + # see Thread._set_tstate_lock(). + _shutdown_locks_lock = _allocate_lock() + _shutdown_locks = set() + ++def _maintain_shutdown_locks(): ++ """ ++ Drop any shutdown locks that don't correspond to running threads anymore. ++ ++ Calling this from time to time avoids an ever-growing _shutdown_locks ++ set when Thread objects are not joined explicitly. See bpo-37788. ++ ++ This must be called with _shutdown_locks_lock acquired. ++ """ ++ # If a lock was released, the corresponding thread has exited ++ to_remove = [lock for lock in _shutdown_locks if not lock.locked()] ++ _shutdown_locks.difference_update(to_remove) ++ ++ + # Main class for threads + + class Thread: +@@ -906,6 +921,7 @@ class Thread: + + if not self.daemon: + with _shutdown_locks_lock: ++ _maintain_shutdown_locks() + _shutdown_locks.add(self._tstate_lock) + + def _bootstrap_inner(self): +@@ -998,7 +1014,8 @@ class Thread: + self._tstate_lock = None + if not self.daemon: + with _shutdown_locks_lock: +- _shutdown_locks.discard(lock) ++ # Remove our lock and other released locks from _shutdown_locks ++ _maintain_shutdown_locks() + + def _delete(self): + "Remove current thread from the dict of currently running threads." +diff --git a/Misc/NEWS.d/next/Library/2021-05-13-19-07-28.bpo-37788.adeFcf.rst b/Misc/NEWS.d/next/Library/2021-05-13-19-07-28.bpo-37788.adeFcf.rst +new file mode 100644 +index 0000000..0c33923 +--- /dev/null ++++ b/Misc/NEWS.d/next/Library/2021-05-13-19-07-28.bpo-37788.adeFcf.rst +@@ -0,0 +1 @@ ++Fix a reference leak when a Thread object is never joined. +-- +2.23.0 + diff --git a/python3.spec b/python3.spec index 11226e8d2bad54d92f2e84d20fb39921856ad39b..b9373b09338a0934684e662b7e4743b3a6b7aadd 100644 --- a/python3.spec +++ b/python3.spec @@ -3,7 +3,7 @@ Summary: Interpreter of the Python3 programming language URL: https://www.python.org/ Version: 3.7.9 -Release: 10 +Release: 11 License: Python %global branchversion 3.7 @@ -106,6 +106,7 @@ Patch319: python3-add-generic-os-support.patch Patch320: CVE-2020-27619.patch Patch323: CVE-2021-3177.patch Patch324: backport-CVE-2021-23336.patch +Patch6000: backport-37788-Fix-reference-leak-when-Thread-is-never-joined.patch Recommends: %{name}-help = %{version}-%{release} Provides: python%{branchversion} = %{version}-%{release} @@ -199,6 +200,7 @@ rm Lib/ensurepip/_bundled/*.whl %patch320 -p1 %patch323 -p1 %patch324 -p1 +%patch6000 -p1 sed -i "s/generic_os/%{_vendor}/g" Lib/platform.py rm configure pyconfig.h.in @@ -800,6 +802,12 @@ export BEP_GTDLIST="$BEP_GTDLIST_TMP" %{_mandir}/*/* %changelog +* Sat May 29 2021 hehuazhen - 3.7.9-11 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:fix reference leak when thread is never joined + * Web Mar 03 2021 wuchaochao - 3.7.9-10 - Type:cves - ID:CVE-2021-23336