Skip to content

Commit 0eb9883

Browse files
gh-109593: Fix reentrancy issue in multiprocessing resource_tracker (#109629)
--------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 2897142 commit 0eb9883

File tree

7 files changed

+95
-2
lines changed

7 files changed

+95
-2
lines changed

Lib/multiprocessing/resource_tracker.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,31 @@
5151
})
5252

5353

54+
class ReentrantCallError(RuntimeError):
55+
pass
56+
57+
5458
class ResourceTracker(object):
5559

5660
def __init__(self):
57-
self._lock = threading.Lock()
61+
self._lock = threading.RLock()
5862
self._fd = None
5963
self._pid = None
6064

65+
def _reentrant_call_error(self):
66+
# gh-109629: this happens if an explicit call to the ResourceTracker
67+
# gets interrupted by a garbage collection, invoking a finalizer (*)
68+
# that itself calls back into ResourceTracker.
69+
# (*) for example the SemLock finalizer
70+
raise ReentrantCallError(
71+
"Reentrant call into the multiprocessing resource tracker")
72+
6173
def _stop(self):
6274
with self._lock:
75+
# This should not happen (_stop() isn't called by a finalizer)
76+
# but we check for it anyway.
77+
if self._lock._recursion_count() > 1:
78+
return self._reentrant_call_error()
6379
if self._fd is None:
6480
# not running
6581
return
@@ -81,6 +97,9 @@ def ensure_running(self):
8197
This can be run from any process. Usually a child process will use
8298
the resource created by its parent.'''
8399
with self._lock:
100+
if self._lock._recursion_count() > 1:
101+
# The code below is certainly not reentrant-safe, so bail out
102+
return self._reentrant_call_error()
84103
if self._fd is not None:
85104
# resource tracker was launched before, is it still running?
86105
if self._check_alive():
@@ -159,7 +178,17 @@ def unregister(self, name, rtype):
159178
self._send('UNREGISTER', name, rtype)
160179

161180
def _send(self, cmd, name, rtype):
162-
self.ensure_running()
181+
try:
182+
self.ensure_running()
183+
except ReentrantCallError:
184+
# The code below might or might not work, depending on whether
185+
# the resource tracker was already running and still alive.
186+
# Better warn the user.
187+
# (XXX is warnings.warn itself reentrant-safe? :-)
188+
warnings.warn(
189+
f"ResourceTracker called reentrantly for resource cleanup, "
190+
f"which is unsupported. "
191+
f"The {rtype} object {name!r} might leak.")
163192
msg = '{0}:{1}:{2}\n'.format(cmd, name, rtype).encode('ascii')
164193
if len(msg) > 512:
165194
# posix guarantees that writes to a pipe of less than PIPE_BUF
@@ -176,6 +205,7 @@ def _send(self, cmd, name, rtype):
176205
unregister = _resource_tracker.unregister
177206
getfd = _resource_tracker.getfd
178207

208+
179209
def main(fd):
180210
'''Run resource tracker.'''
181211
# protect the process from ^C and "killall python" etc

Lib/test/lock_tests.py

+36
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,42 @@ def test_release_save_unacquired(self):
330330
lock.release()
331331
self.assertRaises(RuntimeError, lock._release_save)
332332

333+
def test_recursion_count(self):
334+
lock = self.locktype()
335+
self.assertEqual(0, lock._recursion_count())
336+
lock.acquire()
337+
self.assertEqual(1, lock._recursion_count())
338+
lock.acquire()
339+
lock.acquire()
340+
self.assertEqual(3, lock._recursion_count())
341+
lock.release()
342+
self.assertEqual(2, lock._recursion_count())
343+
lock.release()
344+
lock.release()
345+
self.assertEqual(0, lock._recursion_count())
346+
347+
phase = []
348+
349+
def f():
350+
lock.acquire()
351+
phase.append(None)
352+
while len(phase) == 1:
353+
_wait()
354+
lock.release()
355+
phase.append(None)
356+
357+
with threading_helper.wait_threads_exit():
358+
start_new_thread(f, ())
359+
while len(phase) == 0:
360+
_wait()
361+
self.assertEqual(len(phase), 1)
362+
self.assertEqual(0, lock._recursion_count())
363+
phase.append(None)
364+
while len(phase) == 2:
365+
_wait()
366+
self.assertEqual(len(phase), 3)
367+
self.assertEqual(0, lock._recursion_count())
368+
333369
def test_different_thread(self):
334370
# Cannot release from a different thread
335371
lock = self.locktype()

Lib/test/test_importlib/test_locks.py

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ModuleLockAsRLockTests:
2929
test_timeout = None
3030
# _release_save() unsupported
3131
test_release_save_unacquired = None
32+
# _recursion_count() unsupported
33+
test_recursion_count = None
3234
# lock status in repr unsupported
3335
test_repr = None
3436
test_locked_repr = None

Lib/test/test_threading.py

+3
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,9 @@ class ConditionAsRLockTests(lock_tests.RLockTests):
17831783
# Condition uses an RLock by default and exports its API.
17841784
locktype = staticmethod(threading.Condition)
17851785

1786+
def test_recursion_count(self):
1787+
self.skipTest("Condition does not expose _recursion_count()")
1788+
17861789
class ConditionTests(lock_tests.ConditionTests):
17871790
condtype = staticmethod(threading.Condition)
17881791

Lib/threading.py

+7
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ def _release_save(self):
245245
def _is_owned(self):
246246
return self._owner == get_ident()
247247

248+
# Internal method used for reentrancy checks
249+
250+
def _recursion_count(self):
251+
if self._owner != get_ident():
252+
return 0
253+
return self._count
254+
248255
_PyRLock = _RLock
249256

250257

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid deadlocking on a reentrant call to the multiprocessing resource tracker. Such a reentrant call, though unlikely, can happen if a GC pass invokes the finalizer for a multiprocessing object such as SemLock.

Modules/_threadmodule.c

+14
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,18 @@ PyDoc_STRVAR(rlock_release_save_doc,
490490
\n\
491491
For internal use by `threading.Condition`.");
492492

493+
static PyObject *
494+
rlock_recursion_count(rlockobject *self, PyObject *Py_UNUSED(ignored))
495+
{
496+
unsigned long tid = PyThread_get_thread_ident();
497+
return PyLong_FromUnsignedLong(
498+
self->rlock_owner == tid ? self->rlock_count : 0UL);
499+
}
500+
501+
PyDoc_STRVAR(rlock_recursion_count_doc,
502+
"_recursion_count() -> int\n\
503+
\n\
504+
For internal use by reentrancy checks.");
493505

494506
static PyObject *
495507
rlock_is_owned(rlockobject *self, PyObject *Py_UNUSED(ignored))
@@ -565,6 +577,8 @@ static PyMethodDef rlock_methods[] = {
565577
METH_VARARGS, rlock_acquire_restore_doc},
566578
{"_release_save", (PyCFunction)rlock_release_save,
567579
METH_NOARGS, rlock_release_save_doc},
580+
{"_recursion_count", (PyCFunction)rlock_recursion_count,
581+
METH_NOARGS, rlock_recursion_count_doc},
568582
{"__enter__", _PyCFunction_CAST(rlock_acquire),
569583
METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc},
570584
{"__exit__", (PyCFunction)rlock_release,

0 commit comments

Comments
 (0)