From 7a5e99e7c9dfbc028fbc7af0e3b08a931ffdbfd3 Mon Sep 17 00:00:00 2001 From: Michael Moore Date: Sat, 14 Dec 2024 19:10:31 -0600 Subject: [PATCH] Fix memory leak from shared_description cache, fixes #163 --- stellarisdashboard/parsing/timeline.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/stellarisdashboard/parsing/timeline.py b/stellarisdashboard/parsing/timeline.py index 4468961..0f112df 100644 --- a/stellarisdashboard/parsing/timeline.py +++ b/stellarisdashboard/parsing/timeline.py @@ -21,6 +21,17 @@ def dump_name(name: dict): return json.dumps(name, sort_keys=True) +@cache +def _get_or_add_shared_description(session, text: str) -> datamodel.SharedDescription: + matching_description = ( + session.query(datamodel.SharedDescription) + .filter_by(text=text) + .one_or_none() + ) + if matching_description is None: + matching_description = datamodel.SharedDescription(text=text) + session.add(matching_description) + return matching_description @dataclasses.dataclass class BasicGameInfo: @@ -72,6 +83,7 @@ def process_gamestate(self, game_id: str, gamestate_dict: Dict[str, Any]): ) if config.CONFIG.debug_mode or isinstance(e, KeyboardInterrupt): raise e + _get_or_add_shared_description.cache_clear() def _check_if_gamestate_exists(self, db_game): existing_dates = {gs.date for gs in db_game.game_states} @@ -251,17 +263,8 @@ def data(self) -> Any: def extract_data_from_gamestate(self, dependencies: Dict[str, Any]): pass - @cache def _get_or_add_shared_description(self, text: str) -> datamodel.SharedDescription: - matching_description = ( - self._session.query(datamodel.SharedDescription) - .filter_by(text=text) - .one_or_none() - ) - if matching_description is None: - matching_description = datamodel.SharedDescription(text=text) - self._session.add(matching_description) - return matching_description + _get_or_add_shared_description(self._session, text) class SystemProcessor(AbstractGamestateDataProcessor):