Skip to content

Commit 7007387

Browse files
Yshurogaborbernat
andauthored
🐛 fix(soft): recover markers after ENOSPC (#1101)
Co-authored-by: BernĂĄt GĂĄbor <gaborjbernat@gmail.com> Signed-off-by: Yshuro <yshuro1080@gmail.com>
1 parent f09ab4d commit 7007387

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
On POSIX, if a full filesystem prevents :class:`~filelock.SoftFileLock` from removing a failed marker, cleanup tries
2+
to leave a one-byte record. On filesystems with atomic exchange, a later caller can reclaim it once space is available.

‎docs/how-to.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@ them unchanged for two seconds. An empty marker, which is all a ``SoftFileLock``
854854
this way; remove a leftover one with ``break_lock()``.
855855
That recovery path is not fail closed.
856856

857+
If a POSIX filesystem fills during acquisition, cleanup tries to leave a one-byte marker when it cannot remove the
858+
failed record. Free space so the next caller can reclaim the marker on filesystems that support atomic exchange.
859+
857860
A contender breaks a stale marker without the lock path ever standing empty. Where it cannot (NFS, SMB and platforms
858861
without an atomic exchange, other than Windows) it leaves the marker in place; see :ref:`concepts:How does stale lock
859862
detection work across platforms?` for the details.

‎src/filelock/_util.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
import time
1212
from contextlib import contextmanager, suppress
13-
from errno import EACCES, EINVAL, EIO, EISDIR, ENOSYS, EXDEV
13+
from errno import EACCES, EINVAL, EIO, EISDIR, ENOSPC, ENOSYS, EXDEV
1414
from functools import cache
1515
from pathlib import Path
1616
from typing import TYPE_CHECKING, Final, cast
@@ -119,9 +119,15 @@ def remove_own_file(lock_file: str, fd: int) -> None: # pragma: win32 no cover
119119
# :raises OSError: if the descriptor cannot be read or the unlink fails.
120120
own: Final[os.stat_result] = os.fstat(fd)
121121
with suppress(FileNotFoundError):
122-
break_lock_file(lock_file, own.st_mtime, own.st_ino)
123-
if os.path.samestat(os.lstat(lock_file), own):
124-
Path(lock_file).unlink()
122+
try:
123+
break_lock_file(lock_file, own.st_mtime, own.st_ino)
124+
if os.path.samestat(os.lstat(lock_file), own):
125+
Path(lock_file).unlink()
126+
except OSError as exc:
127+
if exc.errno == ENOSPC:
128+
# A sparse byte makes our failed marker reclaimable without allocating a data block.
129+
os.ftruncate(fd, 1)
130+
raise
125131

126132

127133
def break_lock_file(lock_file: str, mtime_before: float, ino_before: int) -> None:

‎tests/test_soft_stale.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,49 @@ def test_write_failure_rolls_back_acquire(lock_path: Path, mocker: MockerFixture
15301530
assert not lock_path.exists()
15311531

15321532

1533+
@pytest.mark.skipif(sys.platform == "win32", reason="POSIX rolls back before closing the descriptor")
1534+
@NEEDS_STALE_RECLAIM
1535+
@pytest.mark.usefixtures("unchanged_past_grace")
1536+
@pytest.mark.parametrize(
1537+
("cleanup_errno", "fail_unlink", "record"),
1538+
[
1539+
pytest.param(ENOSPC, False, b"\0", id="create-full"),
1540+
pytest.param(ENOSPC, True, b"\0", id="unlink-full"),
1541+
pytest.param(EIO, False, b"", id="io-error"),
1542+
],
1543+
)
1544+
def test_rollback_on_full_filesystem(
1545+
lock_path: Path, mocker: MockerFixture, cleanup_errno: int, record: bytes, *, fail_unlink: bool
1546+
) -> None: # pragma: win32 no cover
1547+
open_file = os.open
1548+
1549+
def open_with_full_directory(path: str, flags: int, mode: int = 0o777) -> int:
1550+
if not fail_unlink and ".break." in path:
1551+
raise OSError(cleanup_errno, os.strerror(cleanup_errno))
1552+
return open_file(path, flags, mode)
1553+
1554+
unlink = Path.unlink
1555+
1556+
def unlink_on_full_filesystem(path: Path, *, missing_ok: bool = False) -> None:
1557+
if fail_unlink and path == lock_path:
1558+
raise OSError(ENOSPC, os.strerror(ENOSPC))
1559+
unlink(path, missing_ok=missing_ok)
1560+
1561+
unlinks = mocker.patch.object(Path, "unlink", side_effect=unlink_on_full_filesystem, autospec=True)
1562+
opens = mocker.patch("filelock._util.os.open", side_effect=open_with_full_directory, autospec=True)
1563+
writes = mocker.patch("filelock._util.os.write", side_effect=OSError(ENOSPC, "record write failed"), autospec=True)
1564+
with pytest.raises(OSError, match="record write failed"):
1565+
SoftFileLock(lock_path).acquire(timeout=0)
1566+
mocker.stop(opens)
1567+
mocker.stop(writes)
1568+
mocker.stop(unlinks)
1569+
assert lock_path.read_bytes() == record
1570+
lock = SoftFileLock(lock_path)
1571+
acquire_after_reclaim(lock)
1572+
assert lock.is_locked == (cleanup_errno == ENOSPC)
1573+
lock.release()
1574+
1575+
15331576
def _hold_reports_foreign_identity(mocker: MockerFixture) -> None:
15341577
# Make the held descriptor report a different inode than the file on disk, as if a peer replaced the marker at the
15351578
# path. fstat runs only on our own lock fd, so nothing else in acquire or release is disturbed.

0 commit comments

Comments
 (0)