From 53a3c641864915a52cefc9fb0d0955723d2e88f3 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sat, 16 Jul 2022 19:41:09 -0400 Subject: [PATCH] moonraker: move loop creation to the eventloop module Python 3.10 deprecates "get_event_loop()", so it is necessary to create a new loop and set it as active at startup. Previously Moonraker only did this after a soft restart. Signed-off-by: Eric Callahan --- moonraker/eventloop.py | 19 ++++++++++++++++++- moonraker/moonraker.py | 12 ------------ 2 files changed, 18 insertions(+), 13 deletions(-) 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")