net: clean shutdown of fetch thread (#38421)

The fetch thread is currently not shut down, meaning it is one of the
threads counted in the "threads are still running after shutdown (bad)"
counter shown when Servo has shut-down.

This PR introduces a mechanism to start, and shut-down, one fetch thread
per process that requires one.

Testing: WPT tests and unit tests(for font context). Also manually
tested loading and closing "about:blank": this change indeed brings down
the count of threads still running after shutdown by one.

Fixes: https://github.com/servo/servo/issues/34887

---------

Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
Gregory Terzian 2025-08-07 17:01:30 +08:00 committed by GitHub
parent ad805e3110
commit b23cf9c6cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 71 additions and 11 deletions

View file

@ -149,7 +149,10 @@ use media::WindowGLContext;
use net_traits::pub_domains::reg_host;
use net_traits::request::Referrer;
use net_traits::storage_thread::{StorageThreadMsg, StorageType};
use net_traits::{self, AsyncRuntime, IpcSend, ReferrerPolicy, ResourceThreads};
use net_traits::{
self, AsyncRuntime, IpcSend, ReferrerPolicy, ResourceThreads, exit_fetch_thread,
start_fetch_thread,
};
use profile_traits::mem::ProfilerMsg;
use profile_traits::{mem, time};
use script_traits::{
@ -726,6 +729,11 @@ where
/// The main event loop for the constellation.
fn run(&mut self) {
// Start a fetch thread.
// In single-process mode this will be the global fetch thread;
// in multi-process mode this will be used only by the canvas paint thread.
let join_handle = start_fetch_thread(&self.public_resource_threads.core_thread);
while !self.shutting_down || !self.pipelines.is_empty() {
// Randomly close a pipeline if --random-pipeline-closure-probability is set
// This is for testing the hardening of the constellation.
@ -733,6 +741,12 @@ where
self.handle_request();
}
self.handle_shutdown();
// Shut down the fetch thread started above.
exit_fetch_thread();
join_handle
.join()
.expect("Failed to join on the fetch thread in the constellation");
}
/// Generate a new pipeline id namespace.

View file

@ -35,8 +35,8 @@ use layout_api::{LayoutFactory, ScriptThreadFactory};
use log::{debug, error, warn};
use media::WindowGLContext;
use net::image_cache::ImageCacheImpl;
use net_traits::ResourceThreads;
use net_traits::image_cache::ImageCache;
use net_traits::{CoreResourceThread, ResourceThreads};
use profile::system_reporter;
use profile_traits::mem::{ProfilerMsg, Reporter};
use profile_traits::{mem as profile_mem, time};
@ -575,6 +575,10 @@ impl UnprivilegedPipelineContent {
&self.script_to_constellation_chan
}
pub fn core_resource_thread(&self) -> &CoreResourceThread {
&self.resource_threads.core_thread
}
pub fn opts(&self) -> Opts {
self.opts.clone()
}