diff options
author | Ben Brown <ben.brown@codethink.co.uk> | 2017-09-26 16:14:30 +0000 |
---|---|---|
committer | Ben Brown <ben.brown@codethink.co.uk> | 2017-09-26 16:14:30 +0000 |
commit | 4e8c46dc37a1f78c204969d889f16904e20d5399 (patch) | |
tree | 1306049482661f8c7a4b7e74d2f24ecc6c13c94b | |
parent | 4419d6600008cb04e9887afb9d01c91a45c9313e (diff) | |
parent | cd3ae9722cf9174a566bb2bbe1447a2b2402709b (diff) | |
download | ybd-4e8c46dc37a1f78c204969d889f16904e20d5399.tar.gz |
Merge branch 'benbrown/lockfiles' into 'master'
Fix file locking errors with higher number of instances
See merge request baserock/ybd!371
-rw-r--r-- | ybd/app.py | 12 | ||||
-rw-r--r-- | ybd/assembly.py | 6 |
2 files changed, 12 insertions, 6 deletions
@@ -41,10 +41,14 @@ class RetryException(Exception): if config.get('last-retry-time'): wait = datetime.datetime.now() - config.get('last-retry-time') if wait.seconds < 1 and os.path.exists(lockfile(dn)): - with open(lockfile(dn), 'r') as L: - call(['flock', '--shared', '--timeout', - config.get('timeout', '60'), str(L.fileno())]) - log(dn, 'Finished wait loop', verbose=True) + try: + with open(lockfile(dn), 'r') as L: + call(['flock', '--shared', '--timeout', + config.get('timeout', '60'), str(L.fileno())]) + log(dn, 'Finished wait loop', verbose=True) + except IOError: + # The process that had the lock is finished with it. + pass config['last-retry-time'] = datetime.datetime.now() config['last-retry-dn'] = dn for dirname in config['sandboxes']: diff --git a/ybd/assembly.py b/ybd/assembly.py index 9699d6e..1ea9664 100644 --- a/ybd/assembly.py +++ b/ybd/assembly.py @@ -210,9 +210,11 @@ def shuffle(contents): @contextlib.contextmanager def claim(dn): with open(lockfile(dn), 'a') as L: + locked = False try: fcntl.flock(L, fcntl.LOCK_EX | fcntl.LOCK_NB) - except Exception as e: + locked = True + except IOError as e: if e.errno in (errno.EACCES, errno.EAGAIN): # flock() will report EACCESS or EAGAIN when the lock fails. raise RetryException(dn) @@ -224,7 +226,7 @@ def claim(dn): try: yield finally: - if os.path.isfile(lockfile(dn)): + if locked and os.path.isfile(lockfile(dn)): os.remove(lockfile(dn)) |