Skip to content

Commit 17af1f6

Browse files
committed
BlockingLockFile: added sanity check that raises IOError if the directory containing the lock was removed. This is unlikely to happen in a production envrironment, but may happen during testing, as folders are moved/deleted once the test is complete. Daemons might still be waiting for something, and they should be allowed to terminate instead of waiting for a possibly long time
1 parent 1019d4c commit 17af1f6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Diff for: lib/git/utils.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ def _release_lock(self):
177177

178178
class BlockingLockFile(LockFile):
179179
"""The lock file will block until a lock could be obtained, or fail after
180-
a specified timeout"""
180+
a specified timeout.
181+
182+
:note: If the directory containing the lock was removed, an exception will
183+
be raised during the blocking period, preventing hangs as the lock
184+
can never be obtained."""
181185
__slots__ = ("_check_interval", "_max_block_time")
182186
def __init__(self, file_path, check_interval_s=0.3, max_block_time_s=sys.maxint):
183187
"""Configure the instance
@@ -195,17 +199,24 @@ def __init__(self, file_path, check_interval_s=0.3, max_block_time_s=sys.maxint)
195199

196200
def _obtain_lock(self):
197201
"""This method blocks until it obtained the lock, or raises IOError if
198-
it ran out of time.
202+
it ran out of time or if the parent directory was not available anymore.
199203
If this method returns, you are guranteed to own the lock"""
200204
starttime = time.time()
201205
maxtime = starttime + float(self._max_block_time)
202206
while True:
203207
try:
204208
super(BlockingLockFile, self)._obtain_lock()
205209
except IOError:
206-
curtime = time.time()
210+
# synity check: if the directory leading to the lockfile is not
211+
# readable anymore, raise an execption
212+
curtime = time.time()
213+
if not os.path.isdir(os.path.dirname(self._lock_file_path())):
214+
msg = "Directory containing the lockfile %r was not readable anymore after waiting %g seconds" % (self._lock_file_path(), curtime - starttime)
215+
raise IOError(msg)
216+
# END handle missing directory
217+
207218
if curtime >= maxtime:
208-
msg = "Waited %f seconds for lock at %r" % ( maxtime - starttime, self._lock_file_path())
219+
msg = "Waited %g seconds for lock at %r" % ( maxtime - starttime, self._lock_file_path())
209220
raise IOError(msg)
210221
# END abort if we wait too long
211222
time.sleep(self._check_interval)

0 commit comments

Comments
 (0)