From 339467eb8564d5f28b64ff817f29d59fc557be87 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 20 Sep 2023 18:25:45 +0200 Subject: [PATCH] gh-109593: ResourceTracker.ensure_running() calls finalizers multiprocessing: Reduce the risk of reentrant calls to ResourceTracker.ensure_running() by running explicitly all finalizers before acquiring the ResourceTracker lock. --- Lib/multiprocessing/resource_tracker.py | 7 +++++++ .../Library/2023-09-20-18-28-12.gh-issue-109593.VVWJDC.rst | 3 +++ 2 files changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-09-20-18-28-12.gh-issue-109593.VVWJDC.rst diff --git a/Lib/multiprocessing/resource_tracker.py b/Lib/multiprocessing/resource_tracker.py index 3783c1ffc6e4a92..dbd10aa74d2a6e5 100644 --- a/Lib/multiprocessing/resource_tracker.py +++ b/Lib/multiprocessing/resource_tracker.py @@ -80,6 +80,13 @@ def ensure_running(self): This can be run from any process. Usually a child process will use the resource created by its parent.''' + + # gh-109593: Reduce the risk of reentrant calls to ensure_running() by + # running explicitly all finalizers. Otherwise, finalizers like + # SemLock._cleanup() can make indirectly a reentrant call to + # ensure_running(). + util._run_finalizers() + with self._lock: if self._fd is not None: # resource tracker was launched before, is it still running? diff --git a/Misc/NEWS.d/next/Library/2023-09-20-18-28-12.gh-issue-109593.VVWJDC.rst b/Misc/NEWS.d/next/Library/2023-09-20-18-28-12.gh-issue-109593.VVWJDC.rst new file mode 100644 index 000000000000000..d60640cd2aab863 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-20-18-28-12.gh-issue-109593.VVWJDC.rst @@ -0,0 +1,3 @@ +:mod:`multiprocessing`: Reduce the risk of reentrant calls to +``ResourceTracker.ensure_running()`` by running explicitly all finalizers +before acquiring the ``ResourceTracker`` lock. Patch by Victor Stinner.