constellation: join on script-threads (#38424)

We currently send exit signals to script-threads, and we also join on
the BHM worker. This ensure the constellation shuts-down only after
script has dropped it's sender to the BHM worker. By joining on the
script-threads, we have a guarantee that they have exited(which is
stronger than having dropped their senders) by the time the
constellation exits.

Testing: Manually opened many tabs and closed the window, both in
single- and multi-process modes.
Fixes: Part of - https://github.com/servo/servo/issues/30849

---------

Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
Gregory Terzian 2025-08-07 02:05:18 +08:00 committed by GitHub
parent b23adab8a5
commit a99ad240a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 45 additions and 16 deletions

View file

@ -461,6 +461,9 @@ pub struct Constellation<STF, SWF> {
/// The async runtime.
async_runtime: Box<dyn AsyncRuntime>,
/// When in single-process mode, join handles for script-threads.
script_join_handles: HashMap<WebViewId, JoinHandle<()>>,
}
/// State needed to construct a constellation.
@ -711,6 +714,7 @@ where
user_content_manager: state.user_content_manager,
process_manager: ProcessManager::new(state.mem_profiler_chan),
async_runtime: state.async_runtime,
script_join_handles: Default::default(),
};
constellation.run();
@ -972,6 +976,10 @@ where
self.background_monitor_control_senders.push(chan);
}
if let Some(join_handle) = pipeline.join_handle {
self.script_join_handles.insert(webview_id, join_handle);
}
if let Some(host) = host {
debug!("{}: Adding new host entry {}", webview_id, host,);
self.set_event_loop(
@ -2523,12 +2531,20 @@ where
fn handle_shutdown(&mut self) {
debug!("Handling shutdown.");
// In single process mode, join on script-threads
// from webview which haven't been manually closed before.
for (_, join_handle) in self.script_join_handles.drain() {
if join_handle.join().is_err() {
error!("Failed to join on a script-thread.");
}
}
// In single process mode, join on the background hang monitor worker thread.
drop(self.background_monitor_register.take());
if let Some(join_handle) = self.background_monitor_register_join_handle.take() {
join_handle
.join()
.expect("Failed to join on the BHM background thread.");
if join_handle.join().is_err() {
error!("Failed to join on the bhm background thread.");
}
}
// At this point, there are no active pipelines,
@ -3053,6 +3069,11 @@ where
.remove(&browsing_context.bc_group_id);
}
// Note: In single-process mode,
// if the webview is manually closed, we drop the join handle without joining on it.
// It is unlikely the thread will still run when the constellation shuts-down.
self.script_join_handles.remove(&webview_id);
debug!("{webview_id}: Closed");
}