[nrwt] handle corrupt http server pid files cleanly
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / servers / http_server_base.py
index 432c72e..3ce15a5 100644 (file)
@@ -75,8 +75,12 @@ class HttpServerBase(object):
 
         # Stop any stale servers left over from previous instances.
         if self._filesystem.exists(self._pid_file):
-            self._pid = int(self._filesystem.read_text_file(self._pid_file))
-            self._stop_running_server()
+            try:
+                self._pid = int(self._filesystem.read_text_file(self._pid_file))
+                self._stop_running_server()
+            except (ValueError, UnicodeDecodeError):
+                # These could be raised if the pid file is corrupt.
+                self._remove_pid_file()
             self._pid = None
 
         self._remove_stale_logs()
@@ -94,29 +98,37 @@ class HttpServerBase(object):
     def stop(self):
         """Stops the server. Stopping a server that isn't started is harmless."""
         actual_pid = None
-        if self._filesystem.exists(self._pid_file):
-            actual_pid = int(self._filesystem.read_text_file(self._pid_file))
+        try:
+            if self._filesystem.exists(self._pid_file):
+                try:
+                    actual_pid = int(self._filesystem.read_text_file(self._pid_file))
+                except (ValueError, UnicodeDecodeError):
+                    # These could be raised if the pid file is corrupt.
+                    pass
+                if not self._pid:
+                    self._pid = actual_pid
+
             if not self._pid:
-                self._pid = actual_pid
-
-        if not self._pid:
-            return
-
-        if not actual_pid:
-            _log.warning('Failed to stop %s: pid file is missing' % self._name)
-            return
-        if self._pid != actual_pid:
-            _log.warning('Failed to stop %s: pid file contains %d, not %d' %
-                         (self._name, actual_pid, self._pid))
-            # Try to kill the existing pid, anyway, in case it got orphaned.
-            self._executive.kill_process(self._pid)
+                return
+
+            if not actual_pid:
+                _log.warning('Failed to stop %s: pid file is missing' % self._name)
+                return
+            if self._pid != actual_pid:
+                _log.warning('Failed to stop %s: pid file contains %d, not %d' %
+                            (self._name, actual_pid, self._pid))
+                # Try to kill the existing pid, anyway, in case it got orphaned.
+                self._executive.kill_process(self._pid)
+                self._pid = None
+                return
+
+            _log.debug("Attempting to shut down %s server at pid %d" % (self._name, self._pid))
+            self._stop_running_server()
+            _log.debug("%s server at pid %d stopped" % (self._name, self._pid))
             self._pid = None
-            return
-
-        _log.debug("Attempting to shut down %s server at pid %d" % (self._name, self._pid))
-        self._stop_running_server()
-        _log.debug("%s server at pid %d stopped" % (self._name, self._pid))
-        self._pid = None
+        finally:
+            # Make sure we delete the pid file no matter what happens.
+            self._remove_pid_file()
 
     def _prepare_config(self):
         """This routine can be overridden by subclasses to do any sort
@@ -143,6 +155,10 @@ class HttpServerBase(object):
 
     # Utility routines.
 
+    def _remove_pid_file(self):
+        if self._filesystem.exists(self._pid_file):
+            self._filesystem.remove(self._pid_file)
+
     def _remove_log_files(self, folder, starts_with):
         files = self._filesystem.listdir(folder)
         for file in files: