From dd6816936d3734dc4d7de8d3aac269d7f81c85ab Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 2 Apr 2025 09:54:05 -0700 Subject: [PATCH] Cleanup lock files in UNIX See https://github.com/benediktschmitt/py-filelock/issues/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). --- tools/filelock.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/filelock.py b/tools/filelock.py index e13bb28ed3293..1fc21f8ecbee3 100644 --- a/tools/filelock.py +++ b/tools/filelock.py @@ -395,16 +395,22 @@ def _acquire(self): except (IOError, OSError): os.close(fd) else: - self._lock_file_fd = fd + st = os.fstat(fd); + if st.st_nlink == 0: + # We raced with another process that deleted the lock file before + # we called fcntl.flock. This means that lock is not valid (since + # another process will just lock a different file) and we need to + # try again. + # See https://stackoverflow.com/a/51070775 + os.close(fd) + else: + self._lock_file_fd = fd return None def _release(self): - # Do not remove the lockfile: - # - # https://github.com/benediktschmitt/py-filelock/issues/31 - # https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition fd = self._lock_file_fd self._lock_file_fd = None + os.unlink(self._lock_file) fcntl.flock(fd, fcntl.LOCK_UN) os.close(fd) return None