Skip to content

Commit

Permalink
storelocking: avoid raising a NotLocked exception while releasing the…
Browse files Browse the repository at this point in the history
… lock while handling an exception
  • Loading branch information
ThomasWaldmann committed Sep 19, 2024
1 parent cee2f06 commit dde3964
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/borg/storelocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ def __init__(self, store, exclusive=False, sleep=None, timeout=1.0, stale=30 * 6
def __enter__(self):
return self.acquire()

def __exit__(self, *exc):
self.release()
def __exit__(self, exc_type, exc_val, exc_tb):
ignore_not_found = exc_type is not None
# if there was an exception, try to release the lock,
# but don't raise another exception while trying if it was not there.
self.release(ignore_not_found=ignore_not_found)

def __repr__(self):
return f"<{self.__class__.__name__}: {self.id!r}>"
Expand Down Expand Up @@ -194,10 +197,14 @@ def acquire(self):
logger.debug("LOCK-ACQUIRE: timeout while trying to acquire a lock.")
raise LockTimeout(str(self.store))

def release(self):
def release(self, *, ignore_not_found=False):
locks = self._find_locks(only_mine=True)
if not locks:
raise NotLocked(str(self.store))
if ignore_not_found:
logger.debug("LOCK-RELEASE: trying to release lock, but none was found.")
return
else:
raise NotLocked(str(self.store))
assert len(locks) == 1
lock = locks[0]
logger.debug(f"LOCK-RELEASE: releasing lock: {lock}.")
Expand Down

0 comments on commit dde3964

Please sign in to comment.