filelock 4.2.5__tar.gz → 4.2.6__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.
Files changed (105) hide show
  1. {filelock-4.2.5 → filelock-4.2.6}/PKG-INFO +1 -1
  2. {filelock-4.2.5 → filelock-4.2.6}/docs/changelog.rst +18 -0
  3. {filelock-4.2.5 → filelock-4.2.6}/docs/concepts.rst +4 -0
  4. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft.py +9 -1
  5. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_util.py +34 -2
  6. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/version.py +2 -2
  7. {filelock-4.2.5 → filelock-4.2.6}/tests/test_soft_stale.py +143 -0
  8. {filelock-4.2.5 → filelock-4.2.6}/.gitignore +0 -0
  9. {filelock-4.2.5 → filelock-4.2.6}/LICENSE +0 -0
  10. {filelock-4.2.5 → filelock-4.2.6}/docs/_static/custom.css +0 -0
  11. {filelock-4.2.5 → filelock-4.2.6}/docs/api.rst +0 -0
  12. {filelock-4.2.5 → filelock-4.2.6}/docs/conf.py +0 -0
  13. {filelock-4.2.5 → filelock-4.2.6}/docs/example.gif +0 -0
  14. {filelock-4.2.5 → filelock-4.2.6}/docs/how-to.rst +0 -0
  15. {filelock-4.2.5 → filelock-4.2.6}/docs/index.rst +0 -0
  16. {filelock-4.2.5 → filelock-4.2.6}/docs/license.rst +0 -0
  17. {filelock-4.2.5 → filelock-4.2.6}/docs/logo.svg +0 -0
  18. {filelock-4.2.5 → filelock-4.2.6}/docs/tutorials.rst +0 -0
  19. {filelock-4.2.5 → filelock-4.2.6}/pyproject.toml +0 -0
  20. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/__init__.py +0 -0
  21. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_api.py +0 -0
  22. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_async.py +0 -0
  23. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_async_read_write.py +0 -0
  24. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_descriptor.py +0 -0
  25. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_error.py +0 -0
  26. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_identity.py +0 -0
  27. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_lease.py +0 -0
  28. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_marker.py +0 -0
  29. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_read_write.py +0 -0
  30. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft_protocol.py +0 -0
  31. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft_rw/__init__.py +0 -0
  32. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft_rw/_async.py +0 -0
  33. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft_rw/_protocol.py +0 -0
  34. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft_rw/_storage.py +0 -0
  35. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_soft_rw/_sync.py +0 -0
  36. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_strict.py +0 -0
  37. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_unix.py +0 -0
  38. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/_windows.py +0 -0
  39. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/asyncio.py +0 -0
  40. {filelock-4.2.5 → filelock-4.2.6}/src/filelock/py.typed +0 -0
  41. {filelock-4.2.5 → filelock-4.2.6}/tasks/benchmark.py +0 -0
  42. {filelock-4.2.5 → filelock-4.2.6}/tasks/capabilities.py +0 -0
  43. {filelock-4.2.5 → filelock-4.2.6}/tasks/capability/assert_fallback.py +0 -0
  44. {filelock-4.2.5 → filelock-4.2.6}/tasks/capability/sitecustomize.py +0 -0
  45. {filelock-4.2.5 → filelock-4.2.6}/tasks/coverage_pragmas.py +0 -0
  46. {filelock-4.2.5 → filelock-4.2.6}/tasks/deny_symlink.py +0 -0
  47. {filelock-4.2.5 → filelock-4.2.6}/tasks/termux/Dockerfile +0 -0
  48. {filelock-4.2.5 → filelock-4.2.6}/tasks/verify_filesystem.py +0 -0
  49. {filelock-4.2.5 → filelock-4.2.6}/tests/async_filelock_cancellation_helpers.py +0 -0
  50. {filelock-4.2.5 → filelock-4.2.6}/tests/capability_marks.py +0 -0
  51. {filelock-4.2.5 → filelock-4.2.6}/tests/conftest.py +0 -0
  52. {filelock-4.2.5 → filelock-4.2.6}/tests/descriptor_helpers.py +0 -0
  53. {filelock-4.2.5 → filelock-4.2.6}/tests/fork_helpers.py +0 -0
  54. {filelock-4.2.5 → filelock-4.2.6}/tests/pid_namespace_helpers.py +0 -0
  55. {filelock-4.2.5 → filelock-4.2.6}/tests/process_helpers.py +0 -0
  56. {filelock-4.2.5 → filelock-4.2.6}/tests/read_write_helpers.py +0 -0
  57. {filelock-4.2.5 → filelock-4.2.6}/tests/soft_rw/conftest.py +0 -0
  58. {filelock-4.2.5 → filelock-4.2.6}/tests/soft_rw/test_soft_rw_async.py +0 -0
  59. {filelock-4.2.5 → filelock-4.2.6}/tests/soft_rw/test_soft_rw_protocol.py +0 -0
  60. {filelock-4.2.5 → filelock-4.2.6}/tests/soft_rw/test_soft_rw_storage.py +0 -0
  61. {filelock-4.2.5 → filelock-4.2.6}/tests/soft_rw/test_soft_rw_sync.py +0 -0
  62. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_filelock.py +0 -0
  63. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_filelock_acquire_cancellation.py +0 -0
  64. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_filelock_release_cancellation.py +0 -0
  65. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_filelock_rollback_agnostic.py +0 -0
  66. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_filelock_runner_shutdown.py +0 -0
  67. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_filelock_transition_admission.py +0 -0
  68. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_read_write.py +0 -0
  69. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_read_write_cancel_wait.py +0 -0
  70. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_read_write_cancellation.py +0 -0
  71. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_read_write_worker_ownership.py +0 -0
  72. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_soft_contracts.py +0 -0
  73. {filelock-4.2.5 → filelock-4.2.6}/tests/test_async_transition_gate.py +0 -0
  74. {filelock-4.2.5 → filelock-4.2.6}/tests/test_default_mode.py +0 -0
  75. {filelock-4.2.5 → filelock-4.2.6}/tests/test_descriptor_helpers.py +0 -0
  76. {filelock-4.2.5 → filelock-4.2.6}/tests/test_error.py +0 -0
  77. {filelock-4.2.5 → filelock-4.2.6}/tests/test_filelock.py +0 -0
  78. {filelock-4.2.5 → filelock-4.2.6}/tests/test_fork_backends.py +0 -0
  79. {filelock-4.2.5 → filelock-4.2.6}/tests/test_fork_coordination.py +0 -0
  80. {filelock-4.2.5 → filelock-4.2.6}/tests/test_fork_ownership.py +0 -0
  81. {filelock-4.2.5 → filelock-4.2.6}/tests/test_fork_registries.py +0 -0
  82. {filelock-4.2.5 → filelock-4.2.6}/tests/test_host_name_grammar.py +0 -0
  83. {filelock-4.2.5 → filelock-4.2.6}/tests/test_lazy_import.py +0 -0
  84. {filelock-4.2.5 → filelock-4.2.6}/tests/test_lifetime_validation.py +0 -0
  85. {filelock-4.2.5 → filelock-4.2.6}/tests/test_lock_expiry.py +0 -0
  86. {filelock-4.2.5 → filelock-4.2.6}/tests/test_marker_records.py +0 -0
  87. {filelock-4.2.5 → filelock-4.2.6}/tests/test_process_identity.py +0 -0
  88. {filelock-4.2.5 → filelock-4.2.6}/tests/test_read_write.py +0 -0
  89. {filelock-4.2.5 → filelock-4.2.6}/tests/test_read_write_fork.py +0 -0
  90. {filelock-4.2.5 → filelock-4.2.6}/tests/test_read_write_private_link.py +0 -0
  91. {filelock-4.2.5 → filelock-4.2.6}/tests/test_read_write_unit.py +0 -0
  92. {filelock-4.2.5 → filelock-4.2.6}/tests/test_soft_lease.py +0 -0
  93. {filelock-4.2.5 → filelock-4.2.6}/tests/test_soft_lifetime_warning.py +0 -0
  94. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft.py +0 -0
  95. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft_compat.py +0 -0
  96. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft_failures.py +0 -0
  97. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft_paths.py +0 -0
  98. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft_races.py +0 -0
  99. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft_recovery.py +0 -0
  100. {filelock-4.2.5 → filelock-4.2.6}/tests/test_strict_soft_stress.py +0 -0
  101. {filelock-4.2.5 → filelock-4.2.6}/tests/test_subclass_options.py +0 -0
  102. {filelock-4.2.5 → filelock-4.2.6}/tests/test_unix_fallback.py +0 -0
  103. {filelock-4.2.5 → filelock-4.2.6}/tests/test_util.py +0 -0
  104. {filelock-4.2.5 → filelock-4.2.6}/tests/test_virtualenv.py +0 -0
  105. {filelock-4.2.5 → filelock-4.2.6}/tox.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: filelock
