From 760a0f8df538ecce87812004ecea73e0db95d99b Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 16 Sep 2020 12:15:19 -0400 Subject: [PATCH] reactor: Add explicit finalize() method to clean up reactor state The existence of a __del__() method prevents deallocation on python2 if there are circular references. Replace the __del__() method with a new finalize() call and arrange for it to be called when the main reactor is released. Signed-off-by: Kevin O'Connor --- klippy/klippy.py | 8 +++++--- klippy/reactor.py | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/klippy/klippy.py b/klippy/klippy.py index 3d090886..187b45e5 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -48,10 +48,10 @@ Printer is shutdown class Printer: config_error = configfile.error command_error = homing.CommandError - def __init__(self, bglogger, start_args): + def __init__(self, main_reactor, bglogger, start_args): self.bglogger = bglogger self.start_args = start_args - self.reactor = reactor.Reactor() + self.reactor = main_reactor self.reactor.register_callback(self._connect) self.state_message = message_startup self.in_shutdown_state = False @@ -312,11 +312,13 @@ def main(): if bglogger is not None: bglogger.clear_rollover_info() bglogger.set_rollover_info('versions', versions) - printer = Printer(bglogger, start_args) + main_reactor = reactor.Reactor() + printer = Printer(main_reactor, bglogger, start_args) res = printer.run() if res in ['exit', 'error_exit']: break time.sleep(1.) + main_reactor.finalize() logging.info("Restarting printer") start_args['start_reason'] = res diff --git a/klippy/reactor.py b/klippy/reactor.py index 07c7db19..e38d4fd9 100644 --- a/klippy/reactor.py +++ b/klippy/reactor.py @@ -178,11 +178,6 @@ class SelectReactor: util.set_nonblock(self._pipe_fds[0]) util.set_nonblock(self._pipe_fds[1]) self.register_fd(self._pipe_fds[0], self._got_pipe_signal) - def __del__(self): - if self._pipe_fds is not None: - os.close(self._pipe_fds[0]) - os.close(self._pipe_fds[1]) - self._pipe_fds = None # Greenlets def _sys_pause(self, waketime): # Pause using system sleep for when reactor not running @@ -251,6 +246,11 @@ class SelectReactor: g_next.switch() def end(self): self._process = False + def finalize(self): + if self._pipe_fds is not None: + os.close(self._pipe_fds[0]) + os.close(self._pipe_fds[1]) + self._pipe_fds = None class PollReactor(SelectReactor): def __init__(self):