Skip to content
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
2 changes: 2 additions & 0 deletions docs/changelog/724.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``SoftReadWriteLock`` now rejects non-finite heartbeat, stale, and polling intervals at construction instead of
allowing them to disrupt heartbeat or polling behavior later.
16 changes: 10 additions & 6 deletions src/filelock/_soft_rw/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import uuid
from contextlib import closing, contextmanager, suppress
from dataclasses import dataclass
from math import isfinite
from pathlib import Path
from typing import TYPE_CHECKING, Final, Literal
from weakref import WeakValueDictionary
Expand Down Expand Up @@ -173,16 +174,19 @@ def __init__( # ruff:ignore[too-many-arguments] # public constructor: one para
poll_interval: float = 0.25,
) -> None:
self._creator_pid = os.getpid()
if heartbeat_interval <= 0:
msg = f"heartbeat_interval must be positive, got {heartbeat_interval}"
if not isfinite(heartbeat_interval) or heartbeat_interval <= 0:
msg = f"heartbeat_interval must be positive and finite, got {heartbeat_interval}"
raise ValueError(msg)
if stale_threshold is None:
stale_threshold = heartbeat_interval * 3
if stale_threshold <= heartbeat_interval:
msg = f"stale_threshold must exceed heartbeat_interval ({stale_threshold} <= {heartbeat_interval})"
if not isfinite(stale_threshold) or stale_threshold <= heartbeat_interval:
msg = (
"stale_threshold must exceed heartbeat_interval and be finite "
f"({stale_threshold} <= {heartbeat_interval})"
)
raise ValueError(msg)
if poll_interval <= 0:
msg = f"poll_interval must be positive, got {poll_interval}"
if not isfinite(poll_interval) or poll_interval <= 0:
msg = f"poll_interval must be positive and finite, got {poll_interval}"
raise ValueError(msg)

self.lock_file: str = os.fspath(lock_file)
Expand Down
18 changes: 18 additions & 0 deletions tests/soft_rw/test_soft_rw_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ def test_rejects_non_positive_poll_interval(lock_file: str) -> None:
SoftReadWriteLock(lock_file, poll_interval=0, is_singleton=False)


@pytest.mark.parametrize("value", [float("nan"), float("inf")])
def test_rejects_non_finite_heartbeat_interval(lock_file: str, value: float) -> None:
with pytest.raises(ValueError, match=r"heartbeat_interval must .*finite"):
SoftReadWriteLock(lock_file, heartbeat_interval=value, is_singleton=False)


@pytest.mark.parametrize("value", [float("nan"), float("inf")])
def test_rejects_non_finite_stale_threshold(lock_file: str, value: float) -> None:
with pytest.raises(ValueError, match=r"stale_threshold must .*finite"):
SoftReadWriteLock(lock_file, stale_threshold=value, is_singleton=False)


@pytest.mark.parametrize("value", [float("nan"), float("inf")])
def test_rejects_non_finite_poll_interval(lock_file: str, value: float) -> None:
with pytest.raises(ValueError, match=r"poll_interval must .*finite"):
SoftReadWriteLock(lock_file, poll_interval=value, is_singleton=False)


def test_public_attributes(lock_file: str) -> None:
lock = SoftReadWriteLock(
lock_file,
Expand Down