summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGregory Becker <becker33@llnl.gov>2015-08-25 15:32:45 -0700
committerGregory Becker <becker33@llnl.gov>2015-08-25 15:32:45 -0700
commite32c59f805c4e8d9cb23ce9fcf2edcf571ce3949 (patch)
treed5cef31defef80543f86d728b673a28bca122817 /lib
parent4a2bd1753a3c0b96c0b38ee9ec909cbf98308125 (diff)
downloadspack-e32c59f805c4e8d9cb23ce9fcf2edcf571ce3949.tar.gz
spack-e32c59f805c4e8d9cb23ce9fcf2edcf571ce3949.tar.bz2
spack-e32c59f805c4e8d9cb23ce9fcf2edcf571ce3949.tar.xz
spack-e32c59f805c4e8d9cb23ce9fcf2edcf571ce3949.zip
Fixed file locking. Fix is slightly ugly (lock integer added) but it gets the job done
It avoids having to spin simply on the OSError.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/database.py49
1 files changed, 31 insertions, 18 deletions
diff --git a/lib/spack/spack/database.py b/lib/spack/spack/database.py
index 7e2c3ac079..5e8bb172b8 100644
--- a/lib/spack/spack/database.py
+++ b/lib/spack/spack/database.py
@@ -115,22 +115,29 @@ class Database(object):
"""
if not self.is_dirty():
return
- """
- while True:
+
+ lock=0
+ while lock==0:
try:
os.mkdir(self.lock_path)
- break
+ lock=1
except OSError as err:
pass
- """
- if os.path.isfile(self.file_path):
- with open(self.file_path,'r') as f:
- self.from_yaml(f)
- else:
+
+ #The try statement ensures that a failure won't leave the
+ #database locked to other processes.
+ try:
+ if os.path.isfile(self.file_path):
+ with open(self.file_path,'r') as f:
+ self.from_yaml(f)
+ else:
#The file doesn't exist, construct empty data.
- self.data = []
+ self.data = []
+ except:
+ os.rmdir(self.lock_path)
+ raise
-# os.rmdir(self.lock_path)
+ os.rmdir(self.lock_path)
def write_database_to_yaml(self,stream):
@@ -162,19 +169,25 @@ class Database(object):
Write the database to the standard location
Implements mkdir locking for the database file
"""
- """
- while True:
+ lock=0
+ while lock==0:
try:
os.mkdir(self.lock_path)
- break
+ lock=1
except OSError as err:
pass
- """
- with open(self.file_path,'w') as f:
- self.last_write_time = int(time.time())
- self.write_database_to_yaml(f)
- # os.rmdir(self.lock_path)
+ #The try statement ensures that a failure won't leave the
+ #database locked to other processes.
+ try:
+ with open(self.file_path,'w') as f:
+ self.last_write_time = int(time.time())
+ self.write_database_to_yaml(f)
+ except:
+ os.rmdir(self.lock_path)
+ raise
+
+ os.rmdir(self.lock_path)
def get_index_of(self, spec):