10000 gh-88887: Cleanup `multiprocessing.resource_tracker.ResourceTracker` upon deletion by luccabb · Pull Request #130429 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-88887: Cleanup multiprocessing.resource_tracker.ResourceTracker upon deletion #130429

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 21 commits into from
Mar 20, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
non blocking acquire for __del__
  • Loading branch information
luccabb committed Mar 9, 2025
commit 98473429b989c347ff7b7ee840d90b00274941dd
54 changes: 31 additions & 23 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,41 @@ def __del__(self):
# making sure child processess are cleaned before ResourceTracker
# gets destructed.
# see https://github.com/python/cpython/issues/88887
self._stop()
self._stop(use_blocking_lock=False)

def _stop(self, close=os.close, waitpid=os.waitpid, waitstatus_to_exitcode=os.waitstatus_to_exitcode):
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
if self._pid is None:
return

# closing the "alive" file descriptor stops main()
close(self._fd)
self._fd = None
def _stop(self, use_blocking_lock=True):
if use_blocking_lock:
with self._lock:
self._cleanup()
else:
self._lock.acquire(blocking=False)
self._cleanup()
self._lock.release()

def _cleanup(self, close=os.close, waitpid=os.waitpid, waitstatus_to_exitcode=os.waitstatus_to_exitcode):
# 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
if self._pid is None:
return

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

_, status = waitpid(self._pid, 0)
_, status = waitpid(self._pid, 0)

self._pid = None
self._pid = None

try:
self._exitcode = waitstatus_to_exitcode(status)
except ValueError:
# os.waitstatus_to_exitcode may raise an exception for invalid values
self._exitcode = None
try:
self._exitcode = waitstatus_to_exitcode(status)
except ValueError:
# os.waitstatus_to_exitcode may raise an exception for invalid values
self._exitcode = None

def getfd(self):
self.ensure_running()
Expand Down
0