From 5680d806d26e10ee5463558dcdfca0bb533dfbe0 Mon Sep 17 00:00:00 2001 From: Stefan Hacker Date: Wed, 24 Jun 2015 05:48:21 +0200 Subject: [PATCH] Add basic stale pid detection heuristic While any normal crashes should already remove the pid file if one was created mumo can still be killed in ways which make it unable to clean up after itself. This patch adds a basic heuristic which checks if the pid mentioned in the pid file belongs to a running process. If so we assume this is actually mumo and terminate. If not we assume mumo isn't running and break the lock. This approach is not 100% reliable and it break the acquire timeout properties we had before but should do the trick. --- mumo.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mumo.py b/mumo.py index 573f015..6824f83 100755 --- a/mumo.py +++ b/mumo.py @@ -542,6 +542,15 @@ def error(self, message): ret = do_main_program() else: pidfile = daemon.pidlockfile.TimeoutPIDLockFile(cfg.system.pidfile, 5) + if pidfile.is_locked(): + try: + os.kill(pidfile.read_pid(), 0) + print >> sys.stderr, 'Mumo already running as %s' % pidfile.read_pid() + sys.exit(1) + except OSError: + print >> sys.stderr, 'Found stale mumo pid file but no process, breaking lock' + pidfile.break_lock() + context = daemon.DaemonContext(working_directory=sys.path[0], stderr=logfile, pidfile=pidfile)