Skip to content

Commit

Permalink
Add zc.lockfile.LockError handling to locks.file_lock
Browse files Browse the repository at this point in the history
Following the fix for #38 by #39, I added a timeout loop and exception handling for the exception raised by zc.lockfile

Add myself to AUTHORS.rst as contributor
Include description of #42 in CHANGES.rst as Unreleased
  • Loading branch information
mshriver committed Oct 29, 2020
1 parent 451debd commit 705bfd8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ These people have contributed to `pytest-services`, in alphabetical order:
* `Dmitrijs Milajevs <dimazest@gmail.com>`_
* `Jason R. Coombs <jaraco@jaraco.com>`_
* `Joep van Dijken <joepvandijken@github.com>`_
* `Magnus Staberg <magnus@staberg.io>`_
* `Michael Shriver <mshriver@redhat.com>`_
* `Oleg Pidsadnyi <oleg.pidsadnyi@gmail.com>`_
* `Zac Hatfield-Dodds <zac@zhd.dev>`_
* `Magnus Staberg <magnus@staberg.io>`_
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Unreleased
-----

- #42: Retry on ``zc.lockfile.LockError`` in ``file_lock``, use existing timeout kwarg

2.2.0
-----

Expand Down
17 changes: 14 additions & 3 deletions pytest_services/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ def file_lock(filename, remove=True, timeout=20):
"""A lock that is shared across processes.
:param filename: the name of the file that will be locked.
:param remove: whether or not to remove the file on context close
:param timeout: Amount of time to retry the file lock if a :class:`zc.lockfile.LockError` is hit
"""
with contextlib.closing(zc.lockfile.SimpleLockFile(filename)) as lockfile:
yield lockfile._fp
total_seconds_slept = 0
while True:
try:
with contextlib.closing(zc.lockfile.SimpleLockFile(filename)) as lockfile:
yield lockfile._fp
break
except zc.lockfile.LockError as err:
if total_seconds_slept >= timeout:
raise err
seconds_to_sleep = random() * 0.1 + 0.05
total_seconds_slept += seconds_to_sleep
time.sleep(seconds_to_sleep)

remove and try_remove(filename)

Expand Down

0 comments on commit 705bfd8

Please sign in to comment.