filelock 4.2.5__tar.gz → 4.2.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {filelock-4.2.5 → filelock-4.2.7}/PKG-INFO +1 -1
- {filelock-4.2.5 → filelock-4.2.7}/docs/changelog.rst +35 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/concepts.rst +4 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_lease.py +4 -5
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft.py +9 -1
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_util.py +34 -2
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/version.py +2 -2
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_soft_lease.py +1 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_soft_stale.py +143 -0
- {filelock-4.2.5 → filelock-4.2.7}/.gitignore +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/LICENSE +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/_static/custom.css +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/api.rst +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/conf.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/example.gif +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/how-to.rst +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/index.rst +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/license.rst +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/logo.svg +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/docs/tutorials.rst +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/pyproject.toml +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/__init__.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_api.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_async.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_async_read_write.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_descriptor.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_error.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_identity.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_marker.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_read_write.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft_protocol.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft_rw/__init__.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft_rw/_async.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft_rw/_protocol.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft_rw/_storage.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_soft_rw/_sync.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_strict.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_unix.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/_windows.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/asyncio.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/src/filelock/py.typed +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/benchmark.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/capabilities.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/capability/assert_fallback.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/capability/sitecustomize.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/coverage_pragmas.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/deny_symlink.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/termux/Dockerfile +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tasks/verify_filesystem.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/async_filelock_cancellation_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/capability_marks.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/conftest.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/descriptor_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/fork_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/pid_namespace_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/process_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/read_write_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/soft_rw/conftest.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/soft_rw/test_soft_rw_async.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/soft_rw/test_soft_rw_protocol.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/soft_rw/test_soft_rw_storage.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/soft_rw/test_soft_rw_sync.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_filelock.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_filelock_acquire_cancellation.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_filelock_release_cancellation.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_filelock_rollback_agnostic.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_filelock_runner_shutdown.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_filelock_transition_admission.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_read_write.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_read_write_cancel_wait.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_read_write_cancellation.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_read_write_worker_ownership.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_soft_contracts.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_async_transition_gate.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_default_mode.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_descriptor_helpers.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_error.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_filelock.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_fork_backends.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_fork_coordination.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_fork_ownership.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_fork_registries.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_host_name_grammar.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_lazy_import.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_lifetime_validation.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_lock_expiry.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_marker_records.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_process_identity.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_read_write.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_read_write_fork.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_read_write_private_link.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_read_write_unit.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_soft_lifetime_warning.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft_compat.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft_failures.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft_paths.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft_races.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft_recovery.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_strict_soft_stress.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_subclass_options.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_unix_fallback.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_util.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tests/test_virtualenv.py +0 -0
- {filelock-4.2.5 → filelock-4.2.7}/tox.toml +0 -0
|
@@ -6,6 +6,41 @@
|
|
|
6
6
|
|
|
7
7
|
.. towncrier release notes start
|
|
8
8
|
|
|
9
|
+
.. _release-4-2-7:
|
|
10
|
+
|
|
11
|
+
********************
|
|
12
|
+
4.2.7 (2026-11-02)
|
|
13
|
+
********************
|
|
14
|
+
|
|
15
|
+
.. _release-4-2-7-fixed:
|
|
16
|
+
|
|
17
|
+
Fixed
|
|
18
|
+
=====
|
|
19
|
+
|
|
20
|
+
- A :class:`~filelock.SoftFileLease` reclaims a dead holder's lease marker even when that holder published another
|
|
21
|
+
``lease_duration``. It used to raise :class:`~filelock.LeaseSettingsMismatch` on every attempt instead, so after a
|
|
22
|
+
holder crashed and its peers restarted with a changed duration the lock stayed wedged until :meth:`force_break()
|
|
23
|
+
<filelock.MarkerSoftFileLock.force_break>`. A live holder with another duration still raises (:pr:`900`) by
|
|
24
|
+
:user:`dxbjavid` and :user:`gaborbernat`.
|
|
25
|
+
|
|
26
|
+
.. _release-4-2-6:
|
|
27
|
+
|
|
28
|
+
********************
|
|
29
|
+
4.2.6 (2026-10-31)
|
|
30
|
+
********************
|
|
31
|
+
|
|
32
|
+
.. _release-4-2-6-fixed:
|
|
33
|
+
|
|
34
|
+
Fixed
|
|
35
|
+
=====
|
|
36
|
+
|
|
37
|
+
- Fix a race in :class:`~filelock.SoftFileLock`, :class:`~filelock.MarkerSoftFileLock` and
|
|
38
|
+
:class:`~filelock.SoftFileLease` when three or more contenders reclaim a dead marker. The race could strand a live
|
|
39
|
+
holder's marker under a break name, leaving the lock unusable or admitting a second holder. Contenders now recheck the
|
|
40
|
+
marker before exchanging names and unwind nested exchanges in order. If the filesystem refuses a return exchange, the
|
|
41
|
+
contender leaves its marker at the lock path for later recovery (:pr:`899`) by :user:`dxbjavid` and
|
|
42
|
+
:user:`gaborbernat`.
|
|
43
|
+
|
|
9
44
|
.. _release-4-2-5:
|
|
10
45
|
|
|
11
46
|
********************
|
|
@@ -763,6 +763,10 @@ If the file it took is the one judged stale, it removes that file and holds the
|
|
|
763
763
|
exchanges after that moves the holder's marker aside only while it checks what it took, and puts it back. A holder that
|
|
764
764
|
releases while its marker is aside leaves a record no lock parses, so the put-back lands a marker that ages out.
|
|
765
765
|
Otherwise a peer recreated the marker, and a second exchange puts the live marker back before its own is removed.
|
|
766
|
+
Contenders that race for one marker nest, each holding what the one before it left at the path, so the exchanges back
|
|
767
|
+
run in reverse: each contender exchanges when the path names its own marker, and one that stalls exchanges anyway to
|
|
768
|
+
free a marker only it can move. It finishes once it holds its own marker again.
|
|
769
|
+
If the filesystem refuses the return exchange, it leaves its marker at the lock path for a later contender to recover.
|
|
766
770
|
|
|
767
771
|
The private name is ``<lock>.break.<pid>.<token>``: the breaker's PID, then 32 random hex digits. A breaker killed
|
|
768
772
|
between its exchange and the put-back leaves its own marker at the lock path and a live holder's marker under that name.
|
|
@@ -261,6 +261,10 @@ class SoftFileLease(MarkerSoftFileLock):
|
|
|
261
261
|
owner, marker = peer
|
|
262
262
|
if pinned not in {None, marker.st_ino}: # pragma: win32 no cover # Windows pins nothing
|
|
263
263
|
return # replaced before the read, so nothing keeps the number read from reuse
|
|
264
|
+
# Duration governs age-based expiry; a dead owner has no lease left to disagree with.
|
|
265
|
+
if owner.mode != "unknown" and owner_is_stale(owner.pid, owner.hostname, owner.start):
|
|
266
|
+
self._take_over(marker, owner.pid)
|
|
267
|
+
return
|
|
264
268
|
# Only a peer that published a lease agreed to be superseded by age; an exclusive owner is reclaimed only once
|
|
265
269
|
# it is provably dead, and a contract this version does not know never is. Raise the mismatch outside the read
|
|
266
270
|
# so the suppression cannot swallow it.
|
|
@@ -270,11 +274,6 @@ class SoftFileLease(MarkerSoftFileLock):
|
|
|
270
274
|
f"{self._lease_duration!r}s; every contender for a path must agree on lease_duration"
|
|
271
275
|
)
|
|
272
276
|
raise LeaseSettingsMismatch(msg)
|
|
273
|
-
# A dead or recycled owner is reclaimed at once, whether it held a lease or an exclusive marker; a live lease
|
|
274
|
-
# owner whose marker stops being refreshed is superseded on the schedule every contender agreed to.
|
|
275
|
-
if owner.mode != "unknown" and owner_is_stale(owner.pid, owner.hostname, owner.start):
|
|
276
|
-
self._take_over(marker, owner.pid)
|
|
277
|
-
return
|
|
278
277
|
if owner.mode == "lease" and self._marker_unchanged_for(marker) >= self._lease_duration:
|
|
279
278
|
self._take_over(marker, None)
|
|
280
279
|
|
|
@@ -30,6 +30,7 @@ from filelock._identity import host_name, owner_is_current_process, owner_is_sta
|
|
|
30
30
|
from filelock._soft_protocol import STRICT_SOFT_SENTINEL_RECORD
|
|
31
31
|
from filelock._util import (
|
|
32
32
|
break_lock_file,
|
|
33
|
+
exchange_back,
|
|
33
34
|
exchange_names,
|
|
34
35
|
exchange_supported,
|
|
35
36
|
pin_inode,
|
|
@@ -210,6 +211,10 @@ class SoftFileLock(BaseFileLock):
|
|
|
210
211
|
identity: Final = _file_identity(os.fstat(fd))
|
|
211
212
|
self._apply_explicit_mode(fd)
|
|
212
213
|
self._write_lock_info(fd)
|
|
214
|
+
# The scan and the write take long enough for a peer to have taken the marker since the check above, and an
|
|
215
|
+
# exchange now would move that peer's live marker aside.
|
|
216
|
+
if (current := os.lstat(self._lock_path)).st_mtime != marker.st_mtime or current.st_ino != marker.st_ino:
|
|
217
|
+
return
|
|
213
218
|
if not exchange_names(aside, self._lock_path):
|
|
214
219
|
return # NFS, SMB and the like refuse it; renaming the marker aside would empty the path
|
|
215
220
|
exchanged = True
|
|
@@ -318,14 +323,17 @@ class SoftFileLock(BaseFileLock):
|
|
|
318
323
|
|
|
319
324
|
def _abandon_take_over(self, fd: int, aside: str, *, put_back: bool) -> None: # pragma: win32 no cover
|
|
320
325
|
self._mark_descriptor_released()
|
|
326
|
+
put_back_done = not put_back
|
|
321
327
|
try:
|
|
322
328
|
if put_back:
|
|
323
|
-
|
|
329
|
+
put_back_done = exchange_back(self._lock_path, aside, fd)
|
|
324
330
|
finally:
|
|
325
331
|
try:
|
|
326
332
|
# Whichever name still holds our marker loses it: the descriptor keeps its inode number ours.
|
|
327
333
|
own: Final = os.fstat(fd)
|
|
328
334
|
for name in (self._lock_path, aside):
|
|
335
|
+
if name == self._lock_path and not put_back_done:
|
|
336
|
+
continue
|
|
329
337
|
with suppress(FileNotFoundError):
|
|
330
338
|
if os.path.samestat(os.lstat(name), own):
|
|
331
339
|
Path(name).unlink()
|
|
@@ -8,6 +8,7 @@ import platform
|
|
|
8
8
|
import secrets
|
|
9
9
|
import stat
|
|
10
10
|
import sys
|
|
11
|
+
import time
|
|
11
12
|
from contextlib import contextmanager, suppress
|
|
12
13
|
from errno import EACCES, EINVAL, EIO, EISDIR, ENOSYS, EXDEV
|
|
13
14
|
from functools import cache
|
|
@@ -234,12 +235,42 @@ def _swap_out(lock_file: str, break_path: str, mtime_before: float, ino_before:
|
|
|
234
235
|
if os.path.samestat(os.lstat(lock_file), os.fstat(fd)):
|
|
235
236
|
Path(lock_file).unlink()
|
|
236
237
|
else:
|
|
237
|
-
|
|
238
|
-
Path(break_path).unlink()
|
|
238
|
+
exchange_back(lock_file, break_path, fd)
|
|
239
239
|
finally:
|
|
240
240
|
os.close(fd)
|
|
241
241
|
|
|
242
242
|
|
|
243
|
+
def exchange_back(lock_file: str, aside: str, fd: int) -> bool: # pragma: win32 no cover
|
|
244
|
+
# Raced breakers nest: each aside holds the prior marker. Returning out of order can strand a live holder's marker.
|
|
245
|
+
# A peer may move the path between our check and exchange, so stalled breakers exchange to open any resulting ring.
|
|
246
|
+
try:
|
|
247
|
+
settled = _exchange_until_aside(lock_file, aside, os.fstat(fd))
|
|
248
|
+
except FileNotFoundError:
|
|
249
|
+
settled = True
|
|
250
|
+
if settled:
|
|
251
|
+
Path(aside).unlink(missing_ok=True)
|
|
252
|
+
return settled
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _exchange_until_aside(lock_file: str, aside: str, own: os.stat_result) -> bool: # pragma: win32 no cover
|
|
256
|
+
# Peers need a few system calls to unwind; a paused or dead peer must not block the caller indefinitely.
|
|
257
|
+
deadline: Final = time.monotonic() + 2.0
|
|
258
|
+
stall: Final = 0.01
|
|
259
|
+
feed_at = time.monotonic() + stall
|
|
260
|
+
while not os.path.samestat(os.lstat(aside), own):
|
|
261
|
+
now = time.monotonic()
|
|
262
|
+
ours = os.path.samestat(os.lstat(lock_file), own)
|
|
263
|
+
if not ours and now >= deadline:
|
|
264
|
+
return False
|
|
265
|
+
if ours or now >= feed_at:
|
|
266
|
+
if not exchange_names(aside, lock_file):
|
|
267
|
+
return False
|
|
268
|
+
feed_at = now + stall
|
|
269
|
+
else:
|
|
270
|
+
time.sleep(0.0005)
|
|
271
|
+
return True
|
|
272
|
+
|
|
273
|
+
|
|
243
274
|
def _break_by_rename(
|
|
244
275
|
lock_file: str, break_path: str, mtime_before: float, ino_before: int
|
|
245
276
|
) -> None: # pragma: win32 cover
|
|
@@ -387,6 +418,7 @@ _SUPPORTS_UTIME_NOFOLLOW: Final[bool] = os.utime in os.supports_follow_symlinks
|
|
|
387
418
|
__all__ = [
|
|
388
419
|
"break_lock_file",
|
|
389
420
|
"ensure_directory_exists",
|
|
421
|
+
"exchange_back",
|
|
390
422
|
"exchange_names",
|
|
391
423
|
"exchange_supported",
|
|
392
424
|
"foreign_directory_owner",
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '4.2.
|
|
22
|
-
__version_tuple__ = version_tuple = (4, 2,
|
|
21
|
+
__version__ = version = '4.2.7'
|
|
22
|
+
__version_tuple__ = version_tuple = (4, 2, 7)
|
|
23
23
|
|
|
24
24
|
__commit_id__ = commit_id = None
|
|
@@ -226,6 +226,7 @@ def test_lease_self_heals_a_malformed_marker(marker: Path, mocker: MockerFixture
|
|
|
226
226
|
"contract",
|
|
227
227
|
[
|
|
228
228
|
pytest.param(f"mode=lease\ntoken=abc\nduration={_DURATION!r}\n", id="lease"),
|
|
229
|
+
pytest.param(f"mode=lease\ntoken=abc\nduration={_DURATION * 3!r}\n", id="lease-other-duration"),
|
|
229
230
|
pytest.param("mode=exclusive\n", id="exclusive"),
|
|
230
231
|
],
|
|
231
232
|
)
|
|
@@ -6,6 +6,7 @@ import os
|
|
|
6
6
|
import socket
|
|
7
7
|
import sys
|
|
8
8
|
import threading
|
|
9
|
+
import time
|
|
9
10
|
from contextlib import contextmanager, suppress
|
|
10
11
|
from errno import EACCES, EINTR, EINVAL, EIO, ENODEV, ENOSPC, EPERM
|
|
11
12
|
from multiprocessing import get_context
|
|
@@ -15,6 +16,7 @@ from typing import TYPE_CHECKING, Final
|
|
|
15
16
|
import pytest
|
|
16
17
|
from capabilities import CAPABILITIES
|
|
17
18
|
|
|
19
|
+
import filelock._soft
|
|
18
20
|
from filelock import CloseErrorPolicy, MarkerSoftFileLock, SoftFileLease, SoftFileLock, Timeout
|
|
19
21
|
from filelock._identity import host_name, process_start_token
|
|
20
22
|
from filelock._soft import _MAX_LOCK_FILE_SIZE, MALFORMED_LOCK_AGE_THRESHOLD, _begin_claim, _end_claim, _read_lock_file
|
|
@@ -783,6 +785,147 @@ def test_stale_break_undoes_an_exchange_that_took_a_peers_marker( # pragma: win
|
|
|
783
785
|
) == (_live(record), [foreign], [])
|
|
784
786
|
|
|
785
787
|
|
|
788
|
+
def _lose_to_a_live_holder( # pragma: win32 no cover
|
|
789
|
+
lock: Path, live: str, mocker: MockerFixture, then: Callable[[str, str], bool | None]
|
|
790
|
+
) -> None:
|
|
791
|
+
(holder := lock.with_name("holder")).write_text(live, encoding="utf-8")
|
|
792
|
+
|
|
793
|
+
def exchange_after_the_holder_won(aside: str, path: str) -> bool:
|
|
794
|
+
mocker.patch("filelock._soft.exchange_names", new=exchange_names)
|
|
795
|
+
holder.replace(lock)
|
|
796
|
+
exchange_names(aside, path)
|
|
797
|
+
then(aside, path)
|
|
798
|
+
return True
|
|
799
|
+
|
|
800
|
+
mocker.patch("filelock._soft.exchange_names", new=exchange_after_the_holder_won)
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
804
|
+
@STALE_BREAKERS
|
|
805
|
+
@NEEDS_STALE_RECLAIM
|
|
806
|
+
def test_stale_break_exchanges_back_after_the_breaker_that_took_its_marker( # pragma: win32 no cover
|
|
807
|
+
tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
|
|
808
|
+
) -> None:
|
|
809
|
+
(lock := tmp_path / "test.lock").write_text(record, encoding="utf-8")
|
|
810
|
+
(peer := tmp_path / "peer").write_text(record.replace(_HOST, "elsewhere"), encoding="utf-8")
|
|
811
|
+
|
|
812
|
+
def peer_exchanges_back(_seconds: float) -> None:
|
|
813
|
+
exchange_names(str(peer), str(lock))
|
|
814
|
+
peer.unlink()
|
|
815
|
+
|
|
816
|
+
_lose_to_a_live_holder(lock, _live(record), mocker, lambda _aside, path: exchange_names(str(peer), path))
|
|
817
|
+
mocker.patch("filelock._util.time", wraps=time, sleep=mocker.Mock(side_effect=peer_exchanges_back))
|
|
818
|
+
with pytest.raises(Timeout):
|
|
819
|
+
lock_type(lock).acquire(blocking=False)
|
|
820
|
+
|
|
821
|
+
assert (lock.read_text(encoding="utf-8"), sorted(tmp_path.iterdir())) == (_live(record), [lock])
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
825
|
+
@STALE_BREAKERS
|
|
826
|
+
@NEEDS_STALE_RECLAIM
|
|
827
|
+
def test_stale_break_opens_a_ring_of_breakers_holding_each_others_markers( # pragma: win32 no cover
|
|
828
|
+
tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
|
|
829
|
+
) -> None:
|
|
830
|
+
(lock := tmp_path / "test.lock").write_text(record, encoding="utf-8")
|
|
831
|
+
foreign: Final = record.replace(_HOST, "elsewhere")
|
|
832
|
+
(peer := tmp_path / "peer").write_text(foreign, encoding="utf-8")
|
|
833
|
+
|
|
834
|
+
def peer_then_our_exchange_out_of_turn(aside: str, path: str) -> None:
|
|
835
|
+
exchange_names(str(peer), path)
|
|
836
|
+
exchange_names(aside, path)
|
|
837
|
+
|
|
838
|
+
def peer_exchanges_back_once_it_stands_at_the_path(_seconds: float) -> None:
|
|
839
|
+
if lock.read_text(encoding="utf-8") == foreign:
|
|
840
|
+
exchange_names(str(peer), str(lock))
|
|
841
|
+
peer.unlink()
|
|
842
|
+
|
|
843
|
+
_lose_to_a_live_holder(lock, _live(record), mocker, peer_then_our_exchange_out_of_turn)
|
|
844
|
+
mocker.patch(
|
|
845
|
+
"filelock._util.time", wraps=time, sleep=mocker.Mock(side_effect=peer_exchanges_back_once_it_stands_at_the_path)
|
|
846
|
+
)
|
|
847
|
+
with pytest.raises(Timeout):
|
|
848
|
+
lock_type(lock).acquire(blocking=False)
|
|
849
|
+
|
|
850
|
+
assert (lock.read_text(encoding="utf-8"), sorted(tmp_path.iterdir())) == (_live(record), [lock])
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
854
|
+
@STALE_BREAKERS
|
|
855
|
+
@NEEDS_STALE_RECLAIM
|
|
856
|
+
def test_stale_break_keeps_every_name_when_the_path_never_comes_back( # pragma: win32 no cover
|
|
857
|
+
tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
|
|
858
|
+
) -> None:
|
|
859
|
+
(lock := tmp_path / "test.lock").write_text(record, encoding="utf-8")
|
|
860
|
+
foreign: Final = record.replace(_HOST, "elsewhere")
|
|
861
|
+
(peer := tmp_path / "peer").write_text(foreign, encoding="utf-8")
|
|
862
|
+
_lose_to_a_live_holder(lock, _live(record), mocker, lambda _aside, path: exchange_names(str(peer), path))
|
|
863
|
+
with pytest.raises(Timeout):
|
|
864
|
+
lock_type(lock).acquire(blocking=False)
|
|
865
|
+
|
|
866
|
+
assert sorted([lock.read_text(encoding="utf-8"), *_break_contents(lock)]) == sorted([foreign, _live(record)])
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
870
|
+
@STALE_BREAKERS
|
|
871
|
+
@NEEDS_STALE_RECLAIM
|
|
872
|
+
def test_stale_break_keeps_its_marker_when_exchange_back_is_refused( # pragma: win32 no cover
|
|
873
|
+
tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
|
|
874
|
+
) -> None:
|
|
875
|
+
(lock := tmp_path / "test.lock").write_text(record, encoding="utf-8")
|
|
876
|
+
_lose_to_a_live_holder(lock, _live(record), mocker, lambda _aside, _path: None)
|
|
877
|
+
mocker.patch("filelock._util.exchange_names", side_effect=[False, AssertionError("retried refused exchange")])
|
|
878
|
+
with pytest.raises(Timeout):
|
|
879
|
+
lock_type(lock).acquire(blocking=False)
|
|
880
|
+
|
|
881
|
+
assert (f"{os.getpid()}\n" in lock.read_text(encoding="utf-8"), _break_contents(lock)) == (
|
|
882
|
+
True,
|
|
883
|
+
[_live(record)],
|
|
884
|
+
)
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
888
|
+
@STALE_BREAKERS
|
|
889
|
+
@NEEDS_STALE_RECLAIM
|
|
890
|
+
def test_stale_break_drops_what_it_took_when_the_holder_released_the_path( # pragma: win32 no cover
|
|
891
|
+
tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
|
|
892
|
+
) -> None:
|
|
893
|
+
(lock := tmp_path / "test.lock").write_text(record, encoding="utf-8")
|
|
894
|
+
_lose_to_a_live_holder(lock, _live(record), mocker, lambda _aside, path: Path(path).unlink())
|
|
895
|
+
with pytest.raises(Timeout):
|
|
896
|
+
lock_type(lock).acquire(blocking=False)
|
|
897
|
+
|
|
898
|
+
assert list(tmp_path.iterdir()) == []
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
902
|
+
@STALE_BREAKERS
|
|
903
|
+
@NEEDS_STALE_RECLAIM
|
|
904
|
+
def test_stale_break_leaves_a_marker_taken_since_its_first_look_on_the_path( # pragma: win32 no cover
|
|
905
|
+
tmp_path: Path,
|
|
906
|
+
lock_type: type[SoftFileLock],
|
|
907
|
+
record: str,
|
|
908
|
+
mocker: MockerFixture,
|
|
909
|
+
on_break_recheck: Callable[[Path, Callable[[], None]], None],
|
|
910
|
+
) -> None:
|
|
911
|
+
(lock := tmp_path / "test.lock").write_text(record, encoding="utf-8")
|
|
912
|
+
(holder := tmp_path / "holder").write_text(_live(record), encoding="utf-8")
|
|
913
|
+
exchange: Final = mocker.spy(filelock._soft, "exchange_names")
|
|
914
|
+
|
|
915
|
+
def peer_takes_the_marker() -> None:
|
|
916
|
+
holder.replace(lock)
|
|
917
|
+
|
|
918
|
+
on_break_recheck(lock, peer_takes_the_marker)
|
|
919
|
+
with pytest.raises(Timeout):
|
|
920
|
+
lock_type(lock).acquire(blocking=False)
|
|
921
|
+
|
|
922
|
+
assert (exchange.call_count, lock.read_text(encoding="utf-8"), sorted(tmp_path.iterdir())) == (
|
|
923
|
+
0,
|
|
924
|
+
_live(record),
|
|
925
|
+
[lock],
|
|
926
|
+
)
|
|
927
|
+
|
|
928
|
+
|
|
786
929
|
@pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
|
|
787
930
|
@STALE_BREAKERS
|
|
788
931
|
@NEEDS_STALE_RECLAIM
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|