Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
16 changes: 16 additions & 0 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ def _reentrant_call_error(self):
raise ReentrantCallError(
"Reentrant call into the multiprocessing resource tracker")

def __del__(self):
# making sure child processess are cleaned before ResourceTracker
# gets destructed.
# see https://github.com/python/cpython/issues/88887
try:
self._stop()
except AttributeError:
# AttributeError is likely caused by module teardown
# > __del__() can be executed during interpreter shutdown. As a
# > consequence, the global variables it needs to access (including
# > other modules) may already have been deleted or set to None.
# see https://docs.python.org/3/reference/datamodel.html#object.__del__
pass

def _stop(self):
with self._lock:
# This should not happen (_stop() isn't called by a finalizer)
Expand All @@ -84,6 +98,8 @@ def _stop(self):
if self._fd is None:
# not running
return
if self._pid is None:
return

# closing the "alive" file descriptor stops main()
os.close(self._fd)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixing multiprocessing Resource Tracker process leaking
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fixing multiprocessing Resource Tracker process leaking
Fixing multiprocessing Resource Tracker process leaking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the issue specific to Python running as the pid 1? If yes, it might be useful to mention it in the Changelog entry.

Copy link
Contributor Author

@luccabb luccabb Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the leaking happens regardless of PID 1. Python relies on kernel's process handling code to clean up the process, which works fine for most cases.

A case where it won't work is when running Python as PID 1 and there's no signal processing handler to reap zombies on client code

Loading