8000 [3.12] gh-88887: Cleanup `multiprocessing.resource_tracker.ResourceTracker` … (GH-130429) by luccabb · Pull Request #131530 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.12] gh-88887: Cleanup multiprocessing.resource_tracker.ResourceTracker … (GH-130429) #131530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,46 @@ def _reentrant_call_error(self):
raise ReentrantCallError(
"Reentrant call into the multiprocessing resource tracker")

def _stop(self):
with self._lock:
# This should not happen (_stop() isn't called by a finalizer)
# but we check for it anyway.
if self._lock._recursion_count() > 1:
return self._reentrant_call_error()
if self._fd is None:
# not running
return

# closing the "alive" file descriptor stops main()
os.close(self._fd)
self._fd = None
def __del__(self):
# making sure child processess are cleaned before ResourceTracker
# gets destructed.
# see https://github.com/python/cpython/issues/88887
self._stop(use_blocking_lock=False)

def _stop(self, use_blocking_lock=True):
if use_blocking_lock:
with self._lock:
self._stop_locked()
else:
acquired = self._lock.acquire(blocking=False)
try:
self._stop_locked()
finally:
if acquired:
self._lock.release()

def _stop_locked(
self,
close=os.close,
waitpid=os.waitpid,
waitstatus_to_exitcode=os.waitstatus_to_exitcode,
):
# This shouldn't happen (it might when called by a finalizer)
# so we check for it anyway.
if self._lock._recursion_count() > 1:
return self._reentrant_call_error()
if self._fd is None:
# not running
return
if self._pid is None:
return

# closing the "alive" file descriptor stops main()
close(self._fd)
self._fd = None

os.waitpid(self._pid, 0)
self._pid = None
waitpid(self._pid, 0)
self._pid = None

def getfd(self):
self.ensure_running()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixing multiprocessing Resource Tracker process leaking, usually observed when running Python as PID 1.
Loading
0