Bug 1310868 - Tie resource collection to lifetime of context manager; r?glandium draft
authorGregory Szorc <gps@mozilla.com>
Mon, 17 Oct 2016 17:03:38 -0700
changeset 428242 2e613f47d1f1308f79d80be662f7a03a110fa249
parent 428241 5639a9f476d08f300c079117e61697f5026b6367
child 534686 24c0c9fb734ab15feea701908cb82c299c139579
push id33259
push userbmo:gps@mozilla.com
push dateFri, 21 Oct 2016 21:41:35 +0000
reviewersglandium
bugs1310868
milestone52.0a1
Bug 1310868 - Tie resource collection to lifetime of context manager; r?glandium Both BuildMonitor and BuildOutputManager are doing similar things and wrapping the build with forms of monitoring. These classes are only used in the `mach build` command. Since the BuildOutputManager takes an instance of BuildMonitor as an argument and since BuildOutputManager behaves as a context manager, it makes sense for BuildOutputManager to control the lifetime of BuildMonitor's collection. So, we teach BuildOutputManager's context manager to start the BuildMonitor and ensure its resource collection is stopped when it exits. MozReview-Commit-ID: 3l9Hwz0Xe7o
python/mozbuild/mozbuild/controller/building.py
python/mozbuild/mozbuild/mach_commands.py
--- a/python/mozbuild/mozbuild/controller/building.py
+++ b/python/mozbuild/mozbuild/controller/building.py
@@ -233,23 +233,26 @@ class BuildMonitor(MozbuildObject):
 
         try:
             warning = self._warnings_collector.process_line(line)
         except:
             pass
 
         return BuildOutputResult(warning, False, True)
 
-    def finish(self, record_usage=True):
-        """Record the end of the build."""
-        self.end_time = time.time()
-
+    def stop_resource_recording(self):
         if self._resources_started:
             self.resources.stop()
 
+        self._resources_started = False
+
+    def finish(self, record_usage=True):
+        """Record the end of the build."""
+        self.stop_resource_recording()
+        self.end_time = time.time()
         self._finder_end_cpu = self._get_finder_cpu_usage()
         self.elapsed = self.end_time - self.start_time
 
         self.warnings_database.prune()
         self.warnings_database.save_to_file(self._warnings_path)
 
         if not record_usage:
             return
--- a/python/mozbuild/mozbuild/mach_commands.py
+++ b/python/mozbuild/mozbuild/mach_commands.py
@@ -225,16 +225,21 @@ class BuildOutputManager(LoggingMixin):
         return self
 
     def __exit__(self, exc_type, exc_value, traceback):
         if self.footer:
             self.footer.clear()
             # Prevents the footer from being redrawn if logging occurs.
             self._handler.footer = None
 
+        # Ensure the resource monitor is stopped because leaving it running
+        # could result in the process hanging on exit because the resource
+        # collection child process hasn't been told to stop.
+        self.monitor.stop_resource_recording()
+
     def write_line(self, line):
         if self.footer:
             self.footer.clear()
 
         print(line)
 
         if self.footer:
             self.footer.draw()