Skip to content

Commit 9cd20ef

Browse files
committed
Restructure code, add more comments and call os.fsync() when locking cache
1 parent b1d458f commit 9cd20ef

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

mbed/mbed.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,43 +1344,46 @@ def cache_lock(self, url):
13441344
cpath = self.url2cachedir(url)
13451345
if cpath:
13461346
lock_file = os.path.join(cpath, '.lock')
1347-
try:
1348-
if not os.path.isdir(cpath):
1349-
os.makedirs(cpath)
1350-
1351-
can_lock = False
1352-
# this loop is handling a lock file from another process if exists
1353-
for i in range(300):
1354-
if i:
1355-
time.sleep(1)
1356-
if os.path.isfile(lock_file):
1357-
try:
1358-
# lock file exists, but we need to check pid as well in case the process died
1359-
with open(lock_file, 'r', 0) as f:
1360-
pid = f.read(8)
1361-
if pid and int(pid) != os.getpid():
1362-
if self.pid_exists(pid):
1363-
continue
1364-
else:
1365-
info("Cache lock file exists, but process is dead. Cleaning up")
1366-
os.remove(lock_file)
1347+
if not os.path.isdir(cpath):
1348+
os.makedirs(cpath)
1349+
1350+
can_lock = False
1351+
# this loop is handling a lock file from another process if exists
1352+
for i in range(300):
1353+
if i:
1354+
time.sleep(1)
1355+
if os.path.isfile(lock_file):
1356+
try:
1357+
# lock file exists, but we need to check pid as well in case the process died
1358+
with open(lock_file, 'r', 0) as f:
1359+
pid = f.read(8)
1360+
if pid and int(pid) != os.getpid():
1361+
if self.pid_exists(pid):
1362+
info("Cache lock file exists and process %s is alive." % pid)
13671363
continue
1368-
except (IOError, OSError):
1364+
else:
1365+
info("Cache lock file exists, but process is dead. Cleaning up")
1366+
os.remove(lock_file)
13691367
continue
1370-
pass
1368+
except (IOError, OSError):
1369+
continue
1370+
pass
13711371

1372-
can_lock = True
1373-
break
1372+
can_lock = True
1373+
break
13741374

1375-
if can_lock:
1375+
if can_lock:
1376+
try:
13761377
with open(lock_file, 'wb', 0) as f:
1378+
info("Writing cache lock file for pid %s" % os.getpid())
13771379
f.write(str(os.getpid()))
13781380
f.flush()
1381+
os.fsync(f)
1382+
except (IOError, OSError):
1383+
error("Unable to lock cache dir \"%s\"" % (cpath))
13791384

1380-
else:
1381-
error("Exceeded 5 minutes limit while waiting for other process to finish caching")
1382-
except Exception:
1383-
error("Unable to lock cache dir \"%s\"" % (cpath))
1385+
else:
1386+
error("Exceeded 5 minutes limit while waiting for other process to finish caching")
13841387
return False
13851388

13861389
def cache_unlock(self, url):

0 commit comments

Comments
 (0)