Skip to content

Commit

Permalink
pythongh-109593: ResourceTracker.ensure_running() calls finalizers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner committed Sep 20, 2023
1 parent ef6d475 commit d11bc95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# this resource tracker process, "killall python" would probably leave unlinked
# resources.

import gc
import os
import signal
import sys
Expand Down Expand Up @@ -80,6 +81,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 a garbage collection. Otherwise, finalizers like
# SemLock._cleanup() can make indirectly a reentrant call to
# ensure_running().
gc.collect()

with self._lock:
if self._fd is not None:
# resource tracker was launched before, is it still running?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`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. Patch by Victor Stinner.

0 comments on commit d11bc95

Please sign in to comment.