Repository navigation
Open
Description
Describe the bug
Sometimes I need to acquire and release the same lock with different objects. I am constricted by a legacy design to do it this way, because acquiring and releasing the lock will be in separate processes. However, if I open a second handle to the same lock key, I can't release the lock from there.
To Reproduce
Python 3.6.5 (default, Mar 25 2020, 10:32:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pottery
>>> import redis
>>> lock = pottery.Redlock(key="test-lock", masters={redis.Redis()}, auto_release_time=40000)
>>> lock.acquire()
True
>>> lock.locked()
33200
>>> lock2 = pottery.Redlock(key="test-lock", masters={redis.Redis()}, auto_release_time=40000)
>>> lock.locked()
23395
>>> lock2.locked()
0
>>> lock2.release()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/arieroos/Workspace/dos/venv/lib/python3.6/site-packages/pottery/redlock.py", line 572, in release
redis_errors=redis_errors,
pottery.exceptions.ReleaseUnlockedLock: key='redlock:test-lock', masters=[Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>], redis_errors=[]Expected behavior
Python 3.6.5 (default, Mar 25 2020, 10:32:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pottery
>>> import redis
>>> lock = pottery.Redlock(key="test-lock", masters={redis.Redis()}, auto_release_time=40000)
>>> lock.acquire()
True
>>> lock.locked()
33200
>>> lock2 = pottery.Redlock(key="test-lock", masters={redis.Redis()}, auto_release_time=40000)
>>> lock.locked()
23395
>>> lock2.locked()
13395 # or some integer like that
>>> lock2.release()
# no exception thrownEnvironment (please complete the following information):
- OS: macOS (However, I use Linux in production)
- Python version: 3.5.6
Additional context
Currently I just ignore the Exception, as most locks will expire in time anyway. However, that is probably bad practice.
Reacted by Leandro Rebelo, Sansão, Hong Quy Nguyen, jvincent, Sigal Shaharabani, Santiago, Grant Audet, Shawn and Cam Smith
Metadata
Metadata
Assignees
Labels
No labels
Activity
Lessica commentedon Sep 20, 2021
@brainix, you assigned an uuid specific to objects and identify an lock by its uuid in redis scripts:
pottery/pottery/redlock.py
Line 305 in 47da5f8
This is weird.
According to https://redis.io/topics/distlock:
But, I think, different
Redlockobjects should share the same “random_value”.belthaZornv commentedon Oct 1, 2021
Same issue here - you'd expect a distributed lock - to be distributed; if you can't check if a lock is locked from a different resource then there isn't much use.
leandrorebelo commentedon Oct 5, 2021
Same issue here - There is no sense to release the lock with the same Redlock instance from acquiring. In my opinion, the key and master should be sufficient to release.
WeatherControl commentedon Feb 23, 2022
Can confirm, there is something wrong with the locks, deadlocking for no reason
sshahar1 commentedon Oct 13, 2022
That is certainly a problem, and it took me a while to understand why this is not working (the UUID creates uniqueness). I was hoping to use this to create some kind of a semaphore, now I wonder if I should implement it myself or just wait for the resource to be released.
sriveros95 commentedon Oct 25, 2022
I don't know if we have the same issue.
We use locks in different services (in some services they are also used in different threads), we set their key and master
We use the locks using
The same key is used between services. It normally works fine (probably when they are not using it at the same time) and then some times we get
ReleaseUnlockedLockI've added some logs to see if what was running inside and it seems it finishes running. Then would it mean another lock acquired it and released it when this one was supposed to have it?
I'll be checking more in depth to add more info, but if some one has a hint of what could be happening or what I could look for would be greatly appreciated.
Update:
Tried a simple test with different services on different machines and worked correctly, will keep looking
Update 2:
Oh I get it now
You cannot use same lock variable, you need to initialize it again so it would have to be (as in the examples I guess)
Arrow-Li commentedon Nov 29, 2022
It's weird, I got same error by below code.
Error Info:
lightning-zgc commentedon May 16, 2023
How about larger auto_release_time