Debian Codegraph @

tests/test_fork_registries.py

raw
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 import pytest
12
13 from filelock import BaseFileLock
14 from filelock._soft_rw._protocol import GenerationLog
15 from filelock._soft_rw._storage import OsFiles
16 from tests.capability_marks import NEEDS_CLASS_COLLECTION, NEEDS_FORK
17 from tests.fork_helpers import exit_child, fork_process
18
19 if TYPE_CHECKING:
20 from pathlib import Path
21
22
23 @NEEDS_FORK
24 def test_equal_unhashable_locks_reset_independently(tmp_path: Path) -> None: # pragma: win32 no cover
25 @dataclass(eq=True, init=False) # pragma: win32 no cover
26 class EqualLock(BaseFileLock): # pragma: win32 no cover
27 def _acquire(self) -> None: # pragma: win32 no cover
28 self._context.lock_file_fd = os.open(self.lock_file, os.O_CREAT | os.O_RDWR, 0o600)
29
30 def _release(self) -> None: # pragma: win32 no cover
31 os.close(self._context.lock_file_fd if self._context.lock_file_fd is not None else -1)
32 self._context.lock_file_fd = None
33
34 first = EqualLock(str(tmp_path / "first.lock"), is_singleton=False)
35 second = EqualLock(str(tmp_path / "second.lock"), is_singleton=False)
36 first.acquire()
37 second.acquire()
38
39 def reset_child() -> NoReturn:
40 state = first.is_locked, first.lock_counter, second.is_locked, second.lock_counter
41 exit_child(0 if state == (False, 0, False, 0) else 1)
42
43 child_pid = fork_process(reset_child)
44 _, status = os.waitpid(child_pid, 0)
45 first.release()
46 second.release()
47
48 assert os.waitstatus_to_exitcode(status) == 0
49
50
51 @pytest.mark.requires_hard_links
52 def test_equal_unhashable_soft_read_write_locks_survive_atexit_registry(
53 tmp_path: Path,
54 ) -> None: # pragma: needs hard-link
55 script = """
56 from __future__ import annotations
57
58 import sys
59 from dataclasses import dataclass
60
61 from filelock import SoftReadWriteLock
62
63 @dataclass(eq=True, init=False)
64 class EqualLock(SoftReadWriteLock):
65 pass
66
67 locks = [EqualLock(path, is_singleton=False, heartbeat_interval=1, stale_threshold=30) for path in sys.argv[1:]]
68 for lock in locks:
69 lock.acquire_write()
70 """
71 paths = [tmp_path / "first.lock", tmp_path / "second.lock"]
72 result = subprocess.run(
73 [sys.executable, "-c", script, *(str(path) for path in paths)],
74 check=False,
75 capture_output=True,
76 text=True,
77 timeout=10,
78 )
79
80 writers = [GenerationLog(OsFiles(str(path)), str(path), f"{path}.rw").latest().writer for path in paths]
81 assert (result.returncode, writers) == (0, [None, None])
82
83
84 @NEEDS_CLASS_COLLECTION
85 def test_dynamic_lock_class_can_be_collected() -> None:
86 class EphemeralLock(BaseFileLock):
87 _acquire = _release = BaseFileLock._acquire
88
89 class_ref = weakref.ref(EphemeralLock)
90 del EphemeralLock
91 gc.collect()
92
93 assert class_ref() is None