Skip to content

Commit dd68169

Browse files
committed
Cleanup lock files in UNIX
See tox-dev/filelock#31 And https://stackoverflow.com/a/51070775 Without this change the `cache/symbol_lists` directory ends up full of many lock files that might never get cleaned up (At least since #24029 landed).
1 parent e014359 commit dd68169

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

tools/filelock.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,22 @@ def _acquire(self):
395395
except (IOError, OSError):
396396
os.close(fd)
397397
else:
398-
self._lock_file_fd = fd
398+
st = os.fstat(fd);
399+
if st.st_nlink == 0:
400+
# We raced with another process that deleted the lock file before
401+
# we called fcntl.flock. This means that lock is not valid (since
402+
# another process will just lock a different file) and we need to
403+
# try again.
404+
# See https://stackoverflow.com/a/51070775
405+
os.close(fd)
406+
else:
407+
self._lock_file_fd = fd
399408
return None
400409

401410
def _release(self):
402-
# Do not remove the lockfile:
403-
#
404-
# https://github.com/benediktschmitt/py-filelock/issues/31
405-
# https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
406411
fd = self._lock_file_fd
407412
self._lock_file_fd = None
413+
os.unlink(self._lock_file)
408414
fcntl.flock(fd, fcntl.LOCK_UN)
409415
os.close(fd)
410416
return None

0 commit comments

Comments
 (0)