Bug 1538677 - [mitmproxy] Allow mitmproxy commands at maximum 30s of runtime. r=rwood,tarek
authorHenrik Skupin <mail@hskupin.info>
Mon, 25 Mar 2019 15:45:09 +0000
changeset 466008 38cd4d2c99a11ee976d0491c5a2fecab92418678
parent 466007 7b0a87b6f9402fd9377da61aa839541e3ef192f1
child 466009 6d8c85e1f976c8ea68fc501cd144bfa717abe299
push idunknown
push userunknown
push dateunknown
reviewersrwood, tarek
bugs1538677
milestone68.0a1
Bug 1538677 - [mitmproxy] Allow mitmproxy commands at maximum 30s of runtime. r=rwood,tarek Differential Revision: https://phabricator.services.mozilla.com/D24711
testing/mozbase/mozproxy/mozproxy/backends/mitm.py
--- a/testing/mozbase/mozproxy/mozproxy/backends/mitm.py
+++ b/testing/mozbase/mozproxy/mozproxy/backends/mitm.py
@@ -37,16 +37,19 @@ except Exception:
         ".mitmproxy",
         "mitmproxy-ca-cert.cer",
     )
 
 # On Windows, deal with mozilla-build having forward slashes in $HOME:
 if os.name == "nt" and "/" in DEFAULT_CERT_PATH:
     DEFAULT_CERT_PATH = DEFAULT_CERT_PATH.replace("/", "\\")
 
+# maximal allowed runtime of a mitmproxy command
+MITMDUMP_COMMAND_TIMEOUT = 30
+
 # to install mitmproxy certificate into Firefox and turn on/off proxy
 POLICIES_CONTENT_ON = """{
   "policies": {
     "Certificates": {
       "Install": ["%(cert)s"]
     },
     "Proxy": {
       "Mode": "manual",
@@ -109,21 +112,16 @@ class Mitmproxy(Playback):
 
         # In case the setup fails, we want to stop the process before raising.
         try:
             self.setup()
         except Exception:
             self.stop()
             raise
 
-    @property
-    def mitmdump_sleep_seconds(self):
-        """Time to sleep, in seconds, after issuing a `mitmdump` command."""
-        return 10 if not self.config['run_local'] else 1
-
     def download(self):
         """Download and unpack mitmproxy binary and pageset using tooltool"""
         if not os.path.exists(self.mozproxy_dir):
             os.makedirs(self.mozproxy_dir)
 
         LOG.info("downloading mitmproxy binary")
         _manifest = os.path.join(here, self.config["playback_binary_manifest"])
         transformed_manifest = transform_platform(_manifest, self.config["platform"])
@@ -174,29 +172,29 @@ class Mitmproxy(Playback):
         LOG.info("Starting mitmproxy playback using command: %s" % " ".join(command))
         # to turn off mitmproxy log output, use these params for Popen:
         # Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
         self.mitmproxy_proc = ProcessHandler(command,
                                              logfile=os.path.join(self.upload_dir,
                                                                   "mitmproxy.log"),
                                              env=env)
         self.mitmproxy_proc.run()
-        max_wait = time.time() + self.mitmdump_sleep_seconds
+        end_time = time.time() + MITMDUMP_COMMAND_TIMEOUT
         ready = False
-        while time.time() < max_wait:
+        while time.time() < end_time:
             ready = self.check_proxy()
             if ready:
                 LOG.info(
                     "Mitmproxy playback successfully started as pid %d"
                     % self.mitmproxy_proc.pid
                 )
                 return
-            time.sleep(1)
+            time.sleep(0.25)
         # cannot continue as we won't be able to playback the pages
-        LOG.error("Aborting: mitmproxy playback process failed to work")
+        LOG.error("Aborting: Mitmproxy process did not startup")
         self.stop_mitmproxy_playback()
         sys.exit()  # XXX why do we need to do that? a raise is not enough?
 
     def stop_mitmproxy_playback(self):
         """Stop the mitproxy server playback"""
         if self.mitmproxy_proc is None or self.mitmproxy_proc.poll() is not None:
             return
         LOG.info(