Use rmtree with retry when deleting after library move

Hopefully makes test failures more rare
This commit is contained in:
Kovid Goyal 2020-07-16 09:43:13 +05:30
parent b3a3eace49
commit 873d6080fd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -386,6 +386,16 @@ def set_global_state(backend):
backend.library_id, (), precompiled_user_functions=backend.get_user_template_functions()) backend.library_id, (), precompiled_user_functions=backend.get_user_template_functions())
def rmtree_with_retry(path, sleep_time=1):
try:
shutil.rmtree(path)
except EnvironmentError as e:
if e.errno == errno.ENOENT and not os.path.exists(path):
return
time.sleep(sleep_time) # In case something has temporarily locked a file
shutil.rmtree(path)
class DB(object): class DB(object):
PATH_LIMIT = 40 if iswindows else 100 PATH_LIMIT = 40 if iswindows else 100
@ -1216,15 +1226,7 @@ class DB(object):
def rmtree(self, path): def rmtree(self, path):
if self.is_deletable(path): if self.is_deletable(path):
try: rmtree_with_retry(path)
shutil.rmtree(path)
except EnvironmentError as e:
if e.errno == errno.ENOENT and not os.path.exists(path):
return
import traceback
traceback.print_exc()
time.sleep(1) # In case something has temporarily locked a file
shutil.rmtree(path)
def construct_path_name(self, book_id, title, author): def construct_path_name(self, book_id, title, author):
''' '''
@ -1980,7 +1982,7 @@ class DB(object):
self._conn = None self._conn = None
for loc in old_dirs: for loc in old_dirs:
try: try:
shutil.rmtree(loc) rmtree_with_retry(loc)
except EnvironmentError as e: except EnvironmentError as e:
if os.path.exists(loc): if os.path.exists(loc):
prints('Failed to delete:', loc, 'with error:', as_unicode(e)) prints('Failed to delete:', loc, 'with error:', as_unicode(e))