Skip to content

Commit a7b2c2e

Browse files
authored
Add zc.lockfile.LockError handling to locks.file_lock (#42)
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
1 parent 451debd commit a7b2c2e

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

AUTHORS.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ These people have contributed to `pytest-services`, in alphabetical order:
1010
* `Dmitrijs Milajevs <[email protected]>`_
1111
* `Jason R. Coombs <[email protected]>`_
1212
* `Joep van Dijken <[email protected]>`_
13+
* `Magnus Staberg <[email protected]>`_
14+
* `Michael Shriver <[email protected]>`_
1315
* `Oleg Pidsadnyi <[email protected]>`_
1416
* `Zac Hatfield-Dodds <[email protected]>`_
15-
* `Magnus Staberg <[email protected]>`_

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
Unreleased
5+
-----
6+
7+
- #42: Retry on ``zc.lockfile.LockError`` in ``file_lock``, use existing timeout kwarg
8+
49
2.2.0
510
-----
611

pytest_services/locks.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ def file_lock(filename, remove=True, timeout=20):
2424
"""A lock that is shared across processes.
2525
2626
:param filename: the name of the file that will be locked.
27-
27+
:param remove: whether or not to remove the file on context close
28+
:param timeout: Amount of time to retry the file lock if a :class:`zc.lockfile.LockError` is hit
2829
"""
29-
with contextlib.closing(zc.lockfile.SimpleLockFile(filename)) as lockfile:
30-
yield lockfile._fp
30+
total_seconds_slept = 0
31+
while True:
32+
try:
33+
with contextlib.closing(zc.lockfile.SimpleLockFile(filename)) as lockfile:
34+
yield lockfile._fp
35+
break
36+
except zc.lockfile.LockError as err:
37+
if total_seconds_slept >= timeout:
38+
raise err
39+
seconds_to_sleep = random() * 0.1 + 0.05
40+
total_seconds_slept += seconds_to_sleep
41+
time.sleep(seconds_to_sleep)
3142

3243
remove and try_remove(filename)
3344

0 commit comments

Comments
 (0)