From 5600a1dca16c1d8c72b87bde9567293542ed7205 Mon Sep 17 00:00:00 2001 From: csmoe Date: Sat, 15 Dec 2018 21:55:58 +0800 Subject: [PATCH] unregister components while exiting --- .../background_hang_monitor.rs | 12 +++++++++ .../tests/hang_monitor_tests.rs | 27 +++++++++++++++++++ components/layout_thread/lib.rs | 1 + components/msg/constellation_msg.rs | 2 ++ components/script/script_thread.rs | 2 ++ 5 files changed, 44 insertions(+) diff --git a/components/background_hang_monitor/background_hang_monitor.rs b/components/background_hang_monitor/background_hang_monitor.rs index f5e5b72fb32..3511b725f5a 100644 --- a/components/background_hang_monitor/background_hang_monitor.rs +++ b/components/background_hang_monitor/background_hang_monitor.rs @@ -72,6 +72,8 @@ impl BackgroundHangMonitorClone for HangMonitorRegister { pub enum MonitoredComponentMsg { /// Register component for monitoring, Register(Box, Duration, Duration), + /// Unregister component for monitoring. + Unregister, /// Notify start of new activity for a given component, NotifyActivity(HangAnnotation), /// Notify start of waiting for a new task to come-in for processing. @@ -119,6 +121,10 @@ impl BackgroundHangMonitor for BackgroundHangMonitorChan { let msg = MonitoredComponentMsg::NotifyWait; self.send(msg); } + fn unregister(&self) { + let msg = MonitoredComponentMsg::Unregister; + self.send(msg); + } } struct MonitoredComponent { @@ -200,6 +206,12 @@ impl BackgroundHangMonitorWorker { "This component was already registered for monitoring." ); }, + (component_id, MonitoredComponentMsg::Unregister) => { + let _ = self + .monitored_components + .remove_entry(&component_id) + .expect("Received Unregister for an unknown component"); + }, (component_id, MonitoredComponentMsg::NotifyActivity(annotation)) => { let component = self .monitored_components diff --git a/components/background_hang_monitor/tests/hang_monitor_tests.rs b/components/background_hang_monitor/tests/hang_monitor_tests.rs index 4c39bbcc7ac..d7e6dcf268d 100644 --- a/components/background_hang_monitor/tests/hang_monitor_tests.rs +++ b/components/background_hang_monitor/tests/hang_monitor_tests.rs @@ -105,3 +105,30 @@ fn test_hang_monitoring() { // Still no new alerts because the hang monitor has shut-down already. assert!(background_hang_monitor_receiver.try_recv().is_err()); } + +#[test] +fn test_hang_monitoring_unregister() { + let (background_hang_monitor_ipc_sender, background_hang_monitor_receiver) = + ipc::channel().expect("ipc channel failure"); + + let background_hang_monitor_register = + HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone()); + let background_hang_monitor = background_hang_monitor_register.register_component( + MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script), + Duration::from_millis(10), + Duration::from_millis(1000), + ); + + // Start an activity. + let hang_annotation = HangAnnotation::Script(ScriptHangAnnotation::AttachLayout); + background_hang_monitor.notify_activity(hang_annotation); + + // Unregister the component. + background_hang_monitor.unregister(); + + // Sleep until the "transient" timeout has been reached. + thread::sleep(Duration::from_millis(10)); + + // No new alert yet + assert!(background_hang_monitor_receiver.try_recv().is_err()); +} diff --git a/components/layout_thread/lib.rs b/components/layout_thread/lib.rs index fd419bdb10f..0d0293d86aa 100644 --- a/components/layout_thread/lib.rs +++ b/components/layout_thread/lib.rs @@ -926,6 +926,7 @@ impl LayoutThread { self.root_flow.borrow_mut().take(); // Drop the rayon threadpool if present. let _ = self.parallel_traversal.take(); + self.background_hang_monitor.unregister(); } fn handle_add_stylesheet(&self, stylesheet: &Stylesheet, guard: &SharedRwLockReadGuard) { diff --git a/components/msg/constellation_msg.rs b/components/msg/constellation_msg.rs index 897c9deecf9..8de57e15d7c 100644 --- a/components/msg/constellation_msg.rs +++ b/components/msg/constellation_msg.rs @@ -469,4 +469,6 @@ pub trait BackgroundHangMonitor { fn notify_activity(&self, annotation: HangAnnotation); /// Notify the start of waiting for a new event to come in. fn notify_wait(&self); + /// Unregister the component from monitor. + fn unregister(&self); } diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index f7bc6b9b26c..077025759a8 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -2379,6 +2379,8 @@ impl ScriptThread { self.handle_exit_pipeline_msg(pipeline_id, DiscardBrowsingContext::Yes); } + self.background_hang_monitor.unregister(); + debug!("Exited script thread."); }