From 05be08a9ee9a645fe63c520d98989d33c6f58209 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 25 Jan 2011 11:09:49 -0700 Subject: [PATCH] Fix the metadata backup thread to more reliably flush an in_limbo id --- src/calibre/gui2/ui.py | 2 -- src/calibre/library/caches.py | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 6a74ccd6ea..c0658536bb 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -638,8 +638,6 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ except KeyboardInterrupt: pass time.sleep(2) - if mb is not None: - mb.flush() self.hide_windows() return True diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 77e75736cf..7d6511e8a5 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -42,6 +42,7 @@ class MetadataBackup(Thread): # {{{ def stop(self): self.keep_running = False + self.flush() # Break cycles so that this object doesn't hold references to db self.do_write = self.get_metadata_for_dump = self.clear_dirtied = \ self.set_dirtied = self.db = None @@ -57,7 +58,10 @@ class MetadataBackup(Thread): # {{{ except: # Happens during interpreter shutdown break + if not self.keep_running: + break + self.in_limbo = id_ try: path, mi = self.get_metadata_for_dump(id_) except: @@ -72,10 +76,10 @@ class MetadataBackup(Thread): # {{{ continue # at this point the dirty indication is off - if mi is None: continue - self.in_limbo = id_ + if not self.keep_running: + break # Give the GUI thread a chance to do something. Python threads don't # have priorities, so this thread would naturally keep the processor @@ -89,6 +93,9 @@ class MetadataBackup(Thread): # {{{ traceback.print_exc() continue + if not self.keep_running: + break + time.sleep(0.1) # Give the GUI thread a chance to do something try: self.do_write(path, raw) @@ -102,7 +109,8 @@ class MetadataBackup(Thread): # {{{ prints('Failed to write backup metadata for id:', id_, 'again, giving up') continue - self.in_limbo = None + + self.in_limbo = None def flush(self): 'Used during shutdown to ensure that a dirtied book is not missed' @@ -111,6 +119,7 @@ class MetadataBackup(Thread): # {{{ self.db.dirtied([self.in_limbo]) except: traceback.print_exc() + self.in_limbo = None def write(self, path, raw): with lopen(path, 'wb') as f: