Fix the metadata backup thread to more reliably flush an in_limbo id

This commit is contained in:
Kovid Goyal 2011-01-25 11:09:49 -07:00
parent 503038f39f
commit 05be08a9ee
2 changed files with 12 additions and 5 deletions

View File

@ -638,8 +638,6 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
time.sleep(2) time.sleep(2)
if mb is not None:
mb.flush()
self.hide_windows() self.hide_windows()
return True return True

View File

@ -42,6 +42,7 @@ class MetadataBackup(Thread): # {{{
def stop(self): def stop(self):
self.keep_running = False self.keep_running = False
self.flush()
# Break cycles so that this object doesn't hold references to db # Break cycles so that this object doesn't hold references to db
self.do_write = self.get_metadata_for_dump = self.clear_dirtied = \ self.do_write = self.get_metadata_for_dump = self.clear_dirtied = \
self.set_dirtied = self.db = None self.set_dirtied = self.db = None
@ -57,7 +58,10 @@ class MetadataBackup(Thread): # {{{
except: except:
# Happens during interpreter shutdown # Happens during interpreter shutdown
break break
if not self.keep_running:
break
self.in_limbo = id_
try: try:
path, mi = self.get_metadata_for_dump(id_) path, mi = self.get_metadata_for_dump(id_)
except: except:
@ -72,10 +76,10 @@ class MetadataBackup(Thread): # {{{
continue continue
# at this point the dirty indication is off # at this point the dirty indication is off
if mi is None: if mi is None:
continue continue
self.in_limbo = id_ if not self.keep_running:
break
# Give the GUI thread a chance to do something. Python threads don't # Give the GUI thread a chance to do something. Python threads don't
# have priorities, so this thread would naturally keep the processor # have priorities, so this thread would naturally keep the processor
@ -89,6 +93,9 @@ class MetadataBackup(Thread): # {{{
traceback.print_exc() traceback.print_exc()
continue continue
if not self.keep_running:
break
time.sleep(0.1) # Give the GUI thread a chance to do something time.sleep(0.1) # Give the GUI thread a chance to do something
try: try:
self.do_write(path, raw) self.do_write(path, raw)
@ -102,6 +109,7 @@ class MetadataBackup(Thread): # {{{
prints('Failed to write backup metadata for id:', id_, prints('Failed to write backup metadata for id:', id_,
'again, giving up') 'again, giving up')
continue continue
self.in_limbo = None self.in_limbo = None
def flush(self): def flush(self):
@ -111,6 +119,7 @@ class MetadataBackup(Thread): # {{{
self.db.dirtied([self.in_limbo]) self.db.dirtied([self.in_limbo])
except: except:
traceback.print_exc() traceback.print_exc()
self.in_limbo = None
def write(self, path, raw): def write(self, path, raw):
with lopen(path, 'wb') as f: with lopen(path, 'wb') as f: