From 82ba7773409636645f7c27f74ddece76e038266d Mon Sep 17 00:00:00 2001 From: sxt1001 Date: Mon, 1 Feb 2021 16:00:54 +0800 Subject: [PATCH 1/2] Fix a reference leak if a thread is not joined (cherry picked from commit f47bafa9a1cb4d4d579529dbfdc489d6bc33b1d2) --- ...rence-leak-if-a-thread-is-not-joined.patch | 63 +++++++++++++++++++ python3.spec | 12 +++- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 backport-Fix-a-reference-leak-if-a-thread-is-not-joined.patch diff --git a/backport-Fix-a-reference-leak-if-a-thread-is-not-joined.patch b/backport-Fix-a-reference-leak-if-a-thread-is-not-joined.patch new file mode 100644 index 0000000..2919676 --- /dev/null +++ b/backport-Fix-a-reference-leak-if-a-thread-is-not-joined.patch @@ -0,0 +1,63 @@ +From d3dcc92778807ae8f7ebe85178f36a29711cd478 Mon Sep 17 00:00:00 2001 +From: Victor Stinner +Date: Mon, 19 Aug 2019 23:37:17 +0100 +Subject: [PATCH] bpo-37788: Fix a reference leak if a thread is not joined + (GH-15228) + +Add threading.Thread.__del__() method to ensure that the thread state +lock is removed from the _shutdown_locks list when a thread +completes. +--- + Lib/test/test_threading.py | 8 ++++++++ + Lib/threading.py | 10 ++++++++++ + .../Library/2019-08-12-17-21-10.bpo-37788.F0tR05.rst | 1 + + 3 files changed, 19 insertions(+) + create mode 100644 Misc/NEWS.d/next/Library/2019-08-12-17-21-10.bpo-37788.F0tR05.rst + +diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py +index 7c16974c1630a..5e90627822f9f 100644 +--- a/Lib/test/test_threading.py ++++ b/Lib/test/test_threading.py +@@ -761,6 +761,14 @@ def test_shutdown_locks(self): + # 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 32a3d7c303362..67e1c4facfee2 100644 +--- a/Lib/threading.py ++++ b/Lib/threading.py +@@ -806,6 +806,16 @@ class is implemented. + # For debugging and _after_fork() + _dangling.add(self) + ++ def __del__(self): ++ if not self._initialized: ++ return ++ lock = self._tstate_lock ++ if lock is not None and not self.daemon: ++ # ensure that self._tstate_lock is not in _shutdown_locks ++ # if join() was not called explicitly ++ with _shutdown_locks_lock: ++ _shutdown_locks.discard(lock) ++ + def _reset_internal_locks(self, is_alive): + # private! Called by _after_fork() to reset our internal locks as + # they may be in an invalid state leading to a deadlock or crash. +diff --git a/Misc/NEWS.d/next/Library/2019-08-12-17-21-10.bpo-37788.F0tR05.rst b/Misc/NEWS.d/next/Library/2019-08-12-17-21-10.bpo-37788.F0tR05.rst +new file mode 100644 +index 0000000000000..d9b1e82b92238 +--- /dev/null ++++ b/Misc/NEWS.d/next/Library/2019-08-12-17-21-10.bpo-37788.F0tR05.rst +@@ -0,0 +1 @@ ++Fix a reference leak if a thread is not joined. diff --git a/python3.spec b/python3.spec index ceaa493..e6cea95 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.8.5 -Release: 2 +Release: 4 License: Python %global branchversion 3.8 @@ -94,6 +94,7 @@ Patch189: 00189-use-rpm-wheels.patch Patch205: 00205-make-libpl-respect-lib64.patch Patch251: 00251-change-user-install-location.patch Patch252: CVE-2020-27619.patch +Patch253: backport-Fix-a-reference-leak-if-a-thread-is-not-joined.patch Provides: python%{branchversion} = %{version}-%{release} Provides: python(abi) = %{branchversion} @@ -187,6 +188,7 @@ rm Lib/ensurepip/_bundled/*.whl %patch205 -p1 %patch251 -p1 %patch252 -p1 +%patch253 -p1 rm configure pyconfig.h.in @@ -790,7 +792,13 @@ export BEP_GTDLIST="$BEP_GTDLIST_TMP" %{_mandir}/*/* %changelog -* Sat Nov 14 2020 shixuantong - 3.8.5-2 +* Mon Feb 01 2021 shixuantong - 3.8.5-4 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:Fix a reference leak if a thread is not joined + +* Sat Nov 14 2020 shixuantong - 3.8.5-3 - Type:cves - ID:CVE-2020-27619 - SUG:NA -- Gitee From 666044a3fd9b9bc474fa83c7890d54b26e23f091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=90=E5=B0=8F=E5=93=A5?= Date: Mon, 1 Feb 2021 18:23:07 +0800 Subject: [PATCH 2/2] update python3.spec. --- python3.spec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python3.spec b/python3.spec index e6cea95..f9867c3 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.8.5 -Release: 4 +Release: 3 License: Python %global branchversion 3.8 @@ -792,13 +792,13 @@ export BEP_GTDLIST="$BEP_GTDLIST_TMP" %{_mandir}/*/* %changelog -* Mon Feb 01 2021 shixuantong - 3.8.5-4 +* Mon Feb 01 2021 shixuantong - 3.8.5-3 - Type:bugfix - ID:NA - SUG:NA - DESC:Fix a reference leak if a thread is not joined -* Sat Nov 14 2020 shixuantong - 3.8.5-3 +* Sat Nov 14 2020 shixuantong - 3.8.5-2 - Type:cves - ID:CVE-2020-27619 - SUG:NA -- Gitee