From 10cf10c16c145831f463c1bdad29863a60130164 Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Tue, 5 Dec 2023 06:48:53 +1100 Subject: [PATCH] fix(db): fix bug with releasing without lock in db.clean() --- .../services/shared/sqlite/sqlite_database.py | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/invokeai/app/services/shared/sqlite/sqlite_database.py b/invokeai/app/services/shared/sqlite/sqlite_database.py index 7a4a471d89..e60c8ee530 100644 --- a/invokeai/app/services/shared/sqlite/sqlite_database.py +++ b/invokeai/app/services/shared/sqlite/sqlite_database.py @@ -31,19 +31,18 @@ class SqliteDatabase: self.conn.execute("PRAGMA foreign_keys = ON;") def clean(self) -> None: - try: - if self.db_path == sqlite_memory: - return - initial_db_size = Path(self.db_path).stat().st_size - self.lock.acquire() - self.conn.execute("VACUUM;") - self.conn.commit() - final_db_size = Path(self.db_path).stat().st_size - freed_space_in_mb = round((initial_db_size - final_db_size) / 1024 / 1024, 2) - if freed_space_in_mb > 0: - self._logger.info(f"Cleaned database (freed {freed_space_in_mb}MB)") - except Exception as e: - self._logger.error(f"Error cleaning database: {e}") - raise - finally: - self.lock.release() + with self.lock: + try: + if self.db_path == sqlite_memory: + return + initial_db_size = Path(self.db_path).stat().st_size + self.lock.acquire() + self.conn.execute("VACUUM;") + self.conn.commit() + final_db_size = Path(self.db_path).stat().st_size + freed_space_in_mb = round((initial_db_size - final_db_size) / 1024 / 1024, 2) + if freed_space_in_mb > 0: + self._logger.info(f"Cleaned database (freed {freed_space_in_mb}MB)") + except Exception as e: + self._logger.error(f"Error cleaning database: {e}") + raise