Skip to content

Commit

Permalink
Merge pull request #7941 from ThomasWaldmann/lockroster-remove-fix-ma…
Browse files Browse the repository at this point in the history
…ster

LockRoster.modify: no KeyError if element was already gone, fixes #7937
  • Loading branch information
ThomasWaldmann committed Nov 18, 2023
2 parents 7c20bb5 + 2d86b7e commit 6a5feaf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/borg/locking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .helpers import Error, ErrorWithTraceback
from .logger import create_logger

ADD, REMOVE = "add", "remove"
ADD, REMOVE, REMOVE2 = "add", "remove", "remove2"
SHARED, EXCLUSIVE = "shared", "exclusive"

logger = create_logger(__name__)
Expand Down Expand Up @@ -326,6 +326,11 @@ def modify(self, key, op):
if op == ADD:
elements.add(self.id)
elif op == REMOVE:
# note: we ignore it if the element is already not present anymore.
# this has been frequently seen in teardowns involving Repository.__del__ and Repository.__exit__.
elements.discard(self.id)
elif op == REMOVE2:
# needed for callers that do not want to ignore.
elements.remove(self.id)
else:
raise ValueError("Unknown LockRoster op %r" % op)
Expand All @@ -340,7 +345,7 @@ def migrate_lock(self, key, old_id, new_id):
killing, self.kill_stale_locks = self.kill_stale_locks, False
try:
try:
self.modify(key, REMOVE)
self.modify(key, REMOVE2)
except KeyError:
# entry was not there, so no need to add a new one, but still update our id
self.id = new_id
Expand Down
3 changes: 2 additions & 1 deletion src/borg/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datetime import datetime, timezone
from functools import partial
from itertools import islice
from typing import Callable, DefaultDict

from .constants import * # NOQA
from .hashindex import NSIndexEntry, NSIndex, NSIndex1, hashindex_variant
Expand Down Expand Up @@ -48,7 +49,7 @@
# may not be able to handle the new tags.
MAX_TAG_ID = 15

FreeSpace = partial(defaultdict, int)
FreeSpace: Callable[[], DefaultDict] = partial(defaultdict, int)


def header_size(tag):
Expand Down
2 changes: 1 addition & 1 deletion src/borg/testsuite/locking.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_kill_stale(self, lockpath, free_pid):
assert roster.get(SHARED) == {our_id}
assert roster.get(EXCLUSIVE) == set()
assert roster.get(SHARED) == set()
with pytest.raises(KeyError):
with pytest.raises(NotLocked):
dead_lock.release()

with Lock(lockpath, id=cant_know_if_dead_id, exclusive=True):
Expand Down

0 comments on commit 6a5feaf

Please sign in to comment.