Skip to content

Commit

Permalink
Prevent suicide over old lock.
Browse files Browse the repository at this point in the history
  • Loading branch information
novoselt committed Dec 24, 2014
1 parent 70aeb73 commit 7d28915
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,29 @@ def get_ip_address(ifname):
if pidlock.is_locked():
old_pid = pidlock.read_pid()
logger.info("Lock file exists for PID %d." % old_pid)
try:
old = psutil.Process(old_pid)
if os.path.basename(__file__) in old.cmdline():
try:
logger.info("Trying to terminate old instance...")
old.terminate()
if os.getpid() == old_pid:
logger.info("Stale lock since we have the same PID.")
else:
try:
old = psutil.Process(old_pid)
if os.path.basename(__file__) in old.cmdline():
try:
old.wait(10)
except psutil.TimeoutExpired:
logger.info("Trying to kill old instance.")
old.kill()
except psutil.AccessDenied:
logger.error("The process seems to be SageCell, but "
"can not be stopped. Its command line: %s"
% old.cmdline())
else:
logger.info("Process does not seem to be SageCell.")
except psutil.NoSuchProcess:
pass
logger.info("No such process exist anymore.")
logger.info("Trying to terminate old instance...")
old.terminate()
try:
old.wait(10)
except psutil.TimeoutExpired:
logger.info("Trying to kill old instance.")
old.kill()
except psutil.AccessDenied:
logger.error("The process seems to be SageCell, but "
"can not be stopped. Its command line: %s"
% old.cmdline())
else:
logger.info("Process does not seem to be SageCell.")
except psutil.NoSuchProcess:
pass
logger.info("No such process exist anymore.")
logger.info("Breaking old lock.")
pidlock.break_lock()
try:
Expand Down

0 comments on commit 7d28915

Please sign in to comment.