Skip to content

Commit f15ce2d

Browse files
TheBluskydvora-h
authored andcommitted
Adding lock_name to LockError (#3023)
* Adding lock_name to LockError * Adding lock_name to LockError --------- Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
1 parent 90805cc commit f15ce2d

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Add an optional lock_name attribute to LockError.
12
* Fix return types for `get`, `set_path` and `strappend` in JSONCommands
23
* Connection.register_connect_callback() is made public.
34
* Fix async `read_response` to use `disable_decoding`.

redis/exceptions.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ class LockError(RedisError, ValueError):
8181
"Errors acquiring or releasing a lock"
8282
# NOTE: For backwards compatibility, this class derives from ValueError.
8383
# This was originally chosen to behave like threading.Lock.
84-
pass
84+
85+
def __init__(self, message, lock_name=None):
86+
self.message = message
87+
self.lock_name = lock_name
8588

8689

8790
class LockNotOwnedError(LockError):

redis/lock.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ def register_scripts(self) -> None:
157157
def __enter__(self) -> "Lock":
158158
if self.acquire():
159159
return self
160-
raise LockError("Unable to acquire lock within the time specified")
160+
raise LockError(
161+
"Unable to acquire lock within the time specified",
162+
lock_name=self.name,
163+
)
161164

162165
def __exit__(
163166
self,
@@ -248,15 +251,18 @@ def release(self) -> None:
248251
"""
249252
expected_token = self.local.token
250253
if expected_token is None:
251-
raise LockError("Cannot release an unlocked lock")
254+
raise LockError("Cannot release an unlocked lock", lock_name=self.name)
252255
self.local.token = None
253256
self.do_release(expected_token)
254257

255258
def do_release(self, expected_token: str) -> None:
256259
if not bool(
257260
self.lua_release(keys=[self.name], args=[expected_token], client=self.redis)
258261
):
259-
raise LockNotOwnedError("Cannot release a lock that's no longer owned")
262+
raise LockNotOwnedError(
263+
"Cannot release a lock that's no longer owned",
264+
lock_name=self.name,
265+
)
260266

261267
def extend(self, additional_time: int, replace_ttl: bool = False) -> bool:
262268
"""
@@ -270,9 +276,9 @@ def extend(self, additional_time: int, replace_ttl: bool = False) -> bool:
270276
`additional_time`.
271277
"""
272278
if self.local.token is None:
273-
raise LockError("Cannot extend an unlocked lock")
279+
raise LockError("Cannot extend an unlocked lock", lock_name=self.name)
274280
if self.timeout is None:
275-
raise LockError("Cannot extend a lock with no timeout")
281+
raise LockError("Cannot extend a lock with no timeout", lock_name=self.name)
276282
return self.do_extend(additional_time, replace_ttl)
277283

278284
def do_extend(self, additional_time: int, replace_ttl: bool) -> bool:
@@ -284,17 +290,23 @@ def do_extend(self, additional_time: int, replace_ttl: bool) -> bool:
284290
client=self.redis,
285291
)
286292
):
287-
raise LockNotOwnedError("Cannot extend a lock that's no longer owned")
293+
raise LockNotOwnedError(
294+
"Cannot extend a lock that's no longer owned",
295+
lock_name=self.name,
296+
)
288297
return True
289298

290299
def reacquire(self) -> bool:
291300
"""
292301
Resets a TTL of an already acquired lock back to a timeout value.
293302
"""
294303
if self.local.token is None:
295-
raise LockError("Cannot reacquire an unlocked lock")
304+
raise LockError("Cannot reacquire an unlocked lock", lock_name=self.name)
296305
if self.timeout is None:
297-
raise LockError("Cannot reacquire a lock with no timeout")
306+
raise LockError(
307+
"Cannot reacquire a lock with no timeout",
308+
lock_name=self.name,
309+
)
298310
return self.do_reacquire()
299311

300312
def do_reacquire(self) -> bool:
@@ -304,5 +316,8 @@ def do_reacquire(self) -> bool:
304316
keys=[self.name], args=[self.local.token, timeout], client=self.redis
305317
)
306318
):
307-
raise LockNotOwnedError("Cannot reacquire a lock that's no longer owned")
319+
raise LockNotOwnedError(
320+
"Cannot reacquire a lock that's no longer owned",
321+
lock_name=self.name,
322+
)
308323
return True

tests/test_lock.py

+7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ def test_context_manager_reacquiring_lock_no_longer_owned_raises_error(self, r):
242242
with self.get_lock(r, "foo", timeout=10, blocking=False):
243243
r.set("foo", "a")
244244

245+
def test_lock_error_gives_correct_lock_name(self, r):
246+
r.set("foo", "bar")
247+
with pytest.raises(LockError) as excinfo:
248+
with self.get_lock(r, "foo", blocking_timeout=0.1):
249+
pass
250+
assert excinfo.value.lock_name == "foo"
251+
245252

246253
class TestLockClassSelection:
247254
def test_lock_class_argument(self, r):

0 commit comments

Comments
 (0)