From 7f503018ba05b8da16d3bbd9156de1e61e0b403e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 1 May 2022 09:43:59 +0530 Subject: [PATCH] Parallelize db shutdown a bit more --- src/calibre/db/backend.py | 4 ++++ src/calibre/db/cache.py | 3 ++- src/calibre/db/fts/pool.py | 10 ++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index a12e00b6fa..b0cfc22995 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -980,6 +980,10 @@ class DB: def shutdown_fts(self): if self.fts_enabled: self.fts.shutdown() + + def join_fts(self): + if self.fts: + self.fts.pool.join() self.fts = None def get_connection(self): diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 144d19b119..0d56141008 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -2387,6 +2387,7 @@ class Cache: self.close_called = True self.shutting_down = True self.event_dispatcher.close() + self.backend.shutdown_fts() if self.fts_queue_thread is not None: self.fts_job_queue.put(None) from calibre.customize.ui import available_library_closed_plugins @@ -2398,10 +2399,10 @@ class Cache: traceback.print_exc() # the fts supervisor thread could be in the middle of committing a # result to the db, so holding a lock here will cause a deadlock - self.backend.shutdown_fts() if self.fts_queue_thread is not None: self.fts_queue_thread.join() self.fts_queue_thread = None + self.backend.join_fts() with self.write_lock: self.backend.close() diff --git a/src/calibre/db/fts/pool.py b/src/calibre/db/fts/pool.py index 675dd7addd..6e0893e06d 100644 --- a/src/calibre/db/fts/pool.py +++ b/src/calibre/db/fts/pool.py @@ -210,11 +210,13 @@ class Pool: w.keep_going = False for i in range(2*len(self.workers)): self.jobs_queue.put(quit) - self.supervisor_thread.join() - for w in self.workers: - w.join() - self.workers = [] self.initialized.clear() + + def join(self): + self.supervisor_thread.join() + for w in self.workers: + w.join() + self.workers = [] # }}} def do_check_for_work(self):