Skip to content

Commit e72ef63

Browse files
committed
Add filesystem error handling during cache_lock()
1 parent 2c8ccf0 commit e72ef63

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

mbed/mbed.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,17 +1351,24 @@ def cache_lock(self, url):
13511351
can_lock = False
13521352
# this loop is handling a lock file from another process if exists
13531353
for i in range(300):
1354-
if os.path.exists(lock_file):
1355-
# lock file exists, but we need to check pid as well in case the process died
1356-
with open(lock_file) as f:
1357-
pid = f.read(200)
1358-
if pid and int(pid) != os.getpid():
1359-
if self.pid_exists(pid):
1360-
time.sleep(1)
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') 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)
13611367
continue
1362-
else:
1363-
info("Cache lock file exists, but process is dead. Cleaning up")
1364-
os.unlink(lock_file)
1368+
except (IOError, OSError):
1369+
continue
1370+
pass
1371+
13651372
can_lock = True
13661373
break
13671374

@@ -1378,12 +1385,12 @@ def cache_unlock(self, url):
13781385
cpath = self.url2cachedir(url)
13791386
if cpath:
13801387
lock_file = os.path.join(cpath, '.lock')
1381-
if os.path.isfile(os.path.join(cpath, '.lock')):
1388+
if os.path.isfile(lock_file):
13821389
try:
13831390
with open(lock_file) as f:
1384-
pid = f.read(200)
1391+
pid = f.read(8)
13851392
if int(pid) == os.getpid():
1386-
os.unlink(os.path.join(cpath, '.lock'))
1393+
os.remove(lock_file)
13871394
else:
13881395
error("Cache lock file exists with a different pid (\"%s\" vs \"%s\")" % (pid, os.getpid()))
13891396
except Exception:

0 commit comments

Comments
 (0)