Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/filelock/_api.py: 69%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import contextlib
4import inspect
5import logging
6import os
7import sys
8import time
9import warnings
10from abc import ABCMeta, abstractmethod
11from dataclasses import dataclass
12from threading import Lock, local
13from typing import TYPE_CHECKING, Any, TypeVar
14from weakref import WeakValueDictionary
16from ._error import Timeout
17from ._util import break_lock_file
19#: Sentinel indicating that no explicit file permission mode was passed.
20#: When used, lock files are created with 0o666 (letting umask and default ACLs control the final permissions)
21#: and fchmod is skipped so that POSIX default ACL inheritance is preserved.
22_UNSET_FILE_MODE: int = -1
24if TYPE_CHECKING:
25 from collections.abc import Callable
26 from types import TracebackType
28 from ._read_write import ReadWriteLock
29 from ._soft_rw import SoftReadWriteLock
31 if sys.version_info >= (3, 11): # pragma: no cover (py311+)
32 from typing import Self
33 else: # pragma: no cover (<py311)
34 from typing_extensions import Self
37_LOGGER = logging.getLogger("filelock")
39# On Windows os.path.realpath calls CreateFileW with share_mode=0, which blocks concurrent DeleteFileW and causes
40# livelocks under threaded contention with SoftFileLock. os.path.abspath is purely string-based and avoids this.
41_canonical = os.path.abspath if sys.platform == "win32" else os.path.realpath
44class _ThreadLocalRegistry(local):
45 def __init__(self) -> None:
46 super().__init__()
47 self.held: dict[str, int] = {}
50_registry = _ThreadLocalRegistry()
53# This is a helper class which is returned by :meth:`BaseFileLock.acquire` and wraps the lock to make sure __enter__
54# is not called twice when entering the with statement. If we would simply return *self*, the lock would be acquired
55# again in the *__enter__* method of the BaseFileLock, but not released again automatically. issue #37 (memory leak)
56class AcquireReturnProxy:
57 """A context-aware object that will release the lock file when exiting."""
59 def __init__(self, lock: BaseFileLock | ReadWriteLock | SoftReadWriteLock) -> None:
60 self.lock: BaseFileLock | ReadWriteLock | SoftReadWriteLock = lock
62 def __enter__(self) -> BaseFileLock | ReadWriteLock | SoftReadWriteLock:
63 return self.lock
65 def __exit__(
66 self,
67 exc_type: type[BaseException] | None,
68 exc_value: BaseException | None,
69 traceback: TracebackType | None,
70 ) -> None:
71 self.lock.release()
74@dataclass
75class FileLockContext:
76 """A dataclass which holds the context for a ``BaseFileLock`` object."""
78 # The context is held in a separate class to allow optional use of thread local storage via the
79 # ThreadLocalFileContext class.
81 #: The path to the lock file.
82 lock_file: str
84 #: The default timeout value.
85 timeout: float
87 #: The mode for the lock files
88 mode: int
90 #: Whether the lock should be blocking or not
91 blocking: bool
93 #: The default polling interval value.
94 poll_interval: float
96 #: The lock lifetime in seconds; ``None`` means the lock never expires.
97 lifetime: float | None = None
99 #: The file descriptor for the *_lock_file* as it is returned by the os.open() function, not None when lock held
100 lock_file_fd: int | None = None
102 #: The lock counter is used for implementing the nested locking mechanism.
103 lock_counter: int = 0 # When the lock is acquired is increased and the lock is only released, when this value is 0
106class ThreadLocalFileContext(FileLockContext, local):
107 """A thread local version of the ``FileLockContext`` class."""
110_T = TypeVar("_T", bound="BaseFileLock")
113class FileLockMeta(ABCMeta):
114 _instances: WeakValueDictionary[str, BaseFileLock]
115 _instances_lock: Lock
117 def __call__( # noqa: PLR0913
118 cls: type[_T],
119 lock_file: str | os.PathLike[str],
120 timeout: float = -1,
121 mode: int = _UNSET_FILE_MODE,
122 thread_local: bool = True, # noqa: FBT001, FBT002
123 *,
124 blocking: bool = True,
125 is_singleton: bool = False,
126 poll_interval: float = 0.05,
127 lifetime: float | None = None,
128 **kwargs: Any, # capture remaining kwargs for subclasses # noqa: ANN401
129 ) -> _T:
130 params = {
131 "timeout": timeout,
132 "mode": mode,
133 "thread_local": thread_local,
134 "blocking": blocking,
135 "is_singleton": is_singleton,
136 "poll_interval": poll_interval,
137 "lifetime": lifetime,
138 **kwargs,
139 }
140 if not is_singleton:
141 return cls._create_instance(lock_file, params)
143 # Look up, build and store under one lock. Without it two threads racing the first construction for a
144 # path both miss the cache and each build their own instance, so callers relying on is_singleton for
145 # reentrant locking across instances end up with two "singletons" and acquire()'s deadlock check then
146 # rejects a legitimate reentrant acquire; the unguarded writes to the WeakValueDictionary are a data
147 # race besides. ReadWriteLock and SoftReadWriteLock already guard their singleton caches this way.
148 with cls._instances_lock:
149 if (instance := cls._instances.get(str(lock_file))) is None:
150 instance = cls._create_instance(lock_file, params)
151 cls._instances[str(lock_file)] = instance
152 return instance
154 params_to_check = {
155 "thread_local": (thread_local, instance.is_thread_local()),
156 "timeout": (timeout, instance.timeout),
157 "mode": (mode, instance._context.mode), # noqa: SLF001
158 "blocking": (blocking, instance.blocking),
159 "poll_interval": (poll_interval, instance.poll_interval),
160 "lifetime": (lifetime, instance.lifetime),
161 }
163 non_matching_params = {
164 name: (passed_param, set_param)
165 for name, (passed_param, set_param) in params_to_check.items()
166 if passed_param != set_param
167 }
168 if not non_matching_params:
169 return instance # ty: ignore[invalid-return-type] # https://github.com/astral-sh/ty/issues/3231
171 # parameters do not match; raise error
172 msg = "Singleton lock instances cannot be initialized with differing arguments"
173 msg += "\nNon-matching arguments: "
174 for param_name, (passed_param, set_param) in non_matching_params.items():
175 msg += f"\n\t{param_name} (existing lock has {set_param} but {passed_param} was passed)"
176 raise ValueError(msg)
178 def _create_instance(cls: type[_T], lock_file: str | os.PathLike[str], params: dict[str, Any]) -> _T:
179 # Keep only the params this subclass's __init__ accepts: virtualenv narrows the signature of its
180 # BaseFileLock descendant, so passing the full set would break it (https://github.com/tox-dev/filelock/pull/340).
181 present_params = inspect.signature(cls.__init__).parameters
182 init_params = {key: value for key, value in params.items() if key in present_params}
183 return super().__call__(lock_file, **init_params)
186class BaseFileLock(contextlib.ContextDecorator, metaclass=FileLockMeta):
187 """
188 Abstract base class for a file lock object.
190 Provides a reentrant, cross-process exclusive lock backed by OS-level primitives. Subclasses implement the actual
191 locking mechanism (:class:`UnixFileLock <filelock.UnixFileLock>`, :class:`WindowsFileLock
192 <filelock.WindowsFileLock>`, :class:`SoftFileLock <filelock.SoftFileLock>`).
194 """
196 _instances: WeakValueDictionary[str, BaseFileLock]
197 _instances_lock: Lock
199 #: How the cross-instance deadlock message names the conflicting holder; the async subclass says "task".
200 _deadlock_holder_desc: str = "FileLock instance in this thread"
202 def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
203 """Setup unique state for lock subclasses."""
204 super().__init_subclass__(**kwargs)
205 cls._instances = WeakValueDictionary()
206 cls._instances_lock = Lock()
208 def __init__( # noqa: PLR0913
209 self,
210 lock_file: str | os.PathLike[str],
211 timeout: float = -1,
212 mode: int = _UNSET_FILE_MODE,
213 thread_local: bool = True, # noqa: FBT001, FBT002
214 *,
215 blocking: bool = True,
216 is_singleton: bool = False,
217 poll_interval: float = 0.05,
218 lifetime: float | None = None,
219 ) -> None:
220 """
221 Create a new lock object.
223 :param lock_file: path to the file
224 :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in the
225 acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it to a
226 negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.
227 :param mode: file permissions for the lockfile. When not specified, the OS controls permissions via umask and
228 default ACLs, preserving POSIX default ACL inheritance in shared directories.
229 :param thread_local: Whether this object's internal context should be thread local or not. If this is set to
230 ``False`` then the lock will be reentrant across threads. When ``True`` (the default), **all fields of the
231 lock's internal context are per-thread**, including the configuration values ``poll_interval``, ``timeout``,
232 ``blocking``, ``mode``, and ``lifetime``. Setting one of these properties from one thread does not change
233 the value seen by another thread; threads that did not perform the write continue to see the value supplied
234 at construction time. If you need configuration values to be visible across threads, construct the lock
235 with ``thread_local=False``.
236 :param blocking: whether the lock should be blocking or not
237 :param is_singleton: If this is set to ``True`` then only one instance of this class will be created per lock
238 file. This is useful if you want to use the lock object for reentrant locking without needing to pass the
239 same object around.
240 :param poll_interval: default interval for polling the lock file, in seconds. It will be used as fallback value
241 in the acquire method, if no poll_interval value (``None``) is given.
242 :param lifetime: maximum time in seconds a lock can be held before it is considered expired. When set, a waiting
243 process will break a lock whose file modification time is older than ``lifetime`` seconds. ``None`` (the
244 default) means locks never expire.
246 """
247 self._is_thread_local = thread_local
248 self._is_singleton = is_singleton
250 # Create the context. Note that external code should not work with the context directly and should instead use
251 # properties of this class.
252 kwargs: dict[str, Any] = {
253 "lock_file": os.fspath(lock_file),
254 "timeout": timeout,
255 "mode": mode,
256 "blocking": blocking,
257 "poll_interval": poll_interval,
258 "lifetime": lifetime,
259 }
260 self._context: FileLockContext = (ThreadLocalFileContext if thread_local else FileLockContext)(**kwargs)
262 def is_thread_local(self) -> bool:
263 """:returns: a flag indicating if this lock is thread local or not"""
264 return self._is_thread_local
266 @property
267 def is_singleton(self) -> bool:
268 """
269 A flag indicating if this lock is singleton or not.
271 .. versionadded:: 3.13.0
273 """
274 return self._is_singleton
276 @property
277 def lock_file(self) -> str:
278 """Path to the lock file."""
279 return self._context.lock_file
281 @property
282 def timeout(self) -> float:
283 """
284 The default timeout value, in seconds.
286 .. versionadded:: 2.0.0
288 """
289 return self._context.timeout
291 @timeout.setter
292 def timeout(self, value: float | str) -> None:
293 """
294 Change the default timeout value.
296 :param value: the new value, in seconds
298 """
299 self._context.timeout = float(value)
301 @property
302 def blocking(self) -> bool:
303 """
304 Whether the locking is blocking or not.
306 .. versionadded:: 3.14.0
308 """
309 return self._context.blocking
311 @blocking.setter
312 def blocking(self, value: bool) -> None:
313 """
314 Change the default blocking value.
316 :param value: the new value as bool
318 """
319 self._context.blocking = value
321 @property
322 def poll_interval(self) -> float:
323 """
324 The default polling interval, in seconds.
326 .. versionadded:: 3.24.0
328 """
329 return self._context.poll_interval
331 @poll_interval.setter
332 def poll_interval(self, value: float) -> None:
333 """
334 Change the default polling interval.
336 :param value: the new value, in seconds
338 """
339 self._context.poll_interval = value
341 @property
342 def lifetime(self) -> float | None:
343 """
344 The lock lifetime in seconds, or ``None`` if the lock never expires.
346 .. versionadded:: 3.24.0
348 """
349 return self._context.lifetime
351 @lifetime.setter
352 def lifetime(self, value: float | None) -> None:
353 """
354 Change the lock lifetime.
356 :param value: the new value in seconds, or ``None`` to disable expiration
358 :raises ValueError: if *value* is a negative number
359 :raises TypeError: if *value* is not ``None`` and not a real number
361 """
362 if value is not None:
363 if isinstance(value, bool) or not isinstance(value, (int, float)):
364 msg = f"lifetime must be a non-negative number or None, not {type(value).__name__}"
365 raise TypeError(msg)
366 if value < 0:
367 msg = f"lifetime must be non-negative, not {value!r}"
368 raise ValueError(msg)
369 self._context.lifetime = value
371 @property
372 def mode(self) -> int:
373 """The file permissions for the lockfile."""
374 return 0o644 if self._context.mode == _UNSET_FILE_MODE else self._context.mode
376 @property
377 def has_explicit_mode(self) -> bool:
378 """Whether the file permissions were explicitly set."""
379 return self._context.mode != _UNSET_FILE_MODE
381 def _open_mode(self) -> int:
382 """:returns: the mode for os.open() — 0o666 when unset (let umask/ACLs decide), else the explicit mode"""
383 return 0o666 if self._context.mode == _UNSET_FILE_MODE else self._context.mode
385 def _try_break_expired_lock(self) -> None:
386 """Remove the lock file if its modification time exceeds the configured :attr:`lifetime`."""
387 if (lifetime := self._context.lifetime) is None:
388 return
389 with contextlib.suppress(OSError):
390 # lstat, not stat: an attacker with write access to the lock directory can replace a held
391 # lock file with a symlink pointing at an old file, making stat() report the target's stale
392 # mtime so a waiter breaks a live lock and two processes hold it at once. lstat reads the
393 # symlink's own mtime, matching the O_NOFOLLOW reads elsewhere.
394 st = os.lstat(self.lock_file)
395 if time.time() - st.st_mtime < lifetime:
396 return
397 break_lock_file(self.lock_file, st.st_mtime, st.st_ino)
399 @abstractmethod
400 def _acquire(self) -> None:
401 """If the file lock could be acquired, self._context.lock_file_fd holds the file descriptor of the lock file."""
402 raise NotImplementedError
404 @abstractmethod
405 def _release(self) -> None:
406 """Releases the lock and sets self._context.lock_file_fd to None."""
407 raise NotImplementedError
409 @property
410 def is_locked(self) -> bool:
411 """
412 A boolean indicating if the lock file is holding the lock currently.
414 .. versionchanged:: 2.0.0
416 This was previously a method and is now a property.
418 """
419 return self._context.lock_file_fd is not None
421 @property
422 def lock_counter(self) -> int:
423 """The number of times this lock has been acquired (but not yet released)."""
424 return self._context.lock_counter
426 @staticmethod
427 def _check_give_up( # noqa: PLR0913
428 lock_id: int,
429 lock_filename: str,
430 *,
431 blocking: bool,
432 cancel_check: Callable[[], bool] | None,
433 timeout: float,
434 start_time: float,
435 ) -> bool:
436 if blocking is False:
437 _LOGGER.debug("Failed to immediately acquire lock %s on %s", lock_id, lock_filename)
438 return True
439 if cancel_check is not None and cancel_check():
440 _LOGGER.debug("Cancellation requested for lock %s on %s", lock_id, lock_filename)
441 return True
442 if 0 <= timeout < time.perf_counter() - start_time:
443 _LOGGER.debug("Timeout on acquiring lock %s on %s", lock_id, lock_filename)
444 return True
445 return False
447 def acquire(
448 self,
449 timeout: float | None = None,
450 poll_interval: float | None = None,
451 *,
452 poll_intervall: float | None = None,
453 blocking: bool | None = None,
454 cancel_check: Callable[[], bool] | None = None,
455 ) -> AcquireReturnProxy:
456 """
457 Try to acquire the file lock.
459 :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and
460 if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired
461 :param poll_interval: interval of trying to acquire the lock file, ``None`` means use the default
462 :attr:`~poll_interval`
463 :param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead
464 :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the
465 first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.
466 :param cancel_check: a callable returning ``True`` when the acquisition should be canceled. Checked on each poll
467 iteration. When triggered, raises :class:`~Timeout` just like an expired timeout.
469 :returns: a context object that will unlock the file when the context is exited
471 :raises Timeout: if fails to acquire lock within the timeout period
473 .. code-block:: python
475 # You can use this method in the context manager (recommended)
476 with lock.acquire():
477 pass
479 # Or use an equivalent try-finally construct:
480 lock.acquire()
481 try:
482 pass
483 finally:
484 lock.release()
486 .. versionchanged:: 2.0.0
488 This method returns now a *proxy* object instead of *self*, so that it can be used in a with statement
489 without side effects.
491 """
492 # Use the default timeout, if no timeout is provided.
493 if timeout is None:
494 timeout = self._context.timeout
496 if blocking is None:
497 blocking = self._context.blocking
499 if poll_intervall is not None:
500 msg = "use poll_interval instead of poll_intervall"
501 warnings.warn(msg, DeprecationWarning, stacklevel=2)
502 poll_interval = poll_intervall
504 poll_interval = poll_interval if poll_interval is not None else self._context.poll_interval
506 # Increment the number right at the beginning. We can still undo it, if something fails.
507 self._context.lock_counter += 1
509 canonical = _canonical(self.lock_file)
510 self._raise_if_would_deadlock(canonical, timeout=timeout, blocking=blocking)
512 start_time = time.perf_counter()
513 try:
514 self._poll_until_acquired(
515 blocking=blocking,
516 cancel_check=cancel_check,
517 timeout=timeout,
518 poll_interval=poll_interval,
519 start_time=start_time,
520 )
521 except BaseException:
522 self._undo_acquire(canonical)
523 raise
524 self._commit_acquire(canonical)
525 return AcquireReturnProxy(lock=self)
527 def _raise_if_would_deadlock(self, canonical: str, *, timeout: float, blocking: bool) -> None:
528 """
529 Fail fast when a *different* live instance already holds this path on the current thread/task.
531 Only the first, indefinitely-blocking acquire can self-deadlock this way: waiting in the OS primitive would
532 block on a lock this thread already owns. A finite timeout or ``blocking=False`` keeps the normal Timeout path.
533 """
534 would_block = self._context.lock_counter == 1 and not self.is_locked and timeout < 0 and blocking
535 if would_block and _registry.held.get(canonical) not in {None, id(self)}:
536 self._context.lock_counter -= 1
537 msg = (
538 f"Deadlock: lock '{self.lock_file}' is already held by a different {self._deadlock_holder_desc}. "
539 f"Use is_singleton=True to enable reentrant locking across instances."
540 )
541 raise RuntimeError(msg)
543 def _undo_acquire(self, canonical: str) -> None:
544 """Roll back the counter after a failed acquire, dropping the registry entry once nothing holds the path."""
545 self._context.lock_counter = max(0, self._context.lock_counter - 1)
546 if self._context.lock_counter == 0:
547 _registry.held.pop(canonical, None)
549 def _commit_acquire(self, canonical: str) -> None:
550 """Record this instance as the holder once the first acquire succeeds, so peers can detect the deadlock."""
551 if self._context.lock_counter == 1:
552 _registry.held[canonical] = id(self)
554 def _drop_registry_entry(self) -> None:
555 """Forget this path's holder on release so a later cross-instance acquire is not misread as a deadlock."""
556 _registry.held.pop(_canonical(self.lock_file), None)
558 def _poll_until_acquired(
559 self,
560 *,
561 blocking: bool,
562 cancel_check: Callable[[], bool] | None,
563 timeout: float,
564 poll_interval: float,
565 start_time: float,
566 ) -> None:
567 lock_id = id(self)
568 lock_filename = self.lock_file
569 while True:
570 if not self.is_locked:
571 self._try_break_expired_lock()
572 _LOGGER.debug("Attempting to acquire lock %s on %s", lock_id, lock_filename)
573 self._acquire()
574 if self.is_locked:
575 _LOGGER.debug("Lock %s acquired on %s", lock_id, lock_filename)
576 return
577 if self._check_give_up(
578 lock_id,
579 lock_filename,
580 blocking=blocking,
581 cancel_check=cancel_check,
582 timeout=timeout,
583 start_time=start_time,
584 ):
585 raise Timeout(lock_filename)
586 msg = "Lock %s not acquired on %s, waiting %s seconds ..."
587 _LOGGER.debug(msg, lock_id, lock_filename, poll_interval)
588 time.sleep(poll_interval)
590 def release(self, force: bool = False) -> None: # noqa: FBT001, FBT002
591 """
592 Release the file lock. The lock is only completely released when the lock counter reaches 0. The lock file
593 itself may be deleted automatically, the behavior is platform-specific.
595 :param force: If true, the lock counter is ignored and the lock is released in every case.
597 """
598 if self.is_locked:
599 self._context.lock_counter -= 1
601 if self._context.lock_counter == 0 or force:
602 lock_id, lock_filename = id(self), self.lock_file
604 _LOGGER.debug("Attempting to release lock %s on %s", lock_id, lock_filename)
605 self._release()
606 self._context.lock_counter = 0
607 self._drop_registry_entry()
608 _LOGGER.debug("Lock %s released on %s", lock_id, lock_filename)
610 def __enter__(self) -> Self:
611 """
612 Acquire the lock.
614 :returns: the lock object
616 """
617 self.acquire()
618 return self
620 def __exit__(
621 self,
622 exc_type: type[BaseException] | None,
623 exc_value: BaseException | None,
624 traceback: TracebackType | None,
625 ) -> None:
626 """
627 Release the lock.
629 :param exc_type: the exception type if raised
630 :param exc_value: the exception value if raised
631 :param traceback: the exception traceback if raised
633 """
634 self.release()
636 def __del__(self) -> None:
637 """Called when the lock object is deleted."""
638 self.release(force=True)
641__all__ = [
642 "_UNSET_FILE_MODE",
643 "AcquireReturnProxy",
644 "BaseFileLock",
645]