repoze/repoze.lru — Issue #10: LRUCache may fail to release lock
Author: snordhausen
Created: 2012-01-23 09:38:13+00:00
State: open

Locking in LRUCache is done like this:

self.lock.acquire()
try:
something()
finally:
self.lock.release()

This will fail to release the lock in case an exception occurs after the acquire() but before the try: block is entered. Such an exception could be a KeyboardError that is caused by somebody pressing CTRL+C on a program running in a terminal.

To check that this really happens, I wrote the below program and pressed CTRL+C while it was running. Failed 7 out of 10 times for me. Much less likely to happen in a real program, but still a race condition.

#!/usr/bin/python
import threading
LOCK = threading.Lock()
def run():
while 1:
LOCK.acquire()
try:
pass
finally:
LOCK.release()
if __name__ == "__main__":
try:
run()
finally:
if LOCK.locked():
print "ooops"
else:
print "OK"