From c7339ab91454fa1d0bdb9b09dd26e4890c040f70 Mon Sep 17 00:00:00 2001 From: Tomaz Solc Date: Wed, 16 Feb 2022 14:19:28 +0100 Subject: [PATCH 1/4] Use time.monotonic() for calculating timeouts. time.time() is affected by system clock updates (for example, by the NTP daemon). This can cause timeouts to be unpredictable if clock jumps forwards or backwards while code is waiting to acquire a lock. time.monotonic() returns a time value that is not affected by clock updates and does not have this issue. In Python 3.5 and later it is available on all systems[1]. [1] https://docs.python.org/3/library/time.html#time.monotonic --- src/filelock/_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/filelock/_api.py b/src/filelock/_api.py index d3cffc4..282106f 100644 --- a/src/filelock/_api.py +++ b/src/filelock/_api.py @@ -161,7 +161,7 @@ def acquire( lock_id = id(self) lock_filename = self._lock_file - start_time = time.time() + start_time = time.monotonic() try: while True: with self._thread_lock: @@ -172,7 +172,7 @@ def acquire( if self.is_locked: _LOGGER.debug("Lock %s acquired on %s", lock_id, lock_filename) break - elif 0 <= timeout < time.time() - start_time: + elif 0 <= timeout < time.monotonic() - start_time: _LOGGER.debug("Timeout on acquiring lock %s on %s", lock_id, lock_filename) raise Timeout(self._lock_file) else: From e242a048abe4118ed648bfee4f7b9deaf4a351de Mon Sep 17 00:00:00 2001 From: Tomaz Solc Date: Wed, 16 Feb 2022 14:45:31 +0100 Subject: [PATCH 2/4] Add change log entry. --- docs/changelog.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 376ec06..6393fb1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,10 @@ Changelog ========= +v3.5.1 (2022-02-16) +------------------- +- Use time.monotonic() instead of time.time() for calculating timeouts. + v3.5.0 (2022-02-15) ------------------- - Enable use as context decorator From 9b40e505bb1443b74e3d34a0d27cb9309ec60553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 16 Feb 2022 13:54:22 +0000 Subject: [PATCH 3/4] PR feedback --- docs/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6393fb1..a34afd2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,7 +3,7 @@ Changelog v3.5.1 (2022-02-16) ------------------- -- Use time.monotonic() instead of time.time() for calculating timeouts. +- Use `time.monotonic` instead of `time.time` for calculating timeouts. v3.5.0 (2022-02-15) ------------------- From aa230b9d5e33b339ce3edbeae9fbac8c727b53d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 16 Feb 2022 13:54:45 +0000 Subject: [PATCH 4/4] Fix changelog --- docs/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a34afd2..4444add 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,7 +3,7 @@ Changelog v3.5.1 (2022-02-16) ------------------- -- Use `time.monotonic` instead of `time.time` for calculating timeouts. +- Use ``time.monotonic`` instead of ``time.time`` for calculating timeouts. v3.5.0 (2022-02-15) -------------------