From 3f2d0d9ac23b7cf55b6e1a975d013396df536f8e Mon Sep 17 00:00:00 2001 From: Steve Loranz Date: Thu, 27 Mar 2014 10:37:19 -0500 Subject: [PATCH] Throw exception instead of blocking forever Change-Id: Iee7a264b41c8cd23c88c3d77e4abc9a059a43314 --- novaimagebuilder/CacheManager.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/novaimagebuilder/CacheManager.py b/novaimagebuilder/CacheManager.py index b56a8b3..9f844eb 100644 --- a/novaimagebuilder/CacheManager.py +++ b/novaimagebuilder/CacheManager.py @@ -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): """