@@ -1351,17 +1351,24 @@ def cache_lock(self, url):
1351
1351
can_lock = False
1352
1352
# this loop is handling a lock file from another process if exists
1353
1353
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 )
1361
1367
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
+
1365
1372
can_lock = True
1366
1373
break
1367
1374
@@ -1378,12 +1385,12 @@ def cache_unlock(self, url):
1378
1385
cpath = self .url2cachedir (url )
1379
1386
if cpath :
1380
1387
lock_file = os .path .join (cpath , '.lock' )
1381
- if os .path .isfile (os . path . join ( cpath , '.lock' ) ):
1388
+ if os .path .isfile (lock_file ):
1382
1389
try :
1383
1390
with open (lock_file ) as f :
1384
- pid = f .read (200 )
1391
+ pid = f .read (8 )
1385
1392
if int (pid ) == os .getpid ():
1386
- os .unlink ( os . path . join ( cpath , '.lock' ) )
1393
+ os .remove ( lock_file )
1387
1394
else :
1388
1395
error ("Cache lock file exists with a different pid (\" %s\" vs \" %s\" )" % (pid , os .getpid ()))
1389
1396
except Exception :
0 commit comments