filelock 4.2.1__tar.gz → 4.2.3__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.1 → filelock-4.2.3}/PKG-INFO +1 -1
  2. {filelock-4.2.1 → filelock-4.2.3}/docs/changelog.rst +17 -0
  3. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_api.py +2 -3
  4. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft_rw/_storage.py +3 -1
  5. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/version.py +2 -2
  6. {filelock-4.2.1 → filelock-4.2.3}/tests/capability_marks.py +8 -0
  7. {filelock-4.2.1 → filelock-4.2.3}/tests/soft_rw/test_soft_rw_protocol.py +57 -5
  8. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_filelock.py +2 -0
  9. {filelock-4.2.1 → filelock-4.2.3}/tests/test_filelock.py +42 -0
  10. {filelock-4.2.1 → filelock-4.2.3}/tests/test_lazy_import.py +14 -0
  11. {filelock-4.2.1 → filelock-4.2.3}/tests/test_lock_expiry.py +4 -0
  12. {filelock-4.2.1 → filelock-4.2.3}/tests/test_marker_records.py +4 -0
  13. {filelock-4.2.1 → filelock-4.2.3}/tests/test_read_write_fork.py +10 -3
  14. {filelock-4.2.1 → filelock-4.2.3}/tests/test_soft_lease.py +13 -1
  15. {filelock-4.2.1 → filelock-4.2.3}/tests/test_soft_stale.py +28 -1
  16. {filelock-4.2.1 → filelock-4.2.3}/tests/test_util.py +20 -1
  17. {filelock-4.2.1 → filelock-4.2.3}/.gitignore +0 -0
  18. {filelock-4.2.1 → filelock-4.2.3}/LICENSE +0 -0
  19. {filelock-4.2.1 → filelock-4.2.3}/README.md +0 -0
  20. {filelock-4.2.1 → filelock-4.2.3}/docs/_static/custom.css +0 -0
  21. {filelock-4.2.1 → filelock-4.2.3}/docs/api.rst +0 -0
  22. {filelock-4.2.1 → filelock-4.2.3}/docs/changelog/template.jinja2 +0 -0
  23. {filelock-4.2.1 → filelock-4.2.3}/docs/concepts.rst +0 -0
  24. {filelock-4.2.1 → filelock-4.2.3}/docs/conf.py +0 -0
  25. {filelock-4.2.1 → filelock-4.2.3}/docs/example.gif +0 -0
  26. {filelock-4.2.1 → filelock-4.2.3}/docs/how-to.rst +0 -0
  27. {filelock-4.2.1 → filelock-4.2.3}/docs/index.rst +0 -0
  28. {filelock-4.2.1 → filelock-4.2.3}/docs/license.rst +0 -0
  29. {filelock-4.2.1 → filelock-4.2.3}/docs/logo.svg +0 -0
  30. {filelock-4.2.1 → filelock-4.2.3}/docs/tutorials.rst +0 -0
  31. {filelock-4.2.1 → filelock-4.2.3}/pyproject.toml +0 -0
  32. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/__init__.py +0 -0
  33. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_async.py +0 -0
  34. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_async_read_write.py +0 -0
  35. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_descriptor.py +0 -0
  36. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_error.py +0 -0
  37. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_identity.py +0 -0
  38. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_lease.py +0 -0
  39. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_marker.py +0 -0
  40. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_read_write.py +0 -0
  41. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft.py +0 -0
  42. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft_protocol.py +0 -0
  43. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft_rw/__init__.py +0 -0
  44. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft_rw/_async.py +0 -0
  45. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft_rw/_protocol.py +0 -0
  46. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_soft_rw/_sync.py +0 -0
  47. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_strict.py +0 -0
  48. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_unix.py +0 -0
  49. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_util.py +0 -0
  50. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/_windows.py +0 -0
  51. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/asyncio.py +0 -0
  52. {filelock-4.2.1 → filelock-4.2.3}/src/filelock/py.typed +0 -0
  53. {filelock-4.2.1 → filelock-4.2.3}/tasks/benchmark.py +0 -0
  54. {filelock-4.2.1 → filelock-4.2.3}/tasks/capabilities.py +0 -0
  55. {filelock-4.2.1 → filelock-4.2.3}/tasks/capability/assert_fallback.py +0 -0
  56. {filelock-4.2.1 → filelock-4.2.3}/tasks/capability/sitecustomize.py +0 -0
  57. {filelock-4.2.1 → filelock-4.2.3}/tasks/coverage_pragmas.py +0 -0
  58. {filelock-4.2.1 → filelock-4.2.3}/tasks/deny_symlink.py +0 -0
  59. {filelock-4.2.1 → filelock-4.2.3}/tasks/termux/Dockerfile +0 -0
  60. {filelock-4.2.1 → filelock-4.2.3}/tasks/verify_filesystem.py +0 -0
  61. {filelock-4.2.1 → filelock-4.2.3}/tests/async_filelock_cancellation_helpers.py +0 -0
  62. {filelock-4.2.1 → filelock-4.2.3}/tests/conftest.py +0 -0
  63. {filelock-4.2.1 → filelock-4.2.3}/tests/fork_helpers.py +0 -0
  64. {filelock-4.2.1 → filelock-4.2.3}/tests/pid_namespace_helpers.py +0 -0
  65. {filelock-4.2.1 → filelock-4.2.3}/tests/process_helpers.py +0 -0
  66. {filelock-4.2.1 → filelock-4.2.3}/tests/read_write_helpers.py +0 -0
  67. {filelock-4.2.1 → filelock-4.2.3}/tests/soft_rw/conftest.py +0 -0
  68. {filelock-4.2.1 → filelock-4.2.3}/tests/soft_rw/test_soft_rw_async.py +0 -0
  69. {filelock-4.2.1 → filelock-4.2.3}/tests/soft_rw/test_soft_rw_storage.py +0 -0
  70. {filelock-4.2.1 → filelock-4.2.3}/tests/soft_rw/test_soft_rw_sync.py +0 -0
  71. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_filelock_acquire_cancellation.py +0 -0
  72. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_filelock_release_cancellation.py +0 -0
  73. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_filelock_rollback_agnostic.py +0 -0
  74. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_filelock_runner_shutdown.py +0 -0
  75. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_filelock_transition_admission.py +0 -0
  76. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_read_write.py +0 -0
  77. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_read_write_cancel_wait.py +0 -0
  78. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_read_write_cancellation.py +0 -0
  79. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_read_write_worker_ownership.py +0 -0
  80. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_soft_contracts.py +0 -0
  81. {filelock-4.2.1 → filelock-4.2.3}/tests/test_async_transition_gate.py +0 -0
  82. {filelock-4.2.1 → filelock-4.2.3}/tests/test_default_mode.py +0 -0
  83. {filelock-4.2.1 → filelock-4.2.3}/tests/test_error.py +0 -0
  84. {filelock-4.2.1 → filelock-4.2.3}/tests/test_fork_backends.py +0 -0
  85. {filelock-4.2.1 → filelock-4.2.3}/tests/test_fork_coordination.py +0 -0
  86. {filelock-4.2.1 → filelock-4.2.3}/tests/test_fork_ownership.py +0 -0
  87. {filelock-4.2.1 → filelock-4.2.3}/tests/test_fork_registries.py +0 -0
  88. {filelock-4.2.1 → filelock-4.2.3}/tests/test_host_name_grammar.py +0 -0
  89. {filelock-4.2.1 → filelock-4.2.3}/tests/test_lifetime_validation.py +0 -0
  90. {filelock-4.2.1 → filelock-4.2.3}/tests/test_process_identity.py +0 -0
  91. {filelock-4.2.1 → filelock-4.2.3}/tests/test_read_write.py +0 -0
  92. {filelock-4.2.1 → filelock-4.2.3}/tests/test_read_write_private_link.py +0 -0
  93. {filelock-4.2.1 → filelock-4.2.3}/tests/test_read_write_unit.py +0 -0
  94. {filelock-4.2.1 → filelock-4.2.3}/tests/test_soft_lifetime_warning.py +0 -0
  95. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft.py +0 -0
  96. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft_compat.py +0 -0
  97. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft_failures.py +0 -0
  98. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft_paths.py +0 -0
  99. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft_races.py +0 -0
  100. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft_recovery.py +0 -0
  101. {filelock-4.2.1 → filelock-4.2.3}/tests/test_strict_soft_stress.py +0 -0
  102. {filelock-4.2.1 → filelock-4.2.3}/tests/test_subclass_options.py +0 -0
  103. {filelock-4.2.1 → filelock-4.2.3}/tests/test_unix_fallback.py +0 -0
  104. {filelock-4.2.1 → filelock-4.2.3}/tests/test_virtualenv.py +0 -0
  105. {filelock-4.2.1 → filelock-4.2.3}/tox.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: filelock
