diff --git a/moonraker/eventloop.py b/moonraker/eventloop.py index 44eb897..22b710b 100644 --- a/moonraker/eventloop.py +++ b/moonraker/eventloop.py @@ -9,6 +9,8 @@ import asyncio import inspect import functools import socket +import time +import logging from typing import ( TYPE_CHECKING, Awaitable, @@ -31,7 +33,7 @@ class EventLoop: self.reset() def reset(self) -> None: - self.aioloop = asyncio.get_event_loop() + self.aioloop = self._create_new_loop() self.add_signal_handler = self.aioloop.add_signal_handler self.remove_signal_handler = self.aioloop.remove_signal_handler self.add_reader = self.aioloop.add_reader @@ -45,6 +47,21 @@ class EventLoop: self.set_debug = self.aioloop.set_debug self.is_running = self.aioloop.is_running + def _create_new_loop(self) -> asyncio.AbstractEventLoop: + for _ in range(5): + # Sometimes the new loop does not properly instantiate. + # Give 5 attempts before raising an exception + new_loop = asyncio.new_event_loop() + if not new_loop.is_closed(): + break + logging.info("Failed to create open eventloop, " + "retyring in .5 seconds...") + time.sleep(.5) + else: + raise RuntimeError("Unable to create new open eventloop") + asyncio.set_event_loop(new_loop) + return new_loop + def register_callback(self, callback: FlexCallback, *args, diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index ba3cd1e..5588243 100755 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -493,18 +493,6 @@ def main(cmd_line_args: argparse.Namespace) -> None: # it is ok to use a blocking sleep here time.sleep(.5) logging.info("Attempting Server Restart...") - for _ in range(5): - # Sometimes the new loop does not properly instantiate. - # Give 5 attempts before raising an exception - new_loop = asyncio.new_event_loop() - if not new_loop.is_closed(): - break - logging.info("Failed to create open eventloop, " - "retyring in .5 seconds...") - time.sleep(.5) - else: - raise RuntimeError("Unable to create new open eventloop") - asyncio.set_event_loop(new_loop) event_loop.reset() event_loop.close() logging.info("Server Shutdown")