From dcd54553883b72428857cdc33339d04324e38579 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Tue, 14 Jul 2020 05:45:03 +0000 Subject: [PATCH] Fix crash on starting a run while the DB is locked --- qwarc/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/qwarc/__init__.py b/qwarc/__init__.py index 7023ef4..de51581 100644 --- a/qwarc/__init__.py +++ b/qwarc/__init__.py @@ -331,7 +331,17 @@ class QWARC: self._db = sqlite3.connect(self._dbPath, timeout = 1) self._db.isolation_level = None # Transactions are handled manually below. - self._db.execute('PRAGMA synchronous = OFF') + + # Setting the synchronous PRAGMA is not possible if the DB is currently locked by another process but also can't be done inside a transaction... So just retry until it succeeds. + while True: + try: + self._db.execute('PRAGMA synchronous = OFF') + except sqlite3.OperationalError as e: + if str(e) != 'database is locked': + raise + await asyncio.sleep(1) + else: + break async with self.exclusive_db_lock() as cursor: cursor.execute('SELECT name FROM sqlite_master WHERE type = "table" AND name = "items"')