3
- Version: 4.2.1
3
+ Version: 4.2.3
4
4
  Summary: A platform independent file lock.
5
5
  Project-URL: Documentation, https://py-filelock.readthedocs.io
6
6
  Project-URL: Homepage, https://github.com/tox-dev/py-filelock
@@ -6,6 +6,23 @@
6
6
 
7
7
  .. towncrier release notes start
8
8
 
9
+ *******************
10
+ 4.2.3 (2026-10-19)
11
+ *******************
12
+
13
+ - Creating a lock on a Python 3.15 interpreter without ``types.LazyImportType`` no longer triggers ``AttributeError`` at
14
+ exit. :pr:`860`
15
+ - Handle ``EFTYPE`` when a no-follow open refuses a symlink in a soft reader/writer lock's protocol tree, so heartbeat
16
+ reports lost ownership rather than a transient refresh error.
17
+
18
+ *******************
19
+ 4.2.2 (2026-10-18)
20
+ *******************
21
+
22
+ - Fix a lock on a relative path that stayed held after a later ``with lock:`` ended, when an earlier ``acquire()`` had
23
+ failed because the working directory no longer exists. That call still raises :class:`FileNotFoundError`, and it now
24
+ leaves the lock's hold count unchanged. :pr:`856`
25
+
9
26
  *******************
10
27
  4.2.1 (2026-10-17)
11
28
  *******************
@@ -1217,10 +1217,9 @@ class BaseFileLock(contextlib.ContextDecorator, metaclass=FileLockMeta): # ruff
1217
1217
  if _on_this_stack(self._context.releasing):
1218
1218
  msg = f"lock '{self.lock_file}' is acquired again while this thread's release of it runs"
1219
1219
  raise RuntimeError(msg)
1220
- # Bump the counter up front; _undo_acquire rolls it back if acquisition fails.
1220
+ canonical = _canonical(self.lock_file)
1221
1221
  self._context.lock_counter += 1
1222
1222
 
1223
- canonical = _canonical(self.lock_file)
1224
1223
  self._raise_if_would_deadlock(canonical, timeout=timeout, blocking=blocking)
1225
1224
  if not self.is_locked:
1226
1225
  self._pin_lock_path(canonical)
@@ -1952,7 +1951,7 @@ _SETTRACE_SURVIVES_AUDIT_HOOKS: Final[bool] = sys.implementation.name != "cpytho
1952
1951
 
1953
1952
  @cache
1954
1953
  def _resolve_lazy_imports_before_shutdown() -> None:
1955
- if sys.version_info >= (3, 15): # pragma: >=3.15 cover
1954
+ if hasattr(types, "LazyImportType"): # pragma: >=3.15 cover
1956
1955
  atexit.register(_resolve_lazy_imports)
1957
1956
 
1958
1957
 
@@ -4,6 +4,7 @@ from __future__ import annotations
4
4
 
5
5
  __lazy_modules__ = ["filelock._soft_rw._protocol", "filelock._strict", "filelock._util", "pathlib"]
6
6
 
7
+ import errno
7
8
  import os
8
9
  import stat
9
10
  import sys
@@ -227,7 +228,8 @@ class OsFiles:
227
228
  try:
228
229
  fd = os.open(name, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | _O_NOFOLLOW, dir_fd=parent_fd)
229
230
  except OSError as error:
230
- if error.errno not in {ELOOP, ENOTDIR}:
231
+ # NetBSD reports EFTYPE rather than ELOOP when O_NOFOLLOW refuses a symlink.
232
+ if error.errno not in {ELOOP, ENOTDIR, getattr(errno, "EFTYPE", ELOOP)}:
231
233
  raise
232
234
  msg = f"{path} is not a directory or is a symlink; refusing to use it"
233
235
  raise TreeReplacedError(msg) from error
@@ -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.1'
22
- __version_tuple__ = version_tuple = (4, 2, 1)
21
+ __version__ = version = '4.2.3'
22
+ __version_tuple__ = version_tuple = (4, 2, 3)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -14,6 +14,8 @@ from typing import TYPE_CHECKING
14
14
  import pytest
15
15
  from capabilities import CAPABILITIES
16
16
 
17
+ from filelock._util import exchange_supported
18
+
17
19
  if TYPE_CHECKING:
18
20
  from typing import Final
19
21
 
@@ -73,6 +75,11 @@ NEEDS_UNLINK_OPEN_FILE: Final[pytest.MarkDecorator] = pytest.mark.skipif(
73
75
  reason="this runtime keeps an open marker undeletable, so no peer can take it from a live holder",
74
76
  )
