From 2d5914b35842128757c0c88f0062f4c57ef0fe21 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Thu, 4 Nov 2021 14:55:52 -0400 Subject: [PATCH] moonraker: attempt to resolve "event loop closed" errors on restart Occasionally a server restart will fail as the new loop instantiates as closed. This seems to be an issue with asyncio, attempt to work around by retyring to create a new loop. Signed-off-by: Eric Callahan --- moonraker/moonraker.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index 8156059..5fff93b 100755 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -845,7 +845,18 @@ def main() -> None: # it is ok to use a blocking sleep here time.sleep(.5) logging.info("Attempting Server Restart...") - asyncio.set_event_loop(asyncio.new_event_loop()) + 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 = EventLoop() event_loop.close() logging.info("Server Shutdown")