Bug 1088192 - mozprocess.ProcessHandlerMixin does not handle missing property "proc" on unstarted processes. r=ahal
authorJulien Pagès <j.parkouss@gmail.com>
Tue, 17 Mar 2015 01:10:00 +0100
changeset 263575 0d976fc6f5822921ae202a9c2454b96a8f8b8197
parent 263574 2da0cd9e724a2088157eb33b51d4f28971909b7a
child 263576 43cc09835a113ccc9494c12fd859701d7c0d545a
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersahal
bugs1088192
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1088192 - mozprocess.ProcessHandlerMixin does not handle missing property "proc" on unstarted processes. r=ahal
testing/mozbase/mozprocess/mozprocess/processhandler.py
testing/mozbase/mozprocess/tests/test_mozprocess_kill.py
testing/mozbase/mozprocess/tests/test_mozprocess_poll.py
--- a/testing/mozbase/mozprocess/mozprocess/processhandler.py
+++ b/testing/mozbase/mozprocess/mozprocess/processhandler.py
@@ -711,43 +711,42 @@ falling back to not using job objects fo
         process, only the root process will be killed.
 
         Note that this does not manage any state, save any output etc,
         it immediately kills the process.
 
         :param sig: Signal used to kill the process, defaults to SIGKILL
                     (has no effect on Windows)
         """
-        try:
-            self.proc.kill(sig=sig)
+        if not hasattr(self, 'proc'):
+            raise RuntimeError("Calling kill() on a non started process is not"
+                               " allowed.")
+        self.proc.kill(sig=sig)
 
-            # When we kill the the managed process we also have to wait for the
-            # reader thread to be finished. Otherwise consumers would have to assume
-            # that it still has not completely shutdown.
-            return self.wait()
-        except AttributeError:
-            # Try to print a relevant error message.
-            if not hasattr(self, 'proc'):
-                print >> sys.stderr, "Unable to kill Process because call to ProcessHandler constructor failed."
-            else:
-                raise
+        # When we kill the the managed process we also have to wait for the
+        # reader thread to be finished. Otherwise consumers would have to assume
+        # that it still has not completely shutdown.
+        return self.wait()
 
     def poll(self):
         """Check if child process has terminated
 
         Returns the current returncode value:
         - None if the process hasn't terminated yet
         - A negative number if the process was killed by signal N (Unix only)
         - '0' if the process ended without failures
 
         """
         # Ensure that we first check for the reader status. Otherwise
         # we might mark the process as finished while output is still getting
         # processed.
-        if self.reader.is_alive():
+        if not hasattr(self, 'proc'):
+            raise RuntimeError("Calling poll() on a non started process is not"
+                               " allowed.")
+        elif self.reader.is_alive():
             return None
         elif hasattr(self.proc, "returncode"):
             return self.proc.returncode
         else:
             return self.proc.poll()
 
     def processOutput(self, timeout=None, outputTimeout=None):
         """
--- a/testing/mozbase/mozprocess/tests/test_mozprocess_kill.py
+++ b/testing/mozbase/mozprocess/tests/test_mozprocess_kill.py
@@ -6,16 +6,22 @@ import unittest
 import proctest
 from mozprocess import processhandler
 
 here = os.path.dirname(os.path.abspath(__file__))
 
 class ProcTestKill(proctest.ProcTest):
     """ Class to test various process tree killing scenatios """
 
+    def test_kill_before_run(self):
+        """Process is not started, and kill() is called"""
+
+        p = processhandler.ProcessHandler([self.python, '-V'])
+        self.assertRaises(RuntimeError, p.kill)
+
     def test_process_kill(self):
         """Process is started, we kill it"""
 
         p = processhandler.ProcessHandler([self.python, self.proclaunch, "process_normal_finish_python.ini"],
                                           cwd=here)
         p.run()
         p.kill()
 
--- a/testing/mozbase/mozprocess/tests/test_mozprocess_poll.py
+++ b/testing/mozbase/mozprocess/tests/test_mozprocess_poll.py
@@ -16,17 +16,17 @@ class ProcTestPoll(proctest.ProcTest):
     """ Class to test process poll """
 
     def test_poll_before_run(self):
         """Process is not started, and poll() is called"""
 
         p = processhandler.ProcessHandler([self.python, self.proclaunch,
                                           "process_normal_finish_python.ini"],
                                           cwd=here)
-        self.assertRaises(AttributeError, p.poll)
+        self.assertRaises(RuntimeError, p.poll)
 
     def test_poll_while_running(self):
         """Process is started, and poll() is called"""
 
         p = processhandler.ProcessHandler([self.python, self.proclaunch,
                                           "process_normal_finish_python.ini"],
                                           cwd=here)
         p.run()
@@ -117,10 +117,11 @@ class ProcTestPoll(proctest.ProcTest):
         self.assertEqual(returncode, p.poll())
 
         detected, output = proctest.check_for_process(self.proclaunch)
         self.determine_status(detected,
                               output,
                               returncode,
                               p.didTimeout)
 
+
 if __name__ == '__main__':
     unittest.main()