Skip to content

Commit c855e4c

Browse files
committed
Enable removal of UNIX lock files
This avoids leaving orphan locks files around after we are done when them. See emscripten-core/emscripten#24035 Fixes: #31
1 parent a686b85 commit c855e4c

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/filelock/_unix.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,21 @@ def _acquire(self) -> None:
5252
msg = "FileSystem does not appear to support flock; use SoftFileLock instead"
5353
raise NotImplementedError(msg) from exception
5454
else:
55-
self._context.lock_file_fd = fd
55+
st = os.fstat(fd)
56+
if st.st_nlink == 0:
57+
# We raced with another process that deleted the lock file
58+
# before we called fcntl.flock. This means that lock is not
59+
# valid (since another process will just lock a different
60+
# file) and we need to try again.
61+
# See https://stackoverflow.com/a/51070775
62+
os.close(fd)
63+
else:
64+
self._context.lock_file_fd = fd
5665

5766
def _release(self) -> None:
58-
# Do not remove the lockfile:
59-
# https://github.com/tox-dev/py-filelock/issues/31
60-
# https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
6167
fd = cast("int", self._context.lock_file_fd)
6268
self._context.lock_file_fd = None
69+
os.unlink(self.lock_file)
6370
fcntl.flock(fd, fcntl.LOCK_UN)
6471
os.close(fd)
6572

0 commit comments

Comments
 (0)