3
- Version: 4.2.5
3
+ Version: 4.2.6
4
4
  Import-Name: filelock
5
5
  Summary: A platform independent file lock.
6
6
  Project-URL: Changelog, https://py-filelock.readthedocs.io/en/latest/changelog.html
@@ -6,6 +6,24 @@
6
6
 
7
7
  .. towncrier release notes start
8
8
 
9
+ .. _release-4-2-6:
10
+
11
+ ********************
12
+ 4.2.6 (2026-10-31)
13
+ ********************
14
+
15
+ .. _release-4-2-6-fixed:
16
+
17
+ Fixed
18
+ =====
19
+
20
+ - Fix a race in :class:`~filelock.SoftFileLock`, :class:`~filelock.MarkerSoftFileLock` and
21
+ :class:`~filelock.SoftFileLease` when three or more contenders reclaim a dead marker. The race could strand a live
22
+ holder's marker under a break name, leaving the lock unusable or admitting a second holder. Contenders now recheck the
23
+ marker before exchanging names and unwind nested exchanges in order. If the filesystem refuses a return exchange, the
24
+ contender leaves its marker at the lock path for later recovery (:pr:`899`) by :user:`dxbjavid` and
25
+ :user:`gaborbernat`.
26
+
9
27
  .. _release-4-2-5:
10
28
 
11
29
  ********************
@@ -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.
@@ -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
- exchange_names(aside, self._lock_path)
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
- exchange_names(break_path, lock_file)
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.5'
22
- __version_tuple__ = version_tuple = (4, 2, 5)
21
+ __version__ = version = '4.2.6'
22
+ __version_tuple__ = version_tuple = (4, 2, 6)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -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