Skip to content

Commit

Permalink
Merge pull request #93 from twidi/feature/locking-bug-with-redis-py-2.10
Browse files Browse the repository at this point in the history
Resolve a redis-py bug with decode_responses=True
  • Loading branch information
twidi committed Dec 16, 2015
2 parents 31316e0 + 520ee8e commit fddad21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
V 0.2.4 2015-12-16

* Locally solve a locking bug in redis-py

V 0.2.3 2015-12-16

* Compatibility with Redis-py 2.10
Expand Down
2 changes: 1 addition & 1 deletion limpyd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
power and the control of the Redis API, in a limpid way, with just as
abstraction as needed."""

VERSION = (0, 2, 3)
VERSION = (0, 2, 4)

__author__ = 'Yohan Boniface'
__contact__ = "[email protected]"
Expand Down
19 changes: 19 additions & 0 deletions limpyd/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,22 @@ def has_scripting(self):
except:
self._has_scripting = False
return self._has_scripting


class Lock(redis.client.Lock):
"""
Override the default Lock class to manage the fact that we use ``decode_responses=True``
but it is not taken into account in python 3 with redis 2.10, as the lock token in
``threading.local`` is saved as ``bytes`` but the value retrieved from redis is decoded.
So the equal check doesn't work.
So we decode the value in ``threading.local`` to make this check work.
See https://github.com/andymccurdy/redis-py/issues/694
"""

def do_release(self, expected_token):

if isinstance(expected_token, bytes) and \
self.redis.connection_pool.connection_kwargs.get('decode_responses', False):
expected_token = expected_token.decode()

super(Lock, self).do_release(expected_token)
2 changes: 1 addition & 1 deletion limpyd/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from copy import copy

from redis.exceptions import RedisError
from redis.client import Lock

from limpyd.database import Lock
from limpyd.utils import make_key, normalize
from limpyd.exceptions import *

Expand Down

0 comments on commit fddad21

Please sign in to comment.