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

Code before:

                        // 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;

                        // Keep running; this worker thread will shutdown
                        // when the monitored components have shutdown,
                        // which we know has happened when `self.port` disconnects.
                        None
                    },
                    Ok(Err(e)) => {
                        log::warn!("BackgroundHangMonitorWorker control message deserialization error: {e:?}");
                        None
                    },
                    Err(_) => return false,
                }
            }
            recv(tick) -> _ => None,
        };

        if let Some(msg) = received {
            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);
            }
        }

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

    fn handle_msg(&mut self, msg: (MonitoredComponentId, MonitoredComponentMsg)) {
        match msg {
            (
                component_id,
                MonitoredComponentMsg::Register(
                    sampler,
                    name,
                    transient_hang_timeout,

Review comment (suggestion):
I was thinking if we should change this `println`. But should be fine as users want to have response when toggle.

Code after:
                }
                recv(tick) -> _ => BhmMessage::Tick,
            }
        } else {
            // control_port is already disconnected, just wait on port and tick
            select! {
                recv(self.port) -> event => {
                    match event {
                        Ok(msg) => BhmMessage::ComponentMessage(msg),
                        Err(_) => BhmMessage::PortDisconnected,
                    }
                },
                recv(tick) -> _ => BhmMessage::Tick,
            }
        };

        match result {
            BhmMessage::PortDisconnected => {
                // All senders have dropped,
                // which means all monitored components have shut down,
                // and so we can as well.
                return false;
            },
            BhmMessage::ToggleSampler(rate, max_duration) => {
                if self.sampling_duration.is_some() {
                    println!("Enabling profiler.");
                    self.finish_sampled_profile();
                    self.sampling_duration = None;
                } else {
                    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