From 7c2a0547b5f0c474a9b8de4c370b9185850ed4a4 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 25 Sep 2024 22:07:25 +0300 Subject: [PATCH] Fix saving items was impossible if database.json.tmp was persists for some reason --- src/studiolibrary/utils.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/studiolibrary/utils.py b/src/studiolibrary/utils.py index 97d55410..2783b975 100644 --- a/src/studiolibrary/utils.py +++ b/src/studiolibrary/utils.py @@ -23,6 +23,7 @@ import tempfile import platform import threading +import time import traceback import collections import distutils.version @@ -789,9 +790,15 @@ def write(path, data): # Use the tmp file to check for concurrent writes if os.path.exists(tmp): - msg = "The path is locked for writing and cannot be accessed {}" - msg = msg.format(tmp) - raise IOError(msg) + if time.time() - os.path.getmtime(tmp) > 60: + # file might remain after an abrupt program termination or partial + # synchronization with a remote storage, if the file is still + # open, there will be exception on deletion + silentRemove(tmp) + else: + msg = "The path is locked for writing and cannot be accessed {}" + msg = msg.format(tmp) + raise IOError(msg) # Safely write the data to a tmp file and then rename to the given path try: