Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
9 changes: 9 additions & 0 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ def _send(self, cmd, name, rtype):
assert nbytes == len(msg), "nbytes {0:n} but len(msg) {1:n}".format(
nbytes, len(msg))

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 (OSError, TypeError, AttributeError):
Copy link
Member

Choose a reason for hiding this comment

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

I'm not confortable with these exceptions. In which case we should expect these exceptions? I would prefer a direct call to self._stop() without try/except.

_stop() should be modified to do nothing if self._pid is None.

    def _stop(self):
        with self._lock:
            if self._pid is None:
                return
            ...

Copy link
Contributor Author

@luccabb luccabb Mar 6, 2025

Choose a reason for hiding this comment

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

moved to catching AttributeError only which is likely caused by __del__:

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.

https://docs.python.org/3/reference/datamodel.html#object.del

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not catching AttributeError here fails some of the tests as the os module is already None when _stop runs

pass


_resource_tracker = ResourceTracker()
ensure_running = _resource_tracker.ensure_running
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