Skip to content

Commit 9847342

Browse files
committed
non blocking acquire for __del__
1 parent aaa8378 commit 9847342

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

Lib/multiprocessing/resource_tracker.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,41 @@ def __del__(self):
7979
# making sure child processess are cleaned before ResourceTracker
8080
# gets destructed.
8181
# see https://github.com/python/cpython/issues/88887
82-
self._stop()
82+
self._stop(use_blocking_lock=False)
8383

84-
def _stop(self, close=os.close, waitpid=os.waitpid, waitstatus_to_exitcode=os.waitstatus_to_exitcode):
85-
with self._lock:
86-
# This should not happen (_stop() isn't called by a finalizer)
87-
# but we check for it anyway.
88-
if self._lock._recursion_count() > 1:
89-
return self._reentrant_call_error()
90-
if self._fd is None:
91-
# not running
92-
return
93-
if self._pid is None:
94-
return
95-
96-
# closing the "alive" file descriptor stops main()
97-
close(self._fd)
98-
self._fd = None
84+
def _stop(self, use_blocking_lock=True):
85+
if use_blocking_lock:
86+
with self._lock:
87+
self._cleanup()
88+
else:
89+
self._lock.acquire(blocking=False)
90+
self._cleanup()
91+
self._lock.release()
92+
93+
def _cleanup(self, close=os.close, waitpid=os.waitpid, waitstatus_to_exitcode=os.waitstatus_to_exitcode):
94+
# This should not happen (_stop() isn't called by a finalizer)
95+
# but we check for it anyway.
96+
if self._lock._recursion_count() > 1:
97+
return self._reentrant_call_error()
98+
if self._fd is None:
99+
# not running
100+
return
101+
if self._pid is None:
102+
return
103+
104+
# closing the "alive" file descriptor stops main()
105+
close(self._fd)
106+
self._fd = None
99107

100-
_, status = waitpid(self._pid, 0)
108+
_, status = waitpid(self._pid, 0)
101109

102-
self._pid = None
110+
self._pid = None
103111

104-
try:
105-
self._exitcode = waitstatus_to_exitcode(status)
106-
except ValueError:
107-
# os.waitstatus_to_exitcode may raise an exception for invalid values
108-
self._exitcode = None
112+
try:
113+
self._exitcode = waitstatus_to_exitcode(status)
114+
except ValueError:
115+
# os.waitstatus_to_exitcode may raise an exception for invalid values
116+
self._exitcode = None
109117

110118
def getfd(self):
111119
self.ensure_running()

0 commit comments

Comments
 (0)