net: clean shutdown of the async runtime (#38425)

The previous use of a static variable for the runtime prevented it from
shutting down cleanly, because shutdown requires dropping or taking
ownership of it. This PR switches the static variable to a handle only,
and introduces a new trait to pass a handle to the async runtime to the
constellation, where it can be shut-down along with other components and
help reduce our count of still running threads after shutdown.

Testing: manual testing, and covered by unit-test in net, and wpt tests.
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-05 05:42:47 +08:00 committed by GitHub
parent 7ad32f944f
commit 77ff351cde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 132 additions and 49 deletions

View file

@ -149,7 +149,7 @@ 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, IpcSend, ReferrerPolicy, ResourceThreads};
use net_traits::{self, AsyncRuntime, IpcSend, ReferrerPolicy, ResourceThreads};
use profile_traits::mem::ProfilerMsg;
use profile_traits::{mem, time};
use script_traits::{
@ -458,6 +458,9 @@ pub struct Constellation<STF, SWF> {
/// The process manager.
process_manager: ProcessManager,
/// The async runtime.
async_runtime: Box<dyn AsyncRuntime>,
}
/// State needed to construct a constellation.
@ -510,6 +513,9 @@ pub struct InitialConstellationState {
/// User content manager
pub user_content_manager: UserContentManager,
/// The async runtime.
pub async_runtime: Box<dyn AsyncRuntime>,
}
/// When we are running reftests, we save an image to compare against a reference.
@ -704,6 +710,7 @@ where
rippy_data,
user_content_manager: state.user_content_manager,
process_manager: ProcessManager::new(state.mem_profiler_chan),
async_runtime: state.async_runtime,
};
constellation.run();
@ -2651,6 +2658,9 @@ where
debug!("Shutting-down IPC router thread in constellation.");
ROUTER.shutdown();
debug!("Shutting-down the async runtime in constellation.");
self.async_runtime.shutdown();
}
fn handle_pipeline_exited(&mut self, pipeline_id: PipelineId) {