Repository: servo/servo
File: components/background_hang_monitor/background_hang_monitor.rs (Rust)

Code before:
                    self.sampling_duration = Some(rate);
                    self.sampling_max_duration = Some(max_duration);
                    self.sampling_baseline = Instant::now();
                }
            },
            BhmMessage::Exit => {
                for component in self.monitored_components.values_mut() {
                    component.exit_signal.signal_to_exit();
                }

                // Note the start of shutdown to ensure exit propagates,
                // even to components that have yet to register themselves,
                // from this point on.
                self.shutting_down = true;
            },
            BhmMessage::ControlError(e) => {
                log::warn!(
                    "BackgroundHangMonitorWorker control message deserialization error: {e}"
                );
            },
            BhmMessage::ControlDisconnected => {
                // The control port has disconnected. This can happen during
                // shutdown when the EventLoop drops before all script threads
                // have finished. Instead of exiting immediately, we signal
                // all components to exit and continue running until they all
                // unregister (indicated by `self.port` disconnecting).
                if !self.shutting_down {
                    for component in self.monitored_components.values_mut() {
                        component.exit_signal.signal_to_exit();
                    }
                    self.shutting_down = true;
                }
                self.control_port = None;
            },
            BhmMessage::ComponentMessage(msg) => {
                self.handle_msg(msg);
                while let Ok(another_msg) = self.port.try_recv() {
                    // Handle any other incoming messages before performing a hang checkpoint.
                    self.handle_msg(another_msg);
                }
            },
            BhmMessage::Tick => {
                // Just proceed to checkpoint
            },
        }

        if let Some(duration) = self.sampling_duration {
            let now = Instant::now();
            if now - self.last_sample > duration {
                self.sample();
                self.last_sample = now;

Review comment (suggestion):
If this is true we should log an error because it means the port was dropped outside of the normal clean-shut down flow(the constellation panicked). 

Code after:
                    println!("Disabling profiler.");
                    self.sampling_duration = Some(rate);
                    self.sampling_max_duration = Some(max_duration);
                    self.sampling_baseline = Instant::now();
                }
            },
            BhmMessage::Exit => {
                for component in self.monitored_components.values_mut() {
                    component.exit_signal.signal_to_exit();
                }

                // Note the start of shutdown to ensure exit propagates,
                // even to components that have yet to register themselves,
                // from this point on.
                self.shutting_down = true;
            },
            BhmMessage::ControlError(e) => {
                warn!("BackgroundHangMonitorWorker control message deserialization error: {e}");
            },
            BhmMessage::ControlDisconnected => {
                // The control port has disconnected. This can happen during
                // shutdown when the EventLoop drops before all script threads
                // have finished. Instead of exiting immediately, we signal
                // all components to exit and continue running until they all
                // unregister (indicated by `self.port` disconnecting).
                if !self.shutting_down {
                    error!("BHM control disconnected before shutting down!");
                    for component in self.monitored_components.values_mut() {
                        component.exit_signal.signal_to_exit();
                    }
                    self.shutting_down = true;
                }
                self.control_port = None;
            },
            BhmMessage::ComponentMessage(msg) => {
                self.handle_msg(msg);
                while let Ok(another_msg) = self.port.try_recv() {
                    // Handle any other incoming messages before performing a hang checkpoint.
                    self.handle_msg(another_msg);
                }
            },
            BhmMessage::Tick => {
                // Just proceed to checkpoint
            },
        }

        if let Some(duration) = self.sampling_duration {
            let now = Instant::now();
            if now - self.last_sample > duration {
                self.sample();
                self.last_sample = now;