From ebc616de72d51f642f5d5dd4ab1f722cf14a0945 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 19 May 2024 18:25:56 +0200 Subject: [PATCH] WebHost: move atexit saving to end of room hosting function (#3339) --- MultiServer.py | 7 ++++--- WebHostLib/customserver.py | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/MultiServer.py b/MultiServer.py index 194f0a67fd6a..e95e44dd7d5c 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -508,7 +508,7 @@ def init_save(self, enabled: bool = True): self.logger.exception(e) self._start_async_saving() - def _start_async_saving(self): + def _start_async_saving(self, atexit_save: bool = True): if not self.auto_saver_thread: def save_regularly(): # time.time() is platform dependent, so using the expensive datetime method instead @@ -532,8 +532,9 @@ def get_datetime_second(): self.auto_saver_thread = threading.Thread(target=save_regularly, daemon=True) self.auto_saver_thread.start() - import atexit - atexit.register(self._save, True) # make sure we save on exit too + if atexit_save: + import atexit + atexit.register(self._save, True) # make sure we save on exit too def get_save(self) -> dict: self.recheck_hints() diff --git a/WebHostLib/customserver.py b/WebHostLib/customserver.py index ff1b19309431..8cff86dcf041 100644 --- a/WebHostLib/customserver.py +++ b/WebHostLib/customserver.py @@ -142,7 +142,7 @@ def init_save(self, enabled: bool = True): savegame_data = Room.get(id=self.room_id).multisave if savegame_data: self.set_save(restricted_loads(Room.get(id=self.room_id).multisave)) - self._start_async_saving() + self._start_async_saving(atexit_save=False) threading.Thread(target=self.listen_to_db_commands, daemon=True).start() @db_session @@ -278,6 +278,7 @@ async def start_room(room_id): raise finally: try: + ctx._save() with (db_session): # ensure the Room does not spin up again on its own, minute of safety buffer room = Room.get(id=room_id)