Check if session id was already registered. If not, create a new session id.
Load data and acquire lock.
Save data and release lock. Update the cache.
If clean up happens between steps two and three, the session id will be in the locks dict, but not in the cache dict, therefore clean up will remove lock id from the dict and when request's thread tries to release the lock, it will raise an exception:
The fix checks whether it's possible to acquire given lock. If so, the lock id is removed, if not, nothing happens, request's thread will take care of that lock.
I'm getting hit by this problem at the moment is there any reason this pull request isn't being merged? I can pick up where @shroom left off if work needs to be done on it, it looks good too my untrained eye at the moment.