-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug in Lock's release() function #415
Comments
i haven't tried your proposed code change, but it looks like it skips deleting the lock if the timeout hasn't passed yet, so the that said, i do agree there's a bug with locks that have timeouts. as currently coded, once the timeout has expired on a lock instance, that lock instance is in an inconsistent state and shouldn't be used to release the lock. no attempt is made to detect this case in TL;DR: i think storing just a timestamp in redis is insufficient to determine whether a lock was lost and acquired by another process when using timeouts. i just opened #416, which covers a similar (but not identical) case. edit: decreased snark |
@chillipino import redis
import time
r = redis.StrictRedis(host = '192.168.56.1', port = 6379, db = 0)
lock = r.lock('hash:lock', 5)
print 'first lock acquire: ', lock.acquire()
print 'current time: ', time.time()
print 'lock value: ', r.get('hash:lock')
print 'release lock'
print 'current time: ', time.time()
lock.release()
print 'lock value: ', r.get('hash:lock') and the test result shows like this: as the test result shows, when a lock is released before timeout, the key is deleted as designed. |
ah, i missed the == case, sorry about that - i do have a hard time believing that code that has been in use for years could be fixed by flipping a comparison. :) it does still attempt to delete the key if it does not exist, though. actually, if another machine happens to acquire the lock between the i still think the best thing to do for locking is to store a value in the server that doesn't depend on clock synchronization across all clients. dealing with floating point serialization, parsing, and comparison makes this worse, especially when the clients involved may be running on different platforms and architectures. there's even an example above - |
The new lock system is out in 2.10. Thanks for your input. |
there is a bug in Lock's release() function.
below is a test script.
and the result is:
first lock acquire: True
current time: 1387347026.74
lock value: 1387347031.729
second_lock acquire: True
current time: 1387347031.75
lock value: 1387347041.741
release first lock
lock value: 1387347041.741
current time: 1387347031.75
third_lock acquire: True
lock value: 1387347041.741
as the result shows, the third lock got the lock, but at the time the second lock is not released.
why?
look at the release() function, there is a check if should delete the key:
as the test script describes: first lock acquire , then second acquire, then first acquire release, but we should not delete the key because the lock is acquired by second lock, the ower is second lock not first lock, so at that time the first lock's release method should do nothing.
may be the logic in release function is wrong, i think we should change it as follows:
The text was updated successfully, but these errors were encountered: