Skip to content

Enable removal of UNIX lock files #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions src/filelock/_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class UnixFileLock(BaseFileLock):

def _acquire(self) -> None:
ensure_directory_exists(self.lock_file)
open_flags = os.O_RDWR | os.O_TRUNC
if not Path(self.lock_file).exists():
open_flags |= os.O_CREAT
open_flags = os.O_RDWR | os.O_TRUNC | os.O_CREAT
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry a little that I had to remove this workaround which was added in: #317.

I can't seem to find a way to keep both of these fixes in place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm hesitant to approve it without it though 🤔

fd = os.open(self.lock_file, open_flags, self._context.mode)
with suppress(PermissionError): # This locked is not owned by this UID
os.fchmod(fd, self._context.mode)
Expand All @@ -52,14 +50,21 @@ def _acquire(self) -> None:
msg = "FileSystem does not appear to support flock; use SoftFileLock instead"
raise NotImplementedError(msg) from exception
else:
self._context.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._context.lock_file_fd = fd

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

Expand Down
9 changes: 9 additions & 0 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,12 @@ def __init__(self, file_path: str) -> None:
lock_path = tmp_path / "a"
lock = FilePathLock(str(lock_path))
assert lock.lock_file == str(lock_path) + ".lock"


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_lock_is_removed(tmp_path: Path, lock_type: type[BaseFileLock]) -> None:
lock_path = tmp_path / "test.lock"
lock = lock_type(lock_path)
with lock:
assert Path.exists(lock_path)
assert not Path.exists(lock_path)
Loading