Skip to content

Commit

Permalink
Throw exception instead of blocking forever
Browse files Browse the repository at this point in the history
Change-Id: Iee7a264b41c8cd23c88c3d77e4abc9a059a43314
  • Loading branch information
steveloranz committed Mar 27, 2014
1 parent 9f15fce commit 3f2d0d9
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions novaimagebuilder/CacheManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@ def lock_and_get_index(self):
# We acquire a thread lock under all circumstances
# This is the safest approach and should be relatively harmless if we are used
# as a module in a non-threaded Python program
self.INDEX_THREAD_LOCK.acquire()
# atomic create if not present
fd = os.open(self.index_filename, os.O_RDWR | os.O_CREAT)
# blocking
fcntl.flock(fd, fcntl.LOCK_EX)
self.index_file = os.fdopen(fd, "r+")
index = self.index_file.read()
if len(index) == 0:
# Empty - possibly because we created it earlier - create empty dict
self.index = { }
if self.INDEX_THREAD_LOCK.acquire(False):
# atomic create if not present
fd = os.open(self.index_filename, os.O_RDWR | os.O_CREAT)
# blocking
fcntl.flock(fd, fcntl.LOCK_EX)
self.index_file = os.fdopen(fd, "r+")
index = self.index_file.read()
if len(index) == 0:
# Empty - possibly because we created it earlier - create empty dict
self.index = {}
else:
self.index = json.loads(index)
else:
self.index = json.loads(index)
raise Exception("Failed to acquire threading lock...")

def write_index_and_unlock(self):
"""
Expand Down

0 comments on commit 3f2d0d9

Please sign in to comment.