| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import subprocess # ruff:ignore[suspicious-subprocess-import] # the process must crash between two filesystem operations |
| 5 |
import sys |
| 6 |
import textwrap |
| 7 |
from typing import TYPE_CHECKING, Final |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from filelock import StrictSoftFileLock, Timeout |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
# The crash points are hit through sys.addaudithook on os.link and os.remove events, which only CPython emits. PyPy |
| 17 |
# runs the audit hook but never fires those events, so the child would finish the acquisition instead of crashing. |
| 18 |
pytestmark = [ |
| 19 |
pytest.mark.skipif( |
| 20 |
sys.implementation.name != "cpython", |
| 21 |
reason="crash injection needs the os.link and os.remove audit events CPython emits", |
| 22 |
), |
| 23 |
pytest.mark.requires_hard_links, |
| 24 |
] |
| 25 |
|
| 26 |
_CRASH_STATUS: Final[int] = 73 |
| 27 |
|
| 28 |
|
| 29 |
def test_strict_soft_recovers_every_claim_for_crashed_token(tmp_path: Path) -> None: |
| 30 |
lock_path = tmp_path / "resource.lock" |
| 31 |
result = subprocess.run( |
| 32 |
[sys.executable, "-c", _crash_while_holding(), os.fspath(lock_path)], |
| 33 |
check=False, |
| 34 |
capture_output=True, |
| 35 |
text=True, |
| 36 |
) |
| 37 |
lock = StrictSoftFileLock(lock_path, timeout=0) |
| 38 |
claims = lock.claims |
| 39 |
|
| 40 |
assert (result.returncode, result.stdout, result.stderr, [claim.state for claim in claims]) == ( |
| 41 |
_CRASH_STATUS, |
| 42 |
"", |
| 43 |
"", |
| 44 |
["held", "intent"], |
| 45 |
) |
| 46 |
assert len({claim.token for claim in claims}) == 1 |
| 47 |
with pytest.raises(Timeout): |
| 48 |
lock.acquire() |
| 49 |
|
| 50 |
for claim in lock.claims: |
| 51 |
lock.force_break(claim.name) |
| 52 |
with lock: |
| 53 |
assert lock.is_locked |
| 54 |
|
| 55 |
|
| 56 |
def test_strict_soft_reclaims_crash_before_private_publication(tmp_path: Path) -> None: |
| 57 |
lock_path = tmp_path / "resource.lock" |
| 58 |
result = subprocess.run( |
| 59 |
[sys.executable, "-c", _crash_during_private_publication("before-link"), os.fspath(lock_path)], |
| 60 |
check=False, |
| 61 |
capture_output=True, |
| 62 |
text=True, |
| 63 |
) |
| 64 |
private_records = tuple(tmp_path.glob("**/.*.private-v1-*.tmp")) |
| 65 |
|
| 66 |
assert (result.returncode, result.stdout, result.stderr, len(private_records)) == (_CRASH_STATUS, "", "", 1) |
| 67 |
os.utime(private_records[0], (0, 0)) |
| 68 |
lock = StrictSoftFileLock(lock_path, timeout=0) |
| 69 |
assert (lock.claims, tuple(tmp_path.glob("**/.*.private-v1-*.tmp"))) == ((), ()) |
| 70 |
with lock: |
| 71 |
assert lock.is_locked |
| 72 |
|
| 73 |
|
| 74 |
def test_strict_soft_reclaims_crash_after_private_publication(tmp_path: Path) -> None: |
| 75 |
lock_path = tmp_path / "resource.lock" |
| 76 |
result = subprocess.run( |
| 77 |
[sys.executable, "-c", _crash_during_private_publication("after-link"), os.fspath(lock_path)], |
| 78 |
check=False, |
| 79 |
capture_output=True, |
| 80 |
text=True, |
| 81 |
) |
| 82 |
private_records = tuple(tmp_path.glob("**/.*.private-v1-*.tmp")) |
| 83 |
lock = StrictSoftFileLock(lock_path, timeout=0) |
| 84 |
claims = lock.claims |
| 85 |
|
| 86 |
assert (result.returncode, result.stdout, result.stderr, len(private_records), len(claims)) == ( |
| 87 |
_CRASH_STATUS, |
| 88 |
"", |
| 89 |
"", |
| 90 |
1, |
| 91 |
1, |
| 92 |
) |
| 93 |
assert tuple(tmp_path.glob("**/.*.private-v1-*.tmp")) == () |
| 94 |
lock.force_break(claims[0].name) |
| 95 |
with lock: |
| 96 |
assert lock.is_locked |
| 97 |
|
| 98 |
|
| 99 |
def _crash_while_holding() -> str: |
| 100 |
# A strict holder keeps both its held and intent claims for the whole hold, so a hard crash mid-hold leaves the |
| 101 |
# pair behind. os._exit skips every finalizer, reproducing a killed process that never releases. |
| 102 |
return textwrap.dedent( |
| 103 |
f""" |
| 104 |
from __future__ import annotations |
| 105 |
|
| 106 |
import os |
| 107 |
import sys |
| 108 |
|
| 109 |
from filelock import StrictSoftFileLock |
| 110 |
|
| 111 |
lock = StrictSoftFileLock(sys.argv[1]) |
| 112 |
lock.acquire() |
| 113 |
os._exit({_CRASH_STATUS}) |
| 114 |
""" |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
def _crash_during_private_publication(stage: str) -> str: |
| 119 |
return textwrap.dedent( |
| 120 |
f""" |
| 121 |
from __future__ import annotations |
| 122 |
|
| 123 |
import os |
| 124 |
import sys |
| 125 |
|
| 126 |
from filelock import StrictSoftFileLock |
| 127 |
|
| 128 |
def exit_at_publication_stage(event: str, args: tuple[str | int, ...]) -> None: |
| 129 |
if event not in {{"os.link", "os.remove"}}: |
| 130 |
return |
| 131 |
path = os.path.basename(os.fsdecode(args[1] if event == "os.link" else args[0])) |
| 132 |
before_link = {stage!r} == "before-link" and event == "os.link" and path.startswith("intent-") |
| 133 |
after_link = ( |
| 134 |
{stage!r} == "after-link" |
| 135 |
and event == "os.remove" |
| 136 |
and path.startswith(".intent-") |
| 137 |
and ".private-v1-" in path |
| 138 |
and path.endswith(".tmp") |
| 139 |
) |
| 140 |
if before_link or after_link: |
| 141 |
os._exit({_CRASH_STATUS}) |
| 142 |
|
| 143 |
sys.addaudithook(exit_at_publication_stage) |
| 144 |
StrictSoftFileLock(sys.argv[1]).acquire() |
| 145 |
""" |
| 146 |
) |