75
77
 
78
+ NEEDS_STALE_RECLAIM: Final[pytest.MarkDecorator] = pytest.mark.skipif(
79
+ sys.platform != "win32" and not exchange_supported(),
80
+ reason="safe stale recovery needs an atomic exchange of two names",
81
+ )
82
+
76
83
  NEEDS_POSIX_SIGNALS: Final[pytest.MarkDecorator] = pytest.mark.skipif(
77
84
  not CAPABILITIES["posix-signals"], reason="liveness is probed with os.kill only where POSIX signals exist"
78
85
  )
@@ -130,6 +137,7 @@ __all__ = [
130
137
  "NEEDS_PROMPT_FINALIZATION",
131
138
  "NEEDS_PROMPT_SIGNAL_DELIVERY",
132
139
  "NEEDS_REGISTER_AT_FORK",
140
+ "NEEDS_STALE_RECLAIM",
133
141
  "NEEDS_SYMLINK",
134
142
  "NEEDS_UNLINK_OPEN_FILE",
135
143
  "SKIP_ON_UNRELIABLE_PROCESS_SYNC",
@@ -12,6 +12,7 @@ would leave on disk.
12
12
 
13
13
  from __future__ import annotations
14
14
 
15
+ import errno
15
16
  import os
16
17
  import random
17
18
  import shutil
@@ -30,6 +31,7 @@ from filelock._soft_rw._protocol import (
30
31
  Ledger,
31
32
  Participant,
32
33
  Snapshot,
34
+ TreeReplacedError,
33
35
  encode_holder,
34
36
  holder_lease,
35
37
  parse_snapshot,
@@ -884,13 +886,25 @@ def test_heartbeat_ignores_a_broken_successor_record(lock_file: str, files: OsFi
884
886
 
885
887
 
886
888
  @pytest.mark.requires_hard_links
887
- def test_heartbeat_reports_the_refresh_error(lock_file: str, files: OsFiles, mocker: MockerFixture) -> None:
888
- writer = _participant(files, lock_file, "write")
889
+ @pytest.mark.parametrize(
890
+ ("error", "expected"),
891
+ [
892
+ pytest.param(OSError(EIO, "Input/output error"), "transient", id="filesystem-error"),
893
+ pytest.param(TreeReplacedError("tree replaced"), "lost", id="replaced-tree"),
894
+ ],
895
+ )
896
+ def test_heartbeat_reports_the_refresh_error(
897
+ lock_file: str,
898
+ files: OsFiles,
899
+ mocker: MockerFixture,
900
+ error: OSError | TreeReplacedError,
901
+ expected: Literal["transient", "lost"],
902
+ ) -> None:
903
+ writer: Final[Participant] = _participant(files, lock_file, "write")
889
904
  writer.publish()
890
905
  assert writer.advance()
891
- error = OSError(EIO, "Input/output error")
892
- mocker.patch.object(OsFiles, "overwrite", side_effect=error)
893
- assert writer.heartbeat() == ("transient", error)
906
+ mocker.patch.object(OsFiles, "overwrite", autospec=True, side_effect=error)
907
+ assert writer.heartbeat() == (expected, error if isinstance(error, OSError) else None)
894
908
 
895
909
 
896
910
  def test_heartbeat_without_a_published_record_is_lost(lock_file: str, files: OsFiles) -> None:
@@ -915,6 +929,44 @@ def test_heartbeat_with_the_tree_swapped_for_a_symlink_is_lost( # pragma: needs
915
929
  assert (writer.heartbeat(), list(target.iterdir())) == (("lost", None), [])
916
930
 
917
931
 
932
+ @pytest.mark.skipif(
933
+ not (CAPABILITIES["symlink"] and CAPABILITIES["link-dir-fd"]),
934
+ reason="a swapped tree is refused only where records are reached through descriptors",
935
+ )
936
+ @pytest.mark.requires_hard_links
937
+ @pytest.mark.parametrize("component", ["root", "holders"])
938
+ def test_heartbeat_with_a_symlink_refused_by_eftype_is_lost( # pragma: needs link-dir-fd
939
+ lock_file: str, files: OsFiles, root: str, tmp_path: Path, mocker: MockerFixture, component: str
940
+ ) -> None:
941
+ writer = _participant(files, lock_file, "write")
942
+ writer.publish()
943
+ assert writer.advance()
944
+ directory = Path(root) if component == "root" else Path(root, component)
945
+ directory.rename(tmp_path / "aside")
946
+ directory.symlink_to(target := tmp_path / "target", target_is_directory=True)
947
+ target.mkdir()
948
+ eftype = getattr(errno, "EFTYPE", 79)
949
+ mocker.patch.object(errno, "EFTYPE", eftype, create=True)
950
+ real_open = os.open
951
+ refusals: list[tuple[int, int | None]] = []
952
+
953
+ def netbsd_open(path: str, flags: int, mode: int = 0o777, *, dir_fd: int | None = None) -> int:
954
+ try:
955
+ return real_open(path, flags, mode, dir_fd=dir_fd)
956
+ except OSError as error:
957
+ # NetBSD uses EFTYPE for O_NOFOLLOW; keep the real traversal and translate only its symlink refusal.
958
+ refusals.append((flags, error.errno))
959
+ raise OSError(eftype, "Inappropriate file type or format", path) from error
960
+
961
+ mocker.patch.object(os, "open", side_effect=netbsd_open)
962
+
963
+ assert (writer.heartbeat(), list(target.iterdir())) == (("lost", None), [])
964
+ assert len(refusals) == 1
965
+ flags, native_errno = refusals[0]
966
+ assert flags & getattr(os, "O_NOFOLLOW", 0)
967
+ assert native_errno in {errno.ELOOP, errno.ENOTDIR, eftype}
968
+
969
+
918
970
  @pytest.mark.requires_hard_links
919
971
  def test_heartbeat_leaves_a_file_linked_over_the_holder_record_alone(lock_file: str, files: OsFiles, root: str) -> None:
920
972
  # A process that can write holders/ can rename a hard link to another of the holder's files over its record.
@@ -38,6 +38,7 @@ from tests.capability_marks import (
38
38
  NEEDS_PARENT_SYMLINK_COLLAPSE,
39
39
  NEEDS_PROMPT_FINALIZATION,
40
40
  NEEDS_PROMPT_SIGNAL_DELIVERY,
41
+ NEEDS_STALE_RECLAIM,
41
42
  )
42
43
 
43
44
  if TYPE_CHECKING:
@@ -1559,6 +1560,7 @@ def test_async_lock_on_a_daemon_loop_thread_held_at_interpreter_exit_releases_it
1559
1560
 
1560
1561
  @pytest.mark.filterwarnings("ignore::filelock.SoftFileLockLifetimeWarning")
1561
1562
  @pytest.mark.asyncio
1563
+ @NEEDS_STALE_RECLAIM
1562
1564
  async def test_executor_acquire_keeps_filesystem_calls_off_the_loop(tmp_path: Path, mocker: MockerFixture) -> None:
1563
1565
  lock_path = tmp_path / "a"
1564
1566
  lock_path.touch()
@@ -3213,6 +3213,48 @@ def test_each_acquisition_resolves_the_working_directory(
3213
3213
  assert (lock.lock_file, (second / "resource.lock").exists()) == ("resource.lock", True)
3214
3214
 
3215
3215
 
3216
+ @pytest.mark.skipif(
3217
+ sys.platform == "win32", reason="a working directory in use cannot be removed"
3218
+ ) # pragma: win32 no cover
3219
+ @pytest.mark.parametrize("lock_type", _RELATIVE_LOCK_TYPES)
3220
+ def test_acquire_without_working_directory_leaves_no_hold(
3221
+ lock_type: type[BaseFileLock], working_directories: tuple[Path, Path]
3222
+ ) -> None:
3223
+ lock: Final[BaseFileLock] = lock_type("resource.lock")
3224
+ working_directories[0].rmdir()
3225
+
3226
+ with pytest.raises(FileNotFoundError):
3227
+ lock.acquire()
3228
+ second: Final[Path] = working_directories[1]
3229
+ os.chdir(second)
3230
+ with lock:
3231
+ assert (lock.lock_counter, lock.is_locked, (second / "resource.lock").exists()) == (1, True, True)
3232
+
3233
+ assert (lock.lock_counter, lock.is_locked) == (0, False)
3234
+
3235
+
3236
+ @pytest.mark.skipif(
3237
+ sys.platform == "win32", reason="a working directory in use cannot be removed"
3238
+ ) # pragma: win32 no cover
3239
+ @pytest.mark.parametrize("lock_type", _RELATIVE_LOCK_TYPES)
3240
+ def test_nested_acquire_without_working_directory_preserves_hold(
3241
+ lock_type: type[BaseFileLock], working_directories: tuple[Path, Path]
3242
+ ) -> None:
3243
+ lock: Final[BaseFileLock] = lock_type("resource.lock")
3244
+ lock.acquire()
3245
+ first: Final[Path] = working_directories[0]
3246
+ (first / "resource.lock").unlink()
3247
+ first.rmdir()
3248
+
3249
+ with pytest.raises(FileNotFoundError):
3250
+ lock.acquire()
3251
+ assert (lock.lock_counter, lock.is_locked) == (1, True)
3252
+
3253
+ os.chdir(working_directories[1])
3254
+ lock.release()
3255
+ assert (lock.lock_counter, lock.is_locked) == (0, False)
3256
+
3257
+
3216
3258
  @pytest.mark.parametrize("lock_type", _RELATIVE_LOCK_TYPES)
3217
3259
  def test_singleton_after_chdir_locks_the_requested_file(
3218
3260
  lock_type: type[BaseFileLock], working_directories: tuple[Path, Path]
@@ -83,6 +83,20 @@ def test_lazy_import_unknown_name_raises() -> None:
83
83
  _ = filelock.missing
84
84
 
85
85
 
86
+ @pytest.mark.skipif(sys.version_info < (3, 15), reason="lazy imports start with Python 3.15")
87
+ def test_lazy_import_missing_runtime_type_does_not_raise_at_exit() -> None: # pragma: >=3.15 cover
88
+ assert not subprocess.run(
89
+ [
90
+ sys.executable,
91
+ "-c",
92
+ "import types; types.__dict__.pop('LazyImportType', None); import filelock; filelock.FileLock('a')",
93
+ ],
94
+ check=True,
95
+ capture_output=True,
96
+ text=True,
97
+ ).stderr
98
+
99
+
86
100
  _DAEMON_LOOP: Final = (
87
101
  "loop = asyncio.new_event_loop(); threading.Thread(target=loop.run_forever, daemon=True).start()\n"
88
102
  )
@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Final
9
9
  import pytest
10
10
 
11
11
  from filelock import AsyncFileLock, AsyncSoftFileLock, FileLock, SoftFileLock, Timeout
12
+ from tests.capability_marks import NEEDS_STALE_RECLAIM
12
13
 
13
14
  if TYPE_CHECKING:
14
15
  from collections.abc import Callable
@@ -20,6 +21,7 @@ _NATIVE_IGNORES: Final[str] = "lifetime is ignored"
20
21
  pytestmark: Final[pytest.MarkDecorator] = pytest.mark.filterwarnings("ignore::filelock.SoftFileLockLifetimeWarning")
21
22
 
22
23
 
24
+ @NEEDS_STALE_RECLAIM
23
25
  def test_expired_soft_lock_is_broken(tmp_path: Path) -> None:
24
26
  lock_path = tmp_path / "test.lock"
25
27
  lock_path.touch()
@@ -183,6 +185,7 @@ def test_lock_file_missing_during_expiry_check(tmp_path: Path) -> None:
183
185
  assert lock.is_locked
184
186
 
185
187
 
188
+ @NEEDS_STALE_RECLAIM
186
189
  def test_expired_lock_becomes_acquirable(tmp_path: Path) -> None:
187
190
  lock_path = tmp_path / "test.lock"
188
191
  lock_path.touch()
@@ -195,6 +198,7 @@ def test_expired_lock_becomes_acquirable(tmp_path: Path) -> None:
195
198
 
196
199
 
197
200
  @pytest.mark.asyncio
201
+ @NEEDS_STALE_RECLAIM
198
202
  async def test_async_expired_lock_is_broken(tmp_path: Path) -> None:
199
203
  lock_path = tmp_path / "test.lock"
200
204
  lock_path.touch()
@@ -13,6 +13,7 @@ from filelock import MarkerSoftFileLock, OwnerRecord, SoftFileLease, Timeout
13
13
  from filelock._identity import host_name, process_start_token
14
14
  from filelock._marker import encode_marker
15
15
  from filelock._soft import MALFORMED_LOCK_AGE_THRESHOLD
16
+ from tests.capability_marks import NEEDS_STALE_RECLAIM
16
17
  from tests.process_helpers import cleanup_processes
17
18
 
18
19
  if TYPE_CHECKING:
@@ -162,6 +163,7 @@ def test_lease_never_ages_out_an_unknown_contract(tmp_path: Path) -> None:
162
163
  assert marker.exists()
163
164
 
164
165
 
166
+ @NEEDS_STALE_RECLAIM
165
167
  def test_lease_ages_out_a_malformed_record(tmp_path: Path, mocker: MockerFixture) -> None:
166
168
  # The self-heal still applies to a record that states no contract at all, so corruption cannot wedge contenders.
167
169
  mocker.patch("filelock._soft.time.monotonic", side_effect=itertools.count(step=MALFORMED_LOCK_AGE_THRESHOLD))
@@ -246,6 +248,7 @@ def test_marker_lock_preserves_live_records(marker: Path, mode: str) -> None:
246
248
 
247
249
 
248
250
  @pytest.mark.timeout(_PROCESS_DEADLINE * 2)
251
+ @NEEDS_STALE_RECLAIM
249
252
  def test_marker_lock_reclaims_dead_owner(marker: Path) -> None:
250
253
  process: Final[BaseProcess] = get_context("spawn").Process(target=_leave_marker, args=(marker,))
251
254
  with cleanup_processes([process]):
@@ -267,6 +270,7 @@ def test_marker_lock_reclaims_dead_owner(marker: Path) -> None:
267
270
  pytest.param("filelock/2\npid=1\nhost=h\nmode=exclu", id="truncated-mode"),
268
271
  ],
269
272
  )
273
+ @NEEDS_STALE_RECLAIM
270
274
  def test_marker_lock_reclaims_malformed_record(marker: Path, record: str, mocker: MockerFixture) -> None:
271
275
  mocker.patch("filelock._soft.time.monotonic", side_effect=itertools.count(step=MALFORMED_LOCK_AGE_THRESHOLD))
272
276
  marker.write_text(record, encoding="utf-8")
@@ -535,10 +535,17 @@ def _dropped_instances_script() -> str: # pragma: needs fd-directory
535
535
  def _descriptors_on(path: Path) -> int: # pragma: needs fd-directory
536
536
  database: Final[os.stat_result] = path.stat()
537
537
  count = 0
538
- for entry in (Path("/dev/fd") if Path("/dev/fd").is_dir() else Path("/proc/self/fd")).iterdir():
539
- # The descriptor that listed the directory is already closed.
538
+ descriptor_ids: Final = (
539
+ range(os.sysconf("SC_OPEN_MAX"))
540
+ if sys.platform == "netbsd"
541
+ else (
542
+ int(entry.name)
543
+ for entry in (Path("/dev/fd") if Path("/dev/fd").is_dir() else Path("/proc/self/fd")).iterdir()
544
+ )
545
+ )
546
+ for descriptor_id in descriptor_ids:
540
547
  with contextlib.suppress(OSError):
541
- descriptor: Final[os.stat_result] = os.fstat(int(entry.name))
548
+ descriptor: Final[os.stat_result] = os.fstat(descriptor_id)
542
549
  count += (descriptor.st_dev, descriptor.st_ino) == (database.st_dev, database.st_ino)
543
550
  return count
544
551
 
@@ -29,7 +29,12 @@ from filelock import (
29
29
  from filelock._identity import host_name
30
30
  from filelock._soft import MALFORMED_LOCK_AGE_THRESHOLD
31
31
  from filelock._util import exchange_names
32
- from tests.capability_marks import NEEDS_COLLECTED_FINALIZATION, NEEDS_PROMPT_FINALIZATION, NEEDS_UNLINK_OPEN_FILE
32
+ from tests.capability_marks import (
33
+ NEEDS_COLLECTED_FINALIZATION,
34
+ NEEDS_PROMPT_FINALIZATION,
35
+ NEEDS_STALE_RECLAIM,
36
+ NEEDS_UNLINK_OPEN_FILE,
37
+ )
33
38
  from tests.conftest import held_after_reclaim
34
39
 
35
40
  if TYPE_CHECKING:
@@ -156,6 +161,7 @@ def test_lease_heartbeat_survives_directory_rename(
156
161
 
157
162
 
158
163
  @NEEDS_UNLINK_OPEN_FILE # pragma: needs unlink-open-file
164
+ @NEEDS_STALE_RECLAIM
159
165
  def test_lease_peer_takes_an_expired_claim(marker: Path, mocker: MockerFixture) -> None:
160
166
  # A wedged holder: its marker stays on disk, but no refresh ever lands on it again, so the claim ages out.
161
167
  mocker.patch("filelock._lease.touch")
@@ -171,6 +177,7 @@ def test_lease_peer_takes_an_expired_claim(marker: Path, mocker: MockerFixture)
171
177
  holder.release()
172
178
 
173
179
 
180
+ @NEEDS_STALE_RECLAIM
174
181
  def test_lease_self_heals_a_malformed_marker(marker: Path, mocker: MockerFixture) -> None:
175
182
  # The pytest timeout bounds recovery while the mocked clock controls marker expiry.
176
183
  clock: Final = mocker.patch("filelock._soft.time", autospec=True)
@@ -188,6 +195,7 @@ def test_lease_self_heals_a_malformed_marker(marker: Path, mocker: MockerFixture
188
195
  pytest.param("mode=exclusive\n", id="exclusive"),
189
196
  ],
190
197
  )
198
+ @NEEDS_STALE_RECLAIM
191
199
  def test_lease_reclaims_a_dead_same_host_holder(marker: Path, contract: str) -> None:
192
200
  marker.write_text(f"filelock/2\npid=999999\nhost={host_name()}\n{contract}", encoding="utf-8")
193
201
 
@@ -262,6 +270,7 @@ def test_lease_tolerates_a_transient_refresh_error(marker: Path, mocker: MockerF
262
270
 
263
271
 
264
272
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
273
+ @NEEDS_STALE_RECLAIM
265
274
  def test_lease_heartbeat_tolerates_one_refresh_under_a_peers_exchange( # pragma: win32 no cover
266
275
  marker: Path, mocker: MockerFixture
267
276
  ) -> None:
@@ -288,6 +297,7 @@ def test_lease_heartbeat_tolerates_one_refresh_under_a_peers_exchange( # pragma
288
297
 
289
298
 
290
299
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
300
+ @NEEDS_STALE_RECLAIM
291
301
  def test_lease_heartbeat_reports_an_owner_changed_that_persists(marker: Path) -> None: # pragma: win32 no cover
292
302
  seen: list[LeaseCompromise] = []
293
303
  lease: Final = SoftFileLease(marker, lease_duration=2, heartbeat_interval=0.05, on_compromise=seen.append)
@@ -650,6 +660,7 @@ def test_lease_drops_lifetime_with_a_warning(marker: Path) -> None:
650
660
  assert lease.lifetime is None
651
661
 
652
662
 
663
+ @NEEDS_STALE_RECLAIM
653
664
  def test_lease_supersedes_a_live_holder_once_its_claim_ages_out(marker: Path, mocker: MockerFixture) -> None:
654
665
  # A live holder whose refresh stalled keeps its marker, yet a contender takes it once it has watched the marker go
655
666
  # lease_duration without a refresh. Windows cannot delete a live holder's open marker, so drive the age branch with
@@ -952,6 +963,7 @@ def _lease_heartbeats() -> set[threading.Thread]:
952
963
 
953
964
 
954
965
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
966
+ @NEEDS_STALE_RECLAIM
955
967
  def test_lease_takes_over_a_dead_owners_marker_with_its_own_lease_record( # pragma: win32 no cover
956
968
  tmp_path: Path,
957
969
  ) -> None:
@@ -20,7 +20,7 @@ from filelock._identity import host_name, process_start_token
20
20
  from filelock._soft import _MAX_LOCK_FILE_SIZE, MALFORMED_LOCK_AGE_THRESHOLD, _begin_claim, _end_claim, _read_lock_file
21
21
  from filelock._soft_protocol import STRICT_SOFT_SENTINEL_RECORD
22
22
  from filelock._util import exchange_names
23
- from tests.capability_marks import NEEDS_POSIX_SIGNALS, NEEDS_SYMLINK, NEEDS_UNLINK_OPEN_FILE
23
+ from tests.capability_marks import NEEDS_POSIX_SIGNALS, NEEDS_STALE_RECLAIM, NEEDS_SYMLINK, NEEDS_UNLINK_OPEN_FILE
24
24
  from tests.conftest import DEAD_OWNER, STALE_BREAKERS, acquire_after_reclaim, held_after_reclaim
25
25
  from tests.pid_namespace_helpers import pin_pid_namespace
26
26
  from tests.process_helpers import break_then_hang_after_exchange, call_within, cleanup_processes
@@ -68,6 +68,7 @@ def test_lock_writes_pid_and_hostname(lock_path: Path) -> None:
68
68
  pytest.param(_holder(_DEAD_PID, start=123456789), id="three_line"),
69
69
  ],
70
70
  )
71
+ @NEEDS_STALE_RECLAIM
71
72
  def test_stale_lock_broken_when_process_dead(lock_path: Path, mocker: MockerFixture, content: str) -> None:
72
73
  lock_path.write_text(content, encoding="utf-8")
73
74
  mocker.patch("filelock._identity.process_alive", return_value=False)
@@ -182,6 +183,7 @@ def test_empty_lock_not_evicted_when_old(lock_path: Path, lock_type: type[SoftFi
182
183
 
183
184
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
184
185
  @STALE_BREAKERS
186
+ @NEEDS_STALE_RECLAIM
185
187
  def test_stale_break_restores_a_stranded_empty_marker( # pragma: win32 no cover
186
188
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
187
189
  ) -> None:
@@ -210,6 +212,7 @@ def test_timeout_names_an_owner_that_recorded_no_pid(lock_path: Path) -> None:
210
212
  ],
211
213
  )
212
214
  @pytest.mark.usefixtures("unchanged_past_grace")
215
+ @NEEDS_STALE_RECLAIM
213
216
  def test_unparseable_lock_evicted_when_old(lock_path: Path, content: bytes) -> None:
214
217
  lock_path.write_bytes(content)
215
218
  # An unreadable lock (bad line count, non-integer pid/start token, or oversized) must self-heal
@@ -264,6 +267,7 @@ def test_fresh_marker_with_truncated_start_token_not_evicted(lock_path: Path) ->
264
267
  ],
265
268
  )
266
269
  @pytest.mark.usefixtures("unchanged_past_grace")
270
+ @NEEDS_STALE_RECLAIM
267
271
  def test_out_of_range_pid_self_heals_when_old(lock_path: Path, pid: int) -> None:
268
272
  lock_path.write_text(_holder(pid), encoding="utf-8")
269
273
  # pid 0 or -1 makes os.kill probe the caller's own process group (reads as alive), so the lock is never
@@ -307,6 +311,7 @@ def test_stale_break_racing_a_successor_admits_no_third_holder(
307
311
 
308
312
 
309
313
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
314
+ @NEEDS_STALE_RECLAIM
310
315
  def test_stale_break_keeps_the_lock_when_a_peer_swaps_its_marker_aside( # pragma: win32 no cover
311
316
  lock_path: Path, mocker: MockerFixture
312
317
  ) -> None:
@@ -343,6 +348,7 @@ def _live(record: str) -> str: # pragma: win32 no cover
343
348
 
344
349
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
345
350
  @STALE_BREAKERS
351
+ @NEEDS_STALE_RECLAIM
346
352
  def test_stale_break_restores_a_live_marker_a_dead_breaker_left_under_its_break_name( # pragma: win32 no cover
347
353
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
348
354
  ) -> None:
@@ -356,6 +362,7 @@ def test_stale_break_restores_a_live_marker_a_dead_breaker_left_under_its_break_
356
362
 
357
363
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
358
364
  @STALE_BREAKERS
365
+ @NEEDS_STALE_RECLAIM
359
366
  def test_stale_break_restores_a_marker_of_another_host_with_the_breakers_pid( # pragma: win32 no cover
360
367
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
361
368
  ) -> None:
@@ -374,6 +381,7 @@ def _restore_name(lock: Path, pid: int) -> Path: # pragma: win32 no cover
374
381
 
375
382
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
376
383
  @STALE_BREAKERS
384
+ @NEEDS_STALE_RECLAIM
377
385
  def test_stale_break_restores_under_an_exclusive_claim( # pragma: win32 no cover
378
386
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
379
387
  ) -> None:
@@ -431,6 +439,7 @@ def test_stale_break_restores_under_an_exclusive_claim( # pragma: win32 no cove
431
439
 
432
440
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
433
441
  @STALE_BREAKERS
442
+ @NEEDS_STALE_RECLAIM
434
443
  def test_stale_break_waits_when_a_peer_claimed_the_record_first( # pragma: win32 no cover
435
444
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
436
445
  ) -> None:
@@ -470,6 +479,7 @@ def test_stale_break_waits_when_the_record_cannot_be_claimed( # pragma: win32 n
470
479
 
471
480
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
472
481
  @STALE_BREAKERS
482
+ @NEEDS_STALE_RECLAIM
473
483
  def test_stale_break_reclaims_a_record_a_dead_recoverer_claimed( # pragma: win32 no cover
474
484
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
475
485
  ) -> None:
@@ -509,6 +519,7 @@ def _refuse_returning_a_claim(mocker: MockerFixture) -> None: # pragma: win32 n
509
519
 
510
520
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
511
521
  @STALE_BREAKERS
522
+ @NEEDS_STALE_RECLAIM
512
523
  def test_stale_break_retries_a_claim_whose_return_failed( # pragma: win32 no cover
513
524
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
514
525
  ) -> None:
@@ -529,6 +540,7 @@ def test_stale_break_retries_a_claim_whose_return_failed( # pragma: win32 no co
529
540
 
530
541
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
531
542
  @STALE_BREAKERS
543
+ @NEEDS_STALE_RECLAIM
532
544
  def test_stale_break_finishes_a_claim_an_unreadable_record_interrupted( # pragma: win32 no cover
533
545
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
534
546
  ) -> None:
@@ -556,6 +568,7 @@ def test_stale_break_finishes_a_claim_an_unreadable_record_interrupted( # pragm
556
568
 
557
569
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
558
570
  @STALE_BREAKERS
571
+ @NEEDS_STALE_RECLAIM
559
572
  def test_stale_break_waits_for_a_claim_another_thread_is_working( # pragma: win32 no cover
560
573
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
561
574
  ) -> None:
@@ -598,6 +611,7 @@ def _acquire(lock_type: type[SoftFileLock], lock: Path) -> None: # pragma: win3
598
611
 
599
612
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
600
613
  @STALE_BREAKERS
614
+ @NEEDS_STALE_RECLAIM
601
615
  def test_stale_break_claim_is_registered_before_it_is_visible( # pragma: win32 no cover
602
616
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
603
617
  ) -> None:
@@ -678,6 +692,7 @@ def test_stale_break_claim_identity_ignores_path_spelling( # pragma: win32 no c
678
692
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
679
693
  @pytest.mark.parametrize("interrupted", ["rename", "read", "exchange"])
680
694
  @STALE_BREAKERS
695
+ @NEEDS_STALE_RECLAIM
681
696
  def test_stale_break_releases_the_registration_when_recovery_is_interrupted( # pragma: win32 no cover
682
697
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, interrupted: str, mocker: MockerFixture
683
698
  ) -> None:
@@ -718,6 +733,7 @@ def test_stale_break_releases_the_registration_when_recovery_is_interrupted( #
718
733
 
719
734
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
720
735
  @STALE_BREAKERS
736
+ @NEEDS_STALE_RECLAIM
721
737
  def test_stale_break_undoes_an_exchange_that_took_a_peers_marker( # pragma: win32 no cover
722
738
  tmp_path: Path, lock_type: type[SoftFileLock], record: str, mocker: MockerFixture
723
739
  ) -> None:
@@ -745,6 +761,7 @@ def test_stale_break_undoes_an_exchange_that_took_a_peers_marker( # pragma: win
745
761
 
746
762
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
747
763
  @STALE_BREAKERS
764
+ @NEEDS_STALE_RECLAIM
748
765
  def test_stale_break_removes_a_stale_marker_a_dead_breaker_left_under_its_break_name( # pragma: win32 no cover
749
766
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
750
767
  ) -> None:
@@ -759,6 +776,7 @@ def test_stale_break_removes_a_stale_marker_a_dead_breaker_left_under_its_break_
759
776
 
760
777
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
761
778
  @STALE_BREAKERS
779
+ @NEEDS_STALE_RECLAIM
762
780
  def test_stale_break_restores_a_strict_sentinel_a_dead_breaker_left_under_its_break_name( # pragma: win32 no cover
763
781
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
764
782
  ) -> None:
@@ -772,6 +790,7 @@ def test_stale_break_restores_a_strict_sentinel_a_dead_breaker_left_under_its_br
772
790
 
773
791
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
774
792
  @pytest.mark.parametrize("lock_type", [MarkerSoftFileLock, SoftFileLease])
793
+ @NEEDS_STALE_RECLAIM
775
794
  def test_stale_break_restores_a_marker_of_an_unknown_contract_under_its_break_name( # pragma: win32 no cover
776
795
  tmp_path: Path, lock_type: type[MarkerSoftFileLock]
777
796
  ) -> None:
@@ -787,6 +806,7 @@ def test_stale_break_restores_a_marker_of_an_unknown_contract_under_its_break_na
787
806
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
788
807
  @pytest.mark.usefixtures("unchanged_past_grace")
789
808
  @STALE_BREAKERS
809
+ @NEEDS_STALE_RECLAIM
790
810
  def test_stale_break_restores_a_marker_a_dead_breaker_left_beside_its_placeholder( # pragma: win32 no cover
791
811
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
792
812
  ) -> None:
@@ -814,6 +834,7 @@ def test_stale_break_restores_a_marker_a_dead_breaker_left_beside_its_placeholde
814
834
  pytest.param(f"filelock/break\n{_DEAD_PID}", id="torn-pid"),
815
835
  ],
816
836
  )
837
+ @NEEDS_STALE_RECLAIM
817
838
  def test_stale_break_evicts_a_placeholder_that_names_no_breaker( # pragma: win32 no cover
818
839
  tmp_path: Path, placeholder: str
819
840
  ) -> None:
@@ -871,6 +892,7 @@ def test_stale_break_waits_when_a_break_name_cannot_be_read( # pragma: win32 no
871
892
 
872
893
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
873
894
  @pytest.mark.timeout(_PROCESS_DEADLINE * 2)
895
+ @NEEDS_STALE_RECLAIM
874
896
  def test_stale_break_after_a_breaker_was_killed_keeps_the_holder_alone( # pragma: win32 no cover
875
897
  lock_path: Path,
876
898
  ) -> None:
@@ -914,6 +936,7 @@ def test_fifo_lock_file_does_not_block(lock_path: Path) -> None:
914
936
 
915
937
 
916
938
  @pytest.mark.usefixtures("unchanged_past_grace")
939
+ @NEEDS_STALE_RECLAIM
917
940
  def test_fifo_lock_file_with_attached_writer_self_heals(lock_path: Path) -> None:
918
941
  if sys.platform == "win32" or not CAPABILITIES["fifo"]: # pragma: win32 cover
919
942
  pytest.skip("os.mkfifo is unavailable") # the platform arm also narrows so ty resolves os.mkfifo below
@@ -930,6 +953,7 @@ def test_fifo_lock_file_with_attached_writer_self_heals(lock_path: Path) -> None
930
953
 
931
954
 
932
955
  @pytest.mark.usefixtures("unchanged_past_grace")
956
+ @NEEDS_STALE_RECLAIM
933
957
  def test_socket_lock_file_self_heals(lock_path: Path) -> None:
934
958
  if sys.platform == "win32": # pragma: win32 cover
935
959
  pytest.skip("AF_UNIX sockets are unix-only")
@@ -1183,6 +1207,7 @@ def test_failed_acquire_spares_a_successor_on_the_freed_inode_number( # pragma:
1183
1207
 
1184
1208
 
1185
1209
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
1210
+ @NEEDS_STALE_RECLAIM
1186
1211
  def test_failed_acquire_spares_a_file_that_replaced_its_placeholder( # pragma: win32 no cover
1187
1212
  lock_path: Path, mocker: MockerFixture
1188
1213
  ) -> None:
@@ -1227,6 +1252,7 @@ def test_publication_without_identity_leaves_the_marker(lock_path: Path, mocker:
1227
1252
 
1228
1253
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
1229
1254
  @pytest.mark.usefixtures("unchanged_past_grace")
1255
+ @NEEDS_STALE_RECLAIM
1230
1256
  def test_release_under_a_peers_exchange_leaves_no_marker_naming_this_process( # pragma: win32 no cover
1231
1257
  lock_path: Path,
1232
1258
  ) -> None:
@@ -1246,6 +1272,7 @@ def test_release_under_a_peers_exchange_leaves_no_marker_naming_this_process( #
1246
1272
 
1247
1273
 
1248
1274
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
1275
+ @NEEDS_STALE_RECLAIM
1249
1276
  def test_release_under_a_peers_exchange_leaves_the_peers_marker(lock_path: Path) -> None: # pragma: win32 no cover
1250
1277
  lock: Final = SoftFileLock(lock_path)
1251
1278
  lock.acquire()
@@ -16,7 +16,7 @@ import filelock._util
16
16
  from filelock import SoftFileLock, Timeout
17
17
  from filelock._identity import host_name
18
18
  from filelock._util import break_lock_file
19
- from tests.capability_marks import NEEDS_FILE_PERMISSIONS, NEEDS_SYMLINK, NEEDS_UNLINK_OPEN_FILE
19
+ from tests.capability_marks import NEEDS_FILE_PERMISSIONS, NEEDS_STALE_RECLAIM, NEEDS_SYMLINK, NEEDS_UNLINK_OPEN_FILE
20
20
  from tests.conftest import DEAD_HOLDER, STALE_BREAKERS
21
21
  from tests.process_helpers import call_within
22
22
 
@@ -42,11 +42,24 @@ def stale_lock(tmp_path: Path) -> Path:
42
42
  return path
43
43
 
44
44
 
45
+ @NEEDS_STALE_RECLAIM
45
46
  def test_break_lock_file_unlinks_a_dead_holders_marker(stale_lock: Path) -> None:
46
47
  with SoftFileLock(stale_lock, timeout=1):
47
48
  assert list(stale_lock.parent.glob("test.lock.break.*")) == []
48
49
 
49
50
 
51
+ @pytest.mark.skipif(sys.platform == "win32", reason="Windows can reclaim a pinned marker without exchange")
52
+ @pytest.mark.usefixtures("break_route")
53
+ @pytest.mark.parametrize("break_route", ["no-exchange"], indirect=True)
54
+ def test_stale_marker_is_retained_without_exchange(stale_lock: Path) -> None: # pragma: win32 no cover
55
+ with pytest.raises(Timeout):
56
+ SoftFileLock(stale_lock).acquire(blocking=False)
57
+ assert (stale_lock.read_text(encoding="utf-8"), list(stale_lock.parent.glob("test.lock.break.*"))) == (
58
+ DEAD_HOLDER,
59
+ [],
60
+ )
61
+
62
+
50
63
  @pytest.mark.skipif(sys.platform != "win32", reason="oplocks exist only on Windows")
51
64
  def test_break_lock_file_skips_a_marker_under_an_oplock( # pragma: win32 cover
52
65
  stale_lock: Path, hold_oplock: Callable[[Path], Callable[[], None]]
@@ -293,6 +306,7 @@ def test_stale_break_skips_a_marker_swapped_in_after_the_pin( # pragma: win32 n
293
306
 
294
307
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot rename a file held open, so it pins nothing")
295
308
  @STALE_BREAKERS
309
+ @NEEDS_STALE_RECLAIM
296
310
  def test_stale_break_reclaims_a_marker_it_cannot_pin( # pragma: win32 no cover
297
311
  tmp_path: Path, mocker: MockerFixture, lock_type: type[SoftFileLock], record: str
298
312
  ) -> None:
@@ -348,6 +362,7 @@ def test_break_lock_file_lets_acquire_retry_after_the_marker_vanishes(
348
362
  "lifetime",
349
363
  [pytest.param(None, id="stale-take-over"), pytest.param(0.1, id="expiry-placeholder")],
350
364
  )
365
+ @NEEDS_STALE_RECLAIM
351
366
  def test_stale_take_over_keeps_the_lock_when_a_rename_replaces_its_marker( # pragma: win32 no cover
352
367
  stale_lock: Path, mocker: MockerFixture, lifetime: float | None
353
368
  ) -> None:
@@ -378,6 +393,7 @@ def test_stale_take_over_keeps_the_lock_when_a_rename_replaces_its_marker( # pr
378
393
 
379
394
 
380
395
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
396
+ @NEEDS_STALE_RECLAIM
381
397
  def test_remove_own_file_placeholder_names_this_process(
382
398
  tmp_path: Path, mocker: MockerFixture
383
399
  ) -> None: # pragma: win32 no cover
@@ -398,6 +414,7 @@ def test_remove_own_file_placeholder_names_this_process(
398
414
 
399
415
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
400
416
  @STALE_BREAKERS
417
+ @NEEDS_STALE_RECLAIM
401
418
  def test_stale_break_takes_the_lock_over_in_one_poll( # pragma: win32 no cover
402
419
  tmp_path: Path, lock_type: type[SoftFileLock], record: str
403
420
  ) -> None:
@@ -476,6 +493,7 @@ def test_break_lock_file_leaves_the_marker_where_the_exchange_fails( # pragma:
476
493
 
477
494
 
478
495
  @pytest.mark.skipif(sys.platform == "win32", reason="Windows cannot exchange two names")
496
+ @NEEDS_STALE_RECLAIM
479
497
  def test_stale_take_over_interrupted_before_the_check_puts_the_marker_back( # pragma: win32 no cover
480
498
  stale_lock: Path, mocker: MockerFixture
481
499
  ) -> None:
@@ -507,6 +525,7 @@ def test_stale_take_over_interrupted_before_the_check_puts_the_marker_back( # p
507
525
  )
508
526
 
509
527
 
528
+ @NEEDS_STALE_RECLAIM
510
529
  def test_break_lock_file_break_name_is_unguessable(stale_lock: Path, mocker: MockerFixture) -> None:
511
530
  # A second breaker in this process could compute <lock>.break.<pid> and rename a recreated live lock onto it
512
531
  # between our lstat and unlink. Were that our break name, our unlink would delete the live lock.
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