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 6a28320 commit d322889
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 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,15 +197,19 @@ 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):
self.last_refresh_dt = None
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

Check warning on line 206 in src/borg/storelocking.py

View check run for this annotation

Codecov / codecov/patch

src/borg/storelocking.py#L205-L206

Added lines #L205 - L206 were not covered by tests
else:
raise NotLocked(str(self.store))
assert len(locks) == 1
lock = locks[0]
logger.debug(f"LOCK-RELEASE: releasing lock: {lock}.")
self._delete_lock(lock["key"], ignore_not_found=True)
self.last_refresh_dt = None

def got_exclusive_lock(self):
locks = self._find_locks(only_mine=True, only_exclusive=True)
Expand Down

0 comments on commit d322889

Please sign in to comment.