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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-09-16 12:15:19 -04:00
parent a3fa11ffd4
commit 760a0f8df5
2 changed files with 10 additions and 8 deletions

View File

@ -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

View File

@ -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):