Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions tools/filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down