Skip to content
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

Replace AsyncProcess exit handler by weakref.finalize #4184

Merged
merged 5 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 15 additions & 18 deletions distributed/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,26 @@ def __init__(self, loop=None, target=None, name=None, args=(), kwargs={}):
dask.config.global_config,
),
)
_dangling.add(self._process)
self._name = self._process.name
self._proc_finalizer = weakref.finalize(self, self._get_finalizer())
jcrist marked this conversation as resolved.
Show resolved Hide resolved
self._watch_q = PyQueue()
self._exit_future = Future()
self._exit_callback = None
self._closed = False

self._start_threads()

def _get_finalizer(self):
def finalize(proc=self._process, r=repr(self)):
jcrist marked this conversation as resolved.
Show resolved Hide resolved
if proc.is_alive():
try:
logger.info("reaping stray process %s" % (proc,))
proc.terminate()
except OSError:
pass

return finalize

def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self._name)

Expand Down Expand Up @@ -118,8 +129,8 @@ def stop_thread(q):
# We don't join the thread here as a finalizer can be called
# asynchronously from anywhere

self._finalizer = weakref.finalize(self, stop_thread, q=self._watch_q)
self._finalizer.atexit = False
self._thread_finalizer = weakref.finalize(self, stop_thread, q=self._watch_q)
self._thread_finalizer.atexit = False

def _on_exit(self, exitcode):
# Called from the event loop when the child process exited
Expand Down Expand Up @@ -292,7 +303,7 @@ def close(self):
immediately and does not ensure the child process has exited.
"""
if not self._closed:
self._finalizer()
self._thread_finalizer()
self._process = None
self._closed = True

Expand Down Expand Up @@ -332,17 +343,3 @@ def daemon(self):
@daemon.setter
def daemon(self, value):
self._process.daemon = value


_dangling = weakref.WeakSet()


@atexit.register
def _cleanup_dangling():
for proc in list(_dangling):
if proc.is_alive():
try:
logger.info("reaping stray process %s" % (proc,))
proc.terminate()
except OSError:
pass
2 changes: 0 additions & 2 deletions distributed/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from .core import connect, rpc, CommClosedError, Status
from .deploy import SpecCluster
from .metrics import time
from .process import _cleanup_dangling
from .proctitle import enable_proctitle_on_children
from .security import Security
from .utils import (
Expand Down Expand Up @@ -1451,7 +1450,6 @@ def check_process_leak(check=True):
else:
assert not mp_context.active_children()

_cleanup_dangling()
for proc in mp_context.active_children():
proc.terminate()

Expand Down