bug 522804 - fix two more JarMaker lockFile race conditions on Windows. r=pike

This commit is contained in:
Ted Mielczarek 2009-11-24 07:11:25 -05:00
parent c6514e06d1
commit 1ccefba1bf

View file

@ -53,8 +53,19 @@ class LockFile(object):
def __init__(self, lockfile): def __init__(self, lockfile):
self.lockfile = lockfile self.lockfile = lockfile
def __del__(self): def __del__(self):
while True:
try:
os.remove(self.lockfile) os.remove(self.lockfile)
break
except OSError, e:
if e.errno == errno.EACCES:
# another process probably has the file open, we'll retry.
# just a short sleep since we want to drop the lock ASAP
# (but we need to let some other process close the file first)
time.sleep(0.1)
else:
# re-raise unknown errors
raise
def lockFile(lockfile, max_wait = 600): def lockFile(lockfile, max_wait = 600):
'''Create and hold a lockfile of the given name, with the given timeout. '''Create and hold a lockfile of the given name, with the given timeout.
@ -80,18 +91,19 @@ def lockFile(lockfile, max_wait = 600):
f = open(lockfile, "r") f = open(lockfile, "r")
s = os.stat(lockfile) s = os.stat(lockfile)
except EnvironmentError, e: except EnvironmentError, e:
if e.errno != errno.ENOENT: if e.errno == errno.ENOENT or \
sys.exit("%s exists but stat() failed: %s" % (sys.platform == "win32" and e.errno == errno.EACCES):
(lockfile, e.strerror))
# we didn't create the lockfile, so it did exist, but it's # we didn't create the lockfile, so it did exist, but it's
# gone now. Just try again # gone now. Just try again
continue continue
sys.exit("%s exists but stat() failed: %s" %
(lockfile, e.strerror))
# we didn't create the lockfile and it's still there, check # we didn't create the lockfile and it's still there, check
# its age # its age
now = int(time.time()) now = int(time.time())
if now - s[stat.ST_MTIME] > max_wait: if now - s[stat.ST_MTIME] > max_wait:
pid = f.readline() pid = f.readline().rstrip()
sys.exit("%s has been locked for more than " \ sys.exit("%s has been locked for more than " \
"%d seconds (PID %s)" % (lockfile, max_wait, "%d seconds (PID %s)" % (lockfile, max_wait,
pid)) pid))