Skip to content

Commit f1337aa

Browse files
committed
Handle case where pid file is in the middle of write
1 parent fb7fa82 commit f1337aa

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

mbed/mbed.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,16 +1351,22 @@ def cache_lock(self, url):
13511351

13521352
lock_dir = os.path.join(cpath, '.lock')
13531353
lock_file = os.path.join(lock_dir, 'pid')
1354+
timeout = 300
13541355

1355-
for i in range(300):
1356+
for i in range(timeout):
13561357
if i:
13571358
time.sleep(1)
13581359

13591360
if os.path.exists(lock_dir):
13601361
if os.path.isfile(lock_file):
13611362
with open(lock_file, 'r', 0) as f:
1362-
pid = int(f.read(8))
1363-
if pid != os.getpid() and self.pid_exists(pid):
1363+
pid = f.read(8)
1364+
if not pid:
1365+
if int(os.path.getmtime(lock_file)) + timeout < int(time.time()):
1366+
info("Cache lock file exists, but is empty. Cleaning up")
1367+
os.remove(lock_file)
1368+
os.rmdir(lock_dir)
1369+
elif int(pid) != os.getpid() and self.pid_exists(pid):
13641370
info("Cache lock file exists and process %s is alive." % pid)
13651371
else:
13661372
info("Cache lock file exists, but %s is dead. Cleaning up" % pid)

0 commit comments

Comments
 (0)