Skip to content

Commit 3bbbe62

Browse files
committed
Also use lock when initializing db manager
1 parent 05bc7e9 commit 3bbbe62

File tree

1 file changed

+15
-14
lines changed
  • src/calibre/devices/kobo

1 file changed

+15
-14
lines changed

src/calibre/devices/kobo/db.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ def connect(path: str = path_on_device) -> None:
5656
self.dbpath = path
5757
self.needs_copy = True
5858
self.use_row_factory = True
59-
try:
60-
connect()
61-
self.needs_copy = False
62-
except apsw.IOError:
63-
debug_print(f'Kobo: I/O error connecting to {self.path_on_device} copying it into temporary storage and connecting there')
64-
with open(self.path_on_device, 'rb') as src, PersistentTemporaryFile(suffix='-kobo-db.sqlite') as dest:
65-
shutil.copyfileobj(src, dest)
66-
wal = self.path_on_device + '-wal'
67-
if os.path.exists(wal):
68-
shutil.copy2(wal, dest.name + '-wal')
59+
with kobo_db_lock:
6960
try:
70-
connect(dest.name)
71-
except Exception:
72-
os.remove(dest.name)
73-
raise
61+
connect()
62+
self.needs_copy = False
63+
except apsw.IOError:
64+
debug_print(f'Kobo: I/O error connecting to {self.path_on_device} copying it into temporary storage and connecting there')
65+
with open(self.path_on_device, 'rb') as src, PersistentTemporaryFile(suffix='-kobo-db.sqlite') as dest:
66+
shutil.copyfileobj(src, dest)
67+
wal = self.path_on_device + '-wal'
68+
if os.path.exists(wal):
69+
shutil.copy2(wal, dest.name + '-wal')
70+
try:
71+
connect(dest.name)
72+
except Exception:
73+
os.remove(dest.name)
74+
raise
7475

7576
def __enter__(self) -> apsw.Connection:
7677
kobo_db_lock.acquire()

0 commit comments

Comments
 (0)