| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import errno |
| 4 |
import os |
| 5 |
import socket |
| 6 |
import sys |
| 7 |
import time |
| 8 |
from errno import EBADF, ENOSYS, EXDEV |
| 9 |
from pathlib import Path |
| 10 |
from typing import TYPE_CHECKING, Final |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from tests.capability_marks import NEEDS_FILE_MODE |
| 15 |
|
| 16 |
if sys.version_info >= (3, 11): |
| 17 |
from builtins import ExceptionGroup # pragma: >=3.11 cover |
| 18 |
else: # pragma: <3.11 cover |
| 19 |
from exceptiongroup import ExceptionGroup |
| 20 |
|
| 21 |
from capabilities import CAPABILITIES |
| 22 |
|
| 23 |
import filelock._strict |
| 24 |
from filelock import ( |
| 25 |
AsyncStrictSoftFileLock, |
| 26 |
SoftFileLock, |
| 27 |
SoftFileLockProtocolError, |
| 28 |
StrictSoftFileClaim, |
| 29 |
StrictSoftFileClaimState, |
| 30 |
StrictSoftFileLock, |
| 31 |
Timeout, |
| 32 |
) |
| 33 |
from filelock._identity import process_start_token |
| 34 |
from filelock._strict import _PRIVATE_RECORD_MARKER, _probe_hard_link_unsupported_errnos |
| 35 |
|
| 36 |
if TYPE_CHECKING: |
| 37 |
from pytest_mock import MockerFixture |
| 38 |
|
| 39 |
_SENTINEL: Final[bytes] = b"1\nfilelock-strict-v1\x00\n0\n" |
| 40 |
|
| 41 |
|
| 42 |
pytestmark = pytest.mark.requires_hard_links |
| 43 |
|
| 44 |
|
| 45 |
def test_strict_soft_acquire_publishes_owner_claim(tmp_path: Path) -> None: |
| 46 |
lock_path = tmp_path / "resource.lock" |
| 47 |
lock = StrictSoftFileLock(lock_path) |
| 48 |
|
| 49 |
with lock: |
| 50 |
held, intent = lock.claims |
| 51 |
owner = { |
| 52 |
"token": held.token, |
| 53 |
"pid": os.getpid(), |
| 54 |
"hostname": socket.gethostname(), |
| 55 |
"start": process_start_token(os.getpid()), |
| 56 |
} |
| 57 |
assert (held, intent) == ( |
| 58 |
StrictSoftFileClaim(name=held.name, state="held", **owner), |
| 59 |
StrictSoftFileClaim(name=intent.name, state="intent", **owner), |
| 60 |
) |
| 61 |
assert (lock_path.read_bytes(), lock.claims, lock.is_locked) == (_SENTINEL, (), False) |
| 62 |
|
| 63 |
|
| 64 |
def test_strict_soft_is_reentrant(tmp_path: Path) -> None: |
| 65 |
lock = StrictSoftFileLock(tmp_path / "resource.lock") |
| 66 |
|
| 67 |
with lock: |
| 68 |
with lock: |
| 69 |
assert (lock.is_locked, lock.lock_counter, len(lock.claims)) == (True, 2, 2) |
| 70 |
assert (lock.is_locked, lock.lock_counter, len(lock.claims)) == (True, 1, 2) |
| 71 |
assert (lock.is_locked, lock.lock_counter, lock.claims) == (False, 0, ()) |
| 72 |
|
| 73 |
|
| 74 |
def test_strict_soft_contender_times_out(tmp_path: Path) -> None: |
| 75 |
lock_path = tmp_path / "resource.lock" |
| 76 |
holder = StrictSoftFileLock(lock_path) |
| 77 |
|
| 78 |
with holder, pytest.raises(Timeout): |
| 79 |
StrictSoftFileLock(lock_path, timeout=0).acquire() |
| 80 |
with StrictSoftFileLock(lock_path, timeout=0) as acquired: |
| 81 |
assert acquired.is_locked |
| 82 |
|
| 83 |
|
| 84 |
def test_strict_soft_waits_for_legacy_holder(tmp_path: Path) -> None: |
| 85 |
lock_path = tmp_path / "resource.lock" |
| 86 |
legacy = SoftFileLock(lock_path) |
| 87 |
strict = StrictSoftFileLock(lock_path, timeout=0) |
| 88 |
|
| 89 |
with legacy, pytest.raises(Timeout): |
| 90 |
strict.acquire() |
| 91 |
with strict: |
| 92 |
assert strict.is_locked |
| 93 |
|
| 94 |
|
| 95 |
def test_legacy_client_cannot_cross_permanent_sentinel(tmp_path: Path) -> None: |
| 96 |
lock_path = tmp_path / "resource.lock" |
| 97 |
with StrictSoftFileLock(lock_path): |
| 98 |
pass |
| 99 |
os.utime(lock_path, (0, 0)) |
| 100 |
|
| 101 |
with pytest.raises(Timeout): |
| 102 |
SoftFileLock(lock_path, timeout=0).acquire() |
| 103 |
assert lock_path.read_bytes() == _SENTINEL |
| 104 |
|
| 105 |
|
| 106 |
def test_legacy_client_never_reclaims_permanent_sentinel(tmp_path: Path, mocker: MockerFixture) -> None: |
| 107 |
lock_path = tmp_path / "resource.lock" |
| 108 |
lock_path.write_bytes(_SENTINEL) |
| 109 |
os.utime(lock_path, (0, 0)) |
| 110 |
mocker.patch("filelock._identity.socket.gethostname", return_value="filelock-strict-v1\x00") |
| 111 |
mocker.patch("filelock._identity.os.kill", side_effect=ProcessLookupError) |
| 112 |
|
| 113 |
with pytest.raises(Timeout): |
| 114 |
SoftFileLock(lock_path, timeout=0).acquire() |
| 115 |
assert lock_path.read_bytes() == _SENTINEL |
| 116 |
|
| 117 |
|
| 118 |
def test_strict_soft_rejects_mutable_descriptor_hook(tmp_path: Path) -> None: |
| 119 |
with pytest.raises(ValueError, match="on_acquired is not supported"): |
| 120 |
StrictSoftFileLock(tmp_path / "resource.lock", on_acquired=lambda _fd: None) |
| 121 |
|
| 122 |
|
| 123 |
def test_strict_soft_preserves_sentinel_when_requested(tmp_path: Path) -> None: |
| 124 |
lock_path = tmp_path / "resource.lock" |
| 125 |
with StrictSoftFileLock(lock_path, preserve_lock_file=True): |
| 126 |
pass |
| 127 |
assert lock_path.read_bytes() == _SENTINEL |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.parametrize( |
| 131 |
"lock_type", |
| 132 |
[ |
| 133 |
pytest.param(StrictSoftFileLock, id="sync"), |
| 134 |
pytest.param(AsyncStrictSoftFileLock, id="async"), |
| 135 |
], |
| 136 |
) |
| 137 |
def test_strict_soft_constructor_rejects_lifetime( |
| 138 |
tmp_path: Path, |
| 139 |
lock_type: type[StrictSoftFileLock | AsyncStrictSoftFileLock], |
| 140 |
) -> None: |
| 141 |
with pytest.warns(UserWarning, match=rf"lifetime is ignored for {lock_type.__name__}") as captured: |
| 142 |
lock = lock_type(tmp_path / "resource.lock", lifetime=1) |
| 143 |
|
| 144 |
assert (lock.lifetime, captured[0].filename) == (None, __file__) |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.parametrize( |
| 148 |
"lock_type", |
| 149 |
[ |
| 150 |
pytest.param(StrictSoftFileLock, id="sync"), |
| 151 |
pytest.param(AsyncStrictSoftFileLock, id="async"), |
| 152 |
], |
| 153 |
) |
| 154 |
def test_strict_soft_setter_rejects_lifetime( |
| 155 |
tmp_path: Path, |
| 156 |
lock_type: type[StrictSoftFileLock | AsyncStrictSoftFileLock], |
| 157 |
) -> None: |
| 158 |
lock = lock_type(tmp_path / "resource.lock") |
| 159 |
|
| 160 |
with pytest.warns(UserWarning, match=rf"lifetime is ignored for {lock_type.__name__}") as captured: |
| 161 |
lock.lifetime = 1 |
| 162 |
|
| 163 |
assert (lock.lifetime, captured[0].filename) == (None, __file__) |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.parametrize( |
| 167 |
"claim_name", |
| 168 |
[ |
| 169 |
pytest.param("", id="empty"), |
| 170 |
pytest.param(".private.tmp", id="private"), |
| 171 |
pytest.param("../claim", id="parent"), |
| 172 |
pytest.param("directory/claim", id="slash"), |
| 173 |
pytest.param(r"directory\claim", id="backslash"), |
| 174 |
pytest.param("claim\0name", id="null"), |
| 175 |
], |
| 176 |
) |
| 177 |
def test_strict_soft_force_break_rejects_non_basename(tmp_path: Path, claim_name: str) -> None: |
| 178 |
with pytest.raises(ValueError, match="one public claim basename"): |
| 179 |
StrictSoftFileLock(tmp_path / "resource.lock").force_break(claim_name) |
| 180 |
|
| 181 |
|
| 182 |
def test_strict_soft_force_break_names_guarantee_loss(tmp_path: Path) -> None: |
| 183 |
lock_path = tmp_path / "resource.lock" |
| 184 |
holder = StrictSoftFileLock(lock_path) |
| 185 |
holder.acquire() |
| 186 |
|
| 187 |
breaker = StrictSoftFileLock(lock_path) |
| 188 |
for claim in holder.claims: |
| 189 |
breaker.force_break(claim.name) |
| 190 |
with StrictSoftFileLock(lock_path, timeout=0) as contender: |
| 191 |
assert (holder.is_locked, contender.is_locked) == (True, True) |
| 192 |
holder.release() |
| 193 |
|
| 194 |
|
| 195 |
def test_strict_soft_force_break_requires_exact_case(tmp_path: Path) -> None: |
| 196 |
holder = StrictSoftFileLock(tmp_path / "resource.lock") |
| 197 |
holder.acquire() |
| 198 |
names = [claim.name for claim in holder.claims] |
| 199 |
|
| 200 |
with pytest.raises(FileNotFoundError): |
| 201 |
StrictSoftFileLock(holder.lock_file).force_break(names[0].upper()) |
| 202 |
assert [claim.name for claim in holder.claims] == names |
| 203 |
holder.release() |
| 204 |
|
| 205 |
|
| 206 |
def test_strict_soft_force_break_accepts_newer_version_claim(tmp_path: Path) -> None: |
| 207 |
lock_path = tmp_path / "resource.lock" |
| 208 |
claims = Path(f"{lock_path}.filelock") / "claims" |
| 209 |
claims.mkdir(parents=True) |
| 210 |
claim_name = "held-v2-opaque.claim" |
| 211 |
(claims / claim_name).write_text("filelock-strict-v2\n", encoding="ascii", newline="") |
| 212 |
lock = StrictSoftFileLock(lock_path, timeout=0) |
| 213 |
|
| 214 |
with pytest.raises(SoftFileLockProtocolError, match="unknown claim name or protocol version"): |
| 215 |
lock.acquire() |
| 216 |
lock.force_break(claim_name) |
| 217 |
with lock: |
| 218 |
assert lock.is_locked |
| 219 |
|
| 220 |
|
| 221 |
@pytest.mark.parametrize( |
| 222 |
("claim_name", "content", "reason"), |
| 223 |
[ |
| 224 |
pytest.param("held-v0-dead.claim", b"old\n", "unknown claim name or protocol version", id="old-version"), |
| 225 |
pytest.param("unrecognized", b"unknown\n", "unknown claim name or protocol version", id="unknown-name"), |
| 226 |
pytest.param( |
| 227 |
"held-v1-gggggggggggggggggggggggggggggggg.claim", |
| 228 |
b"unknown\n", |
| 229 |
"unknown claim name or protocol version", |
| 230 |
id="invalid-token", |
| 231 |
), |
| 232 |
pytest.param( |
| 233 |
"held-v1-00000000000000000000000000000000.claim", |
| 234 |
b"truncated\n", |
| 235 |
"malformed claim record", |
| 236 |
id="malformed-record", |
| 237 |
), |
| 238 |
], |
| 239 |
) |
| 240 |
def test_strict_soft_invalid_claim_fails_closed(tmp_path: Path, claim_name: str, content: bytes, reason: str) -> None: |
| 241 |
lock_path = tmp_path / "resource.lock" |
| 242 |
claims = Path(f"{lock_path}.filelock") / "claims" |
| 243 |
claims.mkdir(parents=True) |
| 244 |
(claims / claim_name).write_bytes(content) |
| 245 |
lock = StrictSoftFileLock(lock_path, timeout=0) |
| 246 |
|
| 247 |
with pytest.raises(SoftFileLockProtocolError, match=reason) as raised: |
| 248 |
lock.acquire() |
| 249 |
assert (raised.value.lock_file, raised.value.claim_name, raised.value.reason, lock.is_locked) == ( |
| 250 |
str(lock_path), |
| 251 |
claim_name, |
| 252 |
reason, |
| 253 |
False, |
| 254 |
) |
| 255 |
|
| 256 |
|
| 257 |
@pytest.mark.parametrize("state", [pytest.param("intent", id="intent"), pytest.param("held", id="held")]) |
| 258 |
def test_strict_soft_orphan_claim_blocks_without_reclamation(tmp_path: Path, state: StrictSoftFileClaimState) -> None: |
| 259 |
lock_path = tmp_path / "resource.lock" |
| 260 |
token = "0" * 32 |
| 261 |
claim_name = f"{state}-v1-{token}.claim" |
| 262 |
claims = Path(f"{lock_path}.filelock") / "claims" |
| 263 |
claims.mkdir(parents=True) |
| 264 |
(claims / claim_name).write_text( |
| 265 |
f"filelock-strict-v1\n{token}\n{os.getpid()}\n{socket.gethostname().encode().hex()}\n4242\n", |
| 266 |
encoding="ascii", |
| 267 |
newline="", |
| 268 |
) |
| 269 |
lock = StrictSoftFileLock(lock_path, timeout=0) |
| 270 |
|
| 271 |
with pytest.raises(Timeout): |
| 272 |
lock.acquire() |
| 273 |
assert lock.claims == ( |
| 274 |
StrictSoftFileClaim( |
| 275 |
name=claim_name, state=state, token=token, pid=os.getpid(), hostname=socket.gethostname(), start=4242 |
| 276 |
), |
| 277 |
) |
| 278 |
|
| 279 |
|
| 280 |
def test_strict_soft_rejects_non_directory_coordination_path(tmp_path: Path) -> None: |
| 281 |
lock_path = tmp_path / "resource.lock" |
| 282 |
Path(f"{lock_path}.filelock").write_text("not a directory", encoding="utf-8") |
| 283 |
|
| 284 |
with pytest.raises(SoftFileLockProtocolError, match="is not a real directory"): |
| 285 |
StrictSoftFileLock(lock_path).acquire() |
| 286 |
|
| 287 |
|
| 288 |
@NEEDS_FILE_MODE # pragma: needs file-mode |
| 289 |
def test_strict_soft_unreadable_claim_fails_closed(tmp_path: Path) -> None: |
| 290 |
lock_path = tmp_path / "resource.lock" |
| 291 |
lock = StrictSoftFileLock(lock_path) |
| 292 |
lock.acquire() |
| 293 |
claim = Path(f"{lock_path}.filelock") / "claims" / lock.claims[0].name |
| 294 |
claim.chmod(0) |
| 295 |
try: |
| 296 |
with pytest.raises(SoftFileLockProtocolError, match="cannot read claim"): |
| 297 |
_ = StrictSoftFileLock(lock_path).claims |
| 298 |
finally: |
| 299 |
claim.chmod(0o600) |
| 300 |
lock.release() |
| 301 |
|
| 302 |
|
| 303 |
@pytest.mark.skipif(not CAPABILITIES["symlink"], reason="stages a claim as a symlink") |
| 304 |
def test_strict_soft_symlink_claim_fails_closed(tmp_path: Path) -> None: # pragma: needs symlink |
| 305 |
lock_path = tmp_path / "resource.lock" |
| 306 |
claims = Path(f"{lock_path}.filelock") / "claims" |
| 307 |
claims.mkdir(parents=True) |
| 308 |
target = tmp_path / "claim-target" |
| 309 |
target.write_bytes(b"filelock-strict-v1\n" + b"0" * 32 + b"\n1\n686f7374\n") |
| 310 |
claim = claims / f"held-v1-{'0' * 32}.claim" |
| 311 |
claim.symlink_to(target) |
| 312 |
|
| 313 |
with pytest.raises(SoftFileLockProtocolError, match="cannot read claim"): |
| 314 |
StrictSoftFileLock(lock_path).acquire() |
| 315 |
|
| 316 |
|
| 317 |
@pytest.mark.parametrize( |
| 318 |
"error", |
| 319 |
[ |
| 320 |
pytest.param(OSError(EXDEV, "cross-device link"), id="filesystem"), |
| 321 |
pytest.param(OSError(ENOSYS, "link unavailable"), id="kernel"), |
| 322 |
pytest.param(NotImplementedError("hard links unavailable"), id="runtime"), |
| 323 |
], |
| 324 |
) |
| 325 |
def test_strict_soft_hard_link_failure_names_filesystem_contract( |
| 326 |
tmp_path: Path, mocker: MockerFixture, error: OSError | NotImplementedError |
| 327 |
) -> None: |
| 328 |
mocker.patch("filelock._strict.os.link", side_effect=error) |
| 329 |
|
| 330 |
with pytest.raises(SoftFileLockProtocolError, match="atomic no-replace hard-link"): |
| 331 |
StrictSoftFileLock(tmp_path / "resource.lock").acquire() |
| 332 |
|
| 333 |
|
| 334 |
@pytest.mark.parametrize( |
| 335 |
("defined", "expected"), |
| 336 |
[ |
| 337 |
pytest.param({"ENOTSUP": 4242, "EOPNOTSUPP": 4343}, {ENOSYS, EXDEV, 4242}, id="enotsup"), |
| 338 |
pytest.param({"EOPNOTSUPP": 4343}, {ENOSYS, EXDEV, 4343}, id="eopnotsupp"), |
| 339 |
pytest.param({}, {ENOSYS, EXDEV}, id="neither"), |
| 340 |
], |
| 341 |
) |
| 342 |
def test_strict_soft_hard_link_unsupported_errnos( |
| 343 |
monkeypatch: pytest.MonkeyPatch, defined: dict[str, int], expected: set[int] |
| 344 |
) -> None: |
| 345 |
for name in ("ENOTSUP", "EOPNOTSUPP"): |
| 346 |
monkeypatch.delattr(errno, name, raising=False) |
| 347 |
for name, code in defined.items(): |
| 348 |
monkeypatch.setattr(errno, name, code, raising=False) |
| 349 |
|
| 350 |
assert _probe_hard_link_unsupported_errnos() == expected |
| 351 |
|
| 352 |
|
| 353 |
def test_strict_soft_write_failure_leaves_no_public_claim(tmp_path: Path, mocker: MockerFixture) -> None: |
| 354 |
lock_path = tmp_path / "resource.lock" |
| 355 |
with StrictSoftFileLock(lock_path): |
| 356 |
pass |
| 357 |
mocker.patch("filelock._util.os.write", side_effect=OSError("write failed")) |
| 358 |
lock = StrictSoftFileLock(lock_path) |
| 359 |
|
| 360 |
with pytest.raises(OSError, match="write failed"): |
| 361 |
lock.acquire() |
| 362 |
assert (lock.claims, tuple((Path(f"{lock_path}.filelock") / "claims").iterdir()), lock.is_locked) == ( |
| 363 |
(), |
| 364 |
(), |
| 365 |
False, |
| 366 |
) |
| 367 |
|
| 368 |
|
| 369 |
def test_strict_soft_sentinel_inspection_failure_closes_descriptor(tmp_path: Path, mocker: MockerFixture) -> None: |
| 370 |
lock_path = tmp_path / "resource.lock" |
| 371 |
with StrictSoftFileLock(lock_path): |
| 372 |
pass |
| 373 |
real_fstat = os.fstat |
| 374 |
inspected_fd: int | None = None |
| 375 |
|
| 376 |
def fail_sentinel_inspection(fd: int) -> os.stat_result: |
| 377 |
nonlocal inspected_fd |
| 378 |
if os.lseek(fd, 0, os.SEEK_CUR) == len(_SENTINEL): |
| 379 |
inspected_fd = fd |
| 380 |
msg = "sentinel inspection failed" |
| 381 |
raise OSError(msg) |
| 382 |
return real_fstat(fd) |
| 383 |
|
| 384 |
mocker.patch("filelock._strict.os.fstat", side_effect=fail_sentinel_inspection) |
| 385 |
lock = StrictSoftFileLock(lock_path) |
| 386 |
|
| 387 |
with pytest.raises(OSError, match="sentinel inspection failed"): |
| 388 |
lock.acquire() |
| 389 |
assert inspected_fd is not None |
| 390 |
# A closed descriptor reports EBADF on every platform, but the message differs (Windows says "handle is invalid"). |
| 391 |
with pytest.raises(OSError, match=r"Bad file descriptor|handle is invalid") as closed: |
| 392 |
real_fstat(inspected_fd) |
| 393 |
assert closed.value.errno == EBADF |
| 394 |
assert (lock.is_locked, lock.lock_counter, lock.claims) == (False, 0, ()) |
| 395 |
|
| 396 |
|
| 397 |
def test_strict_soft_publication_and_close_failures_preserve_both_errors(tmp_path: Path, mocker: MockerFixture) -> None: |
| 398 |
lock_path = tmp_path / "resource.lock" |
| 399 |
real_close = os.close |
| 400 |
real_open_relative = filelock._strict._open_relative |
| 401 |
private_fd: int | None = None |
| 402 |
|
| 403 |
def capture_private_record(directory_ref: tuple[str, int | None], name: str, flags: int, mode: int) -> int: |
| 404 |
nonlocal private_fd |
| 405 |
private_fd = real_open_relative(directory_ref, name, flags, mode) |
| 406 |
return private_fd |
| 407 |
|
| 408 |
# filelock._strict.os is the os module, so this patches os.close process-wide. Key the fault to the descriptor |
| 409 |
# under test rather than to call order, which a background thread's close would consume instead. |
| 410 |
def fail_private_close(fd: int) -> None: |
| 411 |
real_close(fd) |
| 412 |
# Only a dir_fd release closes a second descriptor, so elsewhere this one is the only one. |
| 413 |
if fd == private_fd: # pragma: no branch |
| 414 |
msg = "private close failed" |
| 415 |
raise OSError(msg) |
| 416 |
|
| 417 |
mocker.patch("filelock._util.os.write", side_effect=OSError("record write failed")) |
| 418 |
mocker.patch("filelock._strict._open_relative", side_effect=capture_private_record) |
| 419 |
mocker.patch("filelock._strict.os.close", side_effect=fail_private_close) |
| 420 |
lock = StrictSoftFileLock(lock_path) |
| 421 |
|
| 422 |
with pytest.raises(ExceptionGroup) as raised: |
| 423 |
lock.acquire() |
| 424 |
assert [str(error) for error in raised.value.exceptions] == ["record write failed", "private close failed"] |
| 425 |
assert (lock.is_locked, lock.lock_counter, tuple(tmp_path.glob("**/*.tmp"))) == (False, 0, ()) |
| 426 |
|
| 427 |
|
| 428 |
@pytest.mark.asyncio |
| 429 |
async def test_async_strict_soft_matches_claim_protocol(tmp_path: Path) -> None: |
| 430 |
lock_path = tmp_path / "resource.lock" |
| 431 |
holder = AsyncStrictSoftFileLock(lock_path) |
| 432 |
|
| 433 |
async with holder: |
| 434 |
assert holder.claims[0].state == "held" |
| 435 |
with pytest.raises(Timeout): |
| 436 |
await AsyncStrictSoftFileLock(lock_path, timeout=0).acquire() |
| 437 |
async with AsyncStrictSoftFileLock(lock_path, timeout=0) as contender: |
| 438 |
assert contender.is_locked |
| 439 |
|
| 440 |
|
| 441 |
# The reacquire only needs proving where the holder's own open handle blocks the unlink; elsewhere release |
| 442 |
# removes the name outright and there is no sharing rule to exercise. |
| 443 |
@pytest.mark.skipif( |
| 444 |
CAPABILITIES["unlink-open-file"], reason="exercises pathname sharing where an open file resists unlink" |
| 445 |
) |
| 446 |
def test_strict_soft_release_allows_reacquire(tmp_path: Path) -> None: # pragma: lacks unlink-open-file |
| 447 |
lock_path = tmp_path / "resource.lock" |
| 448 |
|
| 449 |
with StrictSoftFileLock(lock_path) as first: |
| 450 |
assert first.claims[0].state == "held" |
| 451 |
with StrictSoftFileLock(lock_path, timeout=0) as second: |
| 452 |
assert second.claims[0].state == "held" |
| 453 |
|
| 454 |
|
| 455 |
def test_strict_soft_reclaims_an_aged_sentinel_private_record(tmp_path: Path) -> None: |
| 456 |
# A crash between creating a private record and linking it leaves it behind; the next acquire reclaims it once |
| 457 |
# it ages out. The dir_fd reaper cases only proved this where dir_fd exists. |
| 458 |
lock_path = tmp_path / "resource.lock" |
| 459 |
stale = tmp_path / f".{lock_path.name}{_PRIVATE_RECORD_MARKER}{'0' * 32}.tmp" |
| 460 |
stale.write_bytes(b"") |
| 461 |
aged = time.time() - 3600 |
| 462 |
os.utime(stale, (aged, aged)) |
| 463 |
|
| 464 |
with StrictSoftFileLock(lock_path): |
| 465 |
assert not stale.exists() |