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 <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-11-04 14:55:52 -04:00 committed by Eric Callahan
parent c718e9d1c3
commit 2d5914b358
1 changed files with 12 additions and 1 deletions

View File

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