Skip to content

Commit d11bc95

Browse files
committed
gh-109593: ResourceTracker.ensure_running() calls finalizers
multiprocessing: Reduce the risk of reentrant calls to ResourceTracker.ensure_running() by running explicitly a garbage collection, to call pending finalizers, before acquiring the ResourceTracker lock.
1 parent ef6d475 commit d11bc95

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Lib/multiprocessing/resource_tracker.py

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# this resource tracker process, "killall python" would probably leave unlinked
1616
# resources.
1717

18+
import gc
1819
import os
1920
import signal
2021
import sys
@@ -80,6 +81,13 @@ def ensure_running(self):
8081
8182
This can be run from any process. Usually a child process will use
8283
the resource created by its parent.'''
84+
85+
# gh-109593: Reduce the risk of reentrant calls to ensure_running() by
86+
# running explicitly a garbage collection. Otherwise, finalizers like
87+
# SemLock._cleanup() can make indirectly a reentrant call to
88+
# ensure_running().
89+
gc.collect()
90+
8391
with self._lock:
8492
if self._fd is not None:
8593
# resource tracker was launched before, is it still running?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`multiprocessing`: Reduce the risk of reentrant calls to
2+
``ResourceTracker.ensure_running()`` by running explicitly a garbage
3+
collection, to call pending finalizers, before acquiring the
4+
``ResourceTracker`` lock. Patch by Victor Stinner.

0 commit comments

Comments
 (0)