| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import gc |
| 4 |
import os |
| 5 |
import subprocess # ruff:ignore[suspicious-subprocess-import] # interpreter exit exercises the atexit registry |
| 6 |
import sys |
| 7 |
import weakref |
| 8 |
from dataclasses import dataclass |
| 9 |
from typing import TYPE_CHECKING, NoReturn |
| 10 |
|
| 11 |
from filelock import BaseFileLock |
| 12 |
from tests.capability_marks import NEEDS_CLASS_COLLECTION, NEEDS_FORK |
| 13 |
from tests.fork_helpers import exit_child, fork_process |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from pathlib import Path |
| 17 |
|
| 18 |
|
| 19 |
@NEEDS_FORK |
| 20 |
def test_equal_unhashable_locks_reset_independently(tmp_path: Path) -> None: # pragma: win32 no cover |
| 21 |
@dataclass(eq=True, init=False) # pragma: win32 no cover |
| 22 |
class EqualLock(BaseFileLock): # pragma: win32 no cover |
| 23 |
def _acquire(self) -> None: # pragma: win32 no cover |
| 24 |
self._context.lock_file_fd = os.open(self.lock_file, os.O_CREAT | os.O_RDWR, 0o600) |
| 25 |
|
| 26 |
def _release(self) -> None: # pragma: win32 no cover |
| 27 |
os.close(self._context.lock_file_fd if self._context.lock_file_fd is not None else -1) |
| 28 |
self._context.lock_file_fd = None |
| 29 |
|
| 30 |
first = EqualLock(str(tmp_path / "first.lock"), is_singleton=False) |
| 31 |
second = EqualLock(str(tmp_path / "second.lock"), is_singleton=False) |
| 32 |
first.acquire() |
| 33 |
second.acquire() |
| 34 |
|
| 35 |
def reset_child() -> NoReturn: |
| 36 |
state = first.is_locked, first.lock_counter, second.is_locked, second.lock_counter |
| 37 |
exit_child(0 if state == (False, 0, False, 0) else 1) |
| 38 |
|
| 39 |
child_pid = fork_process(reset_child) |
| 40 |
_, status = os.waitpid(child_pid, 0) |
| 41 |
first.release() |
| 42 |
second.release() |
| 43 |
|
| 44 |
assert os.waitstatus_to_exitcode(status) == 0 |
| 45 |
|
| 46 |
|
| 47 |
def test_equal_unhashable_soft_read_write_locks_survive_atexit_registry(tmp_path: Path) -> None: |
| 48 |
script = """ |
| 49 |
from __future__ import annotations |
| 50 |
|
| 51 |
import sys |
| 52 |
from dataclasses import dataclass |
| 53 |
|
| 54 |
from filelock import SoftReadWriteLock |
| 55 |
|
| 56 |
@dataclass(eq=True, init=False) |
| 57 |
class EqualLock(SoftReadWriteLock): |
| 58 |
pass |
| 59 |
|
| 60 |
locks = [EqualLock(path, is_singleton=False, heartbeat_interval=1, stale_threshold=30) for path in sys.argv[1:]] |
| 61 |
for lock in locks: |
| 62 |
lock.acquire_write() |
| 63 |
""" |
| 64 |
paths = [tmp_path / "first.lock", tmp_path / "second.lock"] |
| 65 |
result = subprocess.run( |
| 66 |
[sys.executable, "-c", script, *(str(path) for path in paths)], |
| 67 |
check=False, |
| 68 |
capture_output=True, |
| 69 |
text=True, |
| 70 |
timeout=10, |
| 71 |
) |
| 72 |
|
| 73 |
assert (result.returncode, [path.with_name(f"{path.name}.write").exists() for path in paths]) == (0, [False, False]) |
| 74 |
|
| 75 |
|
| 76 |
@NEEDS_CLASS_COLLECTION |
| 77 |
def test_dynamic_lock_class_can_be_collected() -> None: |
| 78 |
class EphemeralLock(BaseFileLock): |
| 79 |
_acquire = _release = BaseFileLock._acquire |
| 80 |
|
| 81 |
class_ref = weakref.ref(EphemeralLock) |
| 82 |
del EphemeralLock |
| 83 |
gc.collect() |
| 84 |
|
| 85 |
assert class_ref() is None |