skytools.signal_pidfile: ignore empty pidfile, some cleanups
authorMarko Kreen <markokr@gmail.com>
Wed, 7 Dec 2011 10:10:34 +0000 (12:10 +0200)
committerMarko Kreen <markokr@gmail.com>
Wed, 7 Dec 2011 10:10:34 +0000 (12:10 +0200)
Empty pidfile can happen if old process failed to write it
for some reason.

python/skytools/scripting.py

index b0badda9ebd7a355f5aab2d7a01e21aabe03f7c4..583c6464a8411d22e44eab401d286a51930be37f 100644 (file)
@@ -36,10 +36,14 @@ def signal_pidfile(pidfile, sig):
 
     Returns True is successful, False if pidfile does not exist
     or process itself is dead.  Any other errors will passed
-    as exceptions.
-    """
+    as exceptions."""
+
+    ln = ''
     try:
-        pid = int(open(pidfile, 'r').readline())
+        f = open(pidfile, 'r')
+        ln = f.readline().strip()
+        f.close()
+        pid = int(ln)
         os.kill(pid, sig)
         return True
     except IOError, ex:
@@ -49,6 +53,10 @@ def signal_pidfile(pidfile, sig):
         if ex.errno != errno.ESRCH:
             raise
     except ValueError, ex:
+        # this leaves slight race when someone is just creating the file,
+        # but more common case is old empty file.
+        if not ln:
+            return False
         raise ValueError('Corrupt pidfile: %s' % pidfile)
     return False