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

@ -29,9 +29,9 @@ use net_traits::request::{Destination, RequestBuilder, RequestId};
use net_traits::response::{Response, ResponseInit};
use net_traits::storage_thread::StorageThreadMsg;
use net_traits::{
CookieSource, CoreResourceMsg, CoreResourceThread, CustomResponseMediator, DiscardFetch,
FetchChannels, FetchTaskTarget, ResourceFetchTiming, ResourceThreads, ResourceTimingType,
WebSocketDomAction, WebSocketNetworkEvent,
AsyncRuntime, CookieSource, CoreResourceMsg, CoreResourceThread, CustomResponseMediator,
DiscardFetch, FetchChannels, FetchTaskTarget, ResourceFetchTiming, ResourceThreads,
ResourceTimingType, WebSocketDomAction, WebSocketNetworkEvent,
};
use profile_traits::mem::{
ProcessReports, ProfilerChan as MemProfilerChan, Report, ReportKind, ReportsChan,
@ -44,7 +44,7 @@ use serde::{Deserialize, Serialize};
use servo_arc::Arc as ServoArc;
use servo_url::{ImmutableOrigin, ServoUrl};
use crate::async_runtime::HANDLE;
use crate::async_runtime::{init_async_runtime, spawn_task};
use crate::connector::{
CACertificates, CertificateErrorOverrideManager, create_http_client, create_tls_config,
};
@ -84,7 +84,10 @@ pub fn new_resource_threads(
certificate_path: Option<String>,
ignore_certificate_errors: bool,
protocols: Arc<ProtocolRegistry>,
) -> (ResourceThreads, ResourceThreads) {
) -> (ResourceThreads, ResourceThreads, Box<dyn AsyncRuntime>) {
// Initialize the async runtime, and get a handle to it for use in clean shutdown.
let async_runtime = init_async_runtime();
let ca_certificates = match certificate_path {
Some(path) => match load_root_cert_store_from_file(path) {
Ok(root_cert_store) => CACertificates::Override(root_cert_store),
@ -112,6 +115,7 @@ pub fn new_resource_threads(
(
ResourceThreads::new(public_core, storage.clone(), idb.clone()),
ResourceThreads::new(private_core, storage, idb),
async_runtime,
)
}
@ -781,7 +785,7 @@ impl CoreResourceManager {
_ => (FileTokenCheck::NotRequired, None),
};
HANDLE.spawn(async move {
spawn_task(async move {
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
// todo load context / mimesniff in fetch
// todo referrer policy?