Browse Source

Retry pickle-saving 3x

Especially on Windows the file sometimes gets locked, and users have reported 'dictionary changed size during iteration' errors, so somewhere a Lock is missing.
pull/832/merge
Safihre 8 years ago
parent
commit
affea99cb1
  1. 50
      sabnzbd/__init__.py

50
sabnzbd/__init__.py

@ -895,18 +895,25 @@ def save_data(data, _id, path, do_pickle=True, silent=False):
logging.debug("Saving data for %s in %s", _id, path) logging.debug("Saving data for %s in %s", _id, path)
path = os.path.join(path, _id) path = os.path.join(path, _id)
try: # We try 3 times, to avoid any dict or access problems
with open(path, 'wb') as data_file: for t in xrange(3):
if do_pickle: try:
if cfg.use_pickle(): with open(path, 'wb') as data_file:
cPickle.dump(data, data_file) if do_pickle:
if cfg.use_pickle():
cPickle.dump(data, data_file)
else:
pickle.dump(data, data_file)
else: else:
pickle.dump(data, data_file) data_file.write(data)
break
except:
if t == 2:
logging.error(T('Saving %s failed'), path)
logging.info("Traceback: ", exc_info=True)
else: else:
data_file.write(data) # Wait a tiny bit before trying again
except: time.sleep(0.1)
logging.error(T('Saving %s failed'), path)
logging.info("Traceback: ", exc_info=True)
@synchronized(IO_LOCK) @synchronized(IO_LOCK)
@ -959,15 +966,22 @@ def save_admin(data, _id):
path = os.path.join(cfg.admin_dir.get_path(), _id) path = os.path.join(cfg.admin_dir.get_path(), _id)
logging.info("Saving data for %s in %s", _id, path) logging.info("Saving data for %s in %s", _id, path)
try: # We try 3 times, to avoid any dict or access problems
with open(path, 'wb') as data_file: for t in xrange(3):
if cfg.use_pickle(): try:
data = pickle.dump(data, data_file) with open(path, 'wb') as data_file:
if cfg.use_pickle():
data = pickle.dump(data, data_file)
else:
data = cPickle.dump(data, data_file)
break
except:
if t == 2:
logging.error(T('Saving %s failed'), path)
logging.info("Traceback: ", exc_info=True)
else: else:
data = cPickle.dump(data, data_file) # Wait a tiny bit before trying again
except: time.sleep(0.1)
logging.error(T('Saving %s failed'), path)
logging.info("Traceback: ", exc_info=True)
@synchronized(IO_LOCK) @synchronized(IO_LOCK)

Loading…
Cancel
Save