From 25bc35b2e041f8acc061d3b8ae50df947c5a8137 Mon Sep 17 00:00:00 2001 From: Yshuro Date: Fri, 20 Nov 2026 17:18:23 +0100 Subject: [PATCH 1/2] fix: ensure remove_own_file unlinks marker even when break_lock_file fails Signed-off-by: Yshuro --- src/filelock/_util.py | 3 ++- tests/test_util.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/filelock/_util.py b/src/filelock/_util.py index a5c76c4d8..a6fc50dfe 100644 --- a/src/filelock/_util.py +++ b/src/filelock/_util.py @@ -113,7 +113,8 @@ def remove_own_file(lock_file: str, fd: int) -> None: # pragma: win32 no cover # :raises OSError: if the descriptor cannot be read or the unlink fails. own: Final[os.stat_result] = os.fstat(fd) with suppress(FileNotFoundError): - break_lock_file(lock_file, own.st_mtime, own.st_ino) + with suppress(OSError): + break_lock_file(lock_file, own.st_mtime, own.st_ino) if os.path.samestat(os.lstat(lock_file), own): Path(lock_file).unlink() diff --git a/tests/test_util.py b/tests/test_util.py index 464ed3a80..c5b39f7e9 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -6,7 +6,7 @@ import sys import time from contextlib import suppress -from errno import EACCES, EINVAL, EIO, ELOOP, ENOENT, EPERM, ESRCH +from errno import EACCES, EINVAL, EIO, ELOOP, ENOENT, ENOSPC, EPERM, ESRCH from pathlib import Path from typing import TYPE_CHECKING, Final @@ -449,6 +449,25 @@ def record_the_placeholder(source: str, target: str) -> bool: assert placeholders == [f"filelock/break\n{os.getpid()}\n".encode()] +@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names") +@NEEDS_STALE_RECLAIM +def test_remove_own_file_unlinks_when_break_fails_with_enospc( + tmp_path: Path, mocker: MockerFixture +) -> None: # pragma: win32 no cover + lock_file = tmp_path / "test.lock" + fd = os.open(str(lock_file), os.O_CREAT | os.O_RDWR, 0o600) + try: + mocker.patch( + "filelock._util.break_lock_file", + side_effect=OSError(ENOSPC, "No space left on device"), + autospec=True, + ) + filelock._util.remove_own_file(str(lock_file), fd) + assert not lock_file.exists() + finally: + os.close(fd) + + @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names") @STALE_BREAKERS @NEEDS_STALE_RECLAIM From 114f6d9a06a802257c7114d332ca5077210696db Mon Sep 17 00:00:00 2001 From: Yshuro Date: Fri, 20 Nov 2026 21:01:01 +0100 Subject: [PATCH 2/2] changelog: add newsfragment for issue 1029 Signed-off-by: Yshuro --- docs/changelog/1029.bugfix.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 docs/changelog/1029.bugfix.rst diff --git a/docs/changelog/1029.bugfix.rst b/docs/changelog/1029.bugfix.rst new file mode 100644 index 000000000..5015466f4 --- /dev/null +++ b/docs/changelog/1029.bugfix.rst @@ -0,0 +1,4 @@ +Ensure ``remove_own_file`` unlinks the lock marker even when ``break_lock_file`` +fails due to ``ENOSPC`` or another :class:`OSError`. This prevents an empty +:class:`~filelock.SoftFileLock` marker from lingering on disk and blocking +subsequent acquires when filesystem space is exhausted.