Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zc.lockfile.LockError handling to locks.file_lock #42

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>`_
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping with the alphabetical order request at L7

* `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