提交 1ddcd737 authored 作者: Frederic's avatar Frederic

Do not remove the lock if taken by another process. Remove __del__ as it is not…

Do not remove the lock if taken by another process. Remove __del__ as it is not useful, it is registered atexit
上级 5d93f836
...@@ -128,7 +128,7 @@ def release_lock(): ...@@ -128,7 +128,7 @@ def release_lock():
# Only really release lock once all lock requests have ended. # Only really release lock once all lock requests have ended.
if get_lock.lock_is_enabled and get_lock.n_lock == 0: if get_lock.lock_is_enabled and get_lock.n_lock == 0:
get_lock.start_time = None get_lock.start_time = None
get_lock.unlocker.unlock() get_lock.unlocker.unlock(force=False)
def set_lock_status(use_lock): def set_lock_status(use_lock):
...@@ -244,7 +244,7 @@ def lock(tmp_dir, timeout=notset, min_wait=None, max_wait=None, verbosity=1): ...@@ -244,7 +244,7 @@ def lock(tmp_dir, timeout=notset, min_wait=None, max_wait=None, verbosity=1):
msg = "process '%s'" % read_owner.split('_')[0] msg = "process '%s'" % read_owner.split('_')[0]
_logger.warning("Overriding existing lock by dead %s " _logger.warning("Overriding existing lock by dead %s "
"(I am process '%s')", msg, my_pid) "(I am process '%s')", msg, my_pid)
get_lock.unlocker.unlock() get_lock.unlocker.unlock(force=True)
continue continue
if last_owner == read_owner: if last_owner == read_owner:
if (timeout is not None and if (timeout is not None and
...@@ -257,7 +257,7 @@ def lock(tmp_dir, timeout=notset, min_wait=None, max_wait=None, verbosity=1): ...@@ -257,7 +257,7 @@ def lock(tmp_dir, timeout=notset, min_wait=None, max_wait=None, verbosity=1):
msg = "process '%s'" % read_owner.split('_')[0] msg = "process '%s'" % read_owner.split('_')[0]
_logger.warning("Overriding existing lock by %s " _logger.warning("Overriding existing lock by %s "
"(I am process '%s')", msg, my_pid) "(I am process '%s')", msg, my_pid)
get_lock.unlocker.unlock() get_lock.unlocker.unlock(force=True)
continue continue
else: else:
last_owner = read_owner last_owner = read_owner
...@@ -351,13 +351,7 @@ class Unlocker(object): ...@@ -351,13 +351,7 @@ class Unlocker(object):
def __init__(self, tmp_dir): def __init__(self, tmp_dir):
self.tmp_dir = tmp_dir self.tmp_dir = tmp_dir
def __del__(self): def unlock(self, force=False):
# If we have the lock, we should delete it.
if get_lock.n_lock > 0:
while get_lock.n_lock > 0:
release_lock()
def unlock(self):
"""Remove current lock. """Remove current lock.
This function does not crash if it is unable to properly This function does not crash if it is unable to properly
...@@ -374,8 +368,21 @@ class Unlocker(object): ...@@ -374,8 +368,21 @@ class Unlocker(object):
# not exist), we still want to try and remove the directory. # not exist), we still want to try and remove the directory.
if os is None: if os is None:
return return
# Check if someone else didn't took our lock.
lock_file = os.path.join(self.tmp_dir, 'lock')
if not force:
try:
with open(lock_file) as f:
owner = f.readlines()[0].strip()
pid, _, hname = owner.split('_')
if pid != str(os.getpid()) or hname != hostname:
return
except Exception:
pass
try: try:
os.remove(os.path.join(self.tmp_dir, 'lock')) os.remove(lock_file)
except Exception: except Exception:
pass pass
try: try:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论