net: Remove CoreResourceThread from FetchThread state (#38422)

In single process mode, there is a race condition on the initialization
of the global fetch thread: once initialized the global fetch thread
will always use a given core resource thread, and this will be
determined by the component who first initializes it. For example, if
the canvas paint thread first does an async fetch, then this will set
the public core resource as used for all future fetches, including those
coming from a pipeline in private mode.

In multi-process mode, there is a race condition per window event-loop:
the first pipeline to use the fetch will set the core resource thread
for all others.

To ensure the fetch thread uses the correct core resource thread(private
vs public), we need to
pass the core resource thread to each fetch thread operation for which
is it needed.

Testing: It should not break existing fetch WPT tests. The race
condition is not something that can be tested reliably, but it seems to
be based on solid logic.
Fixes: follow-up from
https://github.com/servo/servo/pull/38421/files#r2248950924

---------

Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
Gregory Terzian 2025-08-14 01:40:10 +08:00 committed by GitHub
parent 5ff084a688
commit 70be996a29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 87 additions and 50 deletions

View file

@ -137,7 +137,10 @@ impl DocumentLoader {
request: RequestBuilder,
callback: BoxedFetchCallback,
) {
self.cancellers.push(FetchCanceller::new(request.id));
self.cancellers.push(FetchCanceller::new(
request.id,
self.resource_threads.core_thread.clone(),
));
fetch_async(&self.resource_threads.core_thread, request, None, callback);
}

View file

@ -648,7 +648,10 @@ impl EventSourceMethods<crate::DomTypeHolder> for EventSource {
listener.notify_fetch(message.unwrap());
}),
);
ev.droppable.set_canceller(FetchCanceller::new(request.id));
ev.droppable.set_canceller(FetchCanceller::new(
request.id,
global.core_resource_thread(),
));
global
.core_resource_thread()
.send(CoreResourceMsg::Fetch(

View file

@ -24,8 +24,8 @@ use layout_api::MediaFrame;
use media::{GLPlayerMsg, GLPlayerMsgForward, WindowGLContext};
use net_traits::request::{Destination, RequestId};
use net_traits::{
FetchMetadata, FetchResponseListener, FilteredMetadata, Metadata, NetworkError,
ResourceFetchTiming, ResourceTimingType,
CoreResourceThread, FetchMetadata, FetchResponseListener, FilteredMetadata, Metadata,
NetworkError, ResourceFetchTiming, ResourceTimingType,
};
use pixels::RasterImage;
use script_bindings::codegen::GenericBindings::TimeRangesBinding::TimeRangesMethods;
@ -945,7 +945,10 @@ impl HTMLMediaElement {
current_fetch_context.cancel(CancelReason::Overridden);
}
*current_fetch_context = Some(HTMLMediaElementFetchContext::new(request.id));
*current_fetch_context = Some(HTMLMediaElementFetchContext::new(
request.id,
global.core_resource_thread(),
));
let listener =
HTMLMediaElementFetchListener::new(self, request.id, url.clone(), offset.unwrap_or(0));
@ -2848,14 +2851,17 @@ pub(crate) struct HTMLMediaElementFetchContext {
}
impl HTMLMediaElementFetchContext {
fn new(request_id: RequestId) -> HTMLMediaElementFetchContext {
fn new(
request_id: RequestId,
core_resource_thread: CoreResourceThread,
) -> HTMLMediaElementFetchContext {
HTMLMediaElementFetchContext {
request_id,
cancel_reason: None,
is_seekable: false,
origin_clean: true,
data_source: DomRefCell::new(BufferedDataSource::new()),
fetch_canceller: FetchCanceller::new(request_id),
fetch_canceller: FetchCanceller::new(request_id, core_resource_thread.clone()),
}
}

View file

@ -16,8 +16,8 @@ use net_traits::image_cache::{
};
use net_traits::request::{CredentialsMode, Destination, RequestBuilder, RequestId};
use net_traits::{
FetchMetadata, FetchResponseListener, FetchResponseMsg, NetworkError, ResourceFetchTiming,
ResourceTimingType,
CoreResourceThread, FetchMetadata, FetchResponseListener, FetchResponseMsg, NetworkError,
ResourceFetchTiming, ResourceTimingType,
};
use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use servo_media::player::video::VideoFrame;
@ -271,7 +271,13 @@ impl HTMLVideoElement {
LoadType::Image(poster_url.clone()),
));
let context = PosterFrameFetchContext::new(self, poster_url, id, request.id);
let context = PosterFrameFetchContext::new(
self,
poster_url,
id,
request.id,
self.global().core_resource_thread(),
);
self.owner_document().fetch_background(request, context);
}
@ -498,6 +504,7 @@ impl PosterFrameFetchContext {
url: ServoUrl,
id: PendingImageId,
request_id: RequestId,
core_resource_thread: CoreResourceThread,
) -> PosterFrameFetchContext {
let window = elem.owner_window();
PosterFrameFetchContext {
@ -507,7 +514,7 @@ impl PosterFrameFetchContext {
cancelled: false,
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
url,
fetch_canceller: FetchCanceller::new(request_id),
fetch_canceller: FetchCanceller::new(request_id, core_resource_thread),
}
}
}

View file

@ -1582,7 +1582,8 @@ impl XMLHttpRequest {
)
};
*self.canceller.borrow_mut() = FetchCanceller::new(request_builder.id);
*self.canceller.borrow_mut() =
FetchCanceller::new(request_builder.id, global.core_resource_thread());
global.fetch(request_builder, context.clone(), task_source);
if let Some(script_port) = script_port {

View file

@ -48,21 +48,27 @@ struct FetchContext {
resource_timing: ResourceFetchTiming,
}
/// RAII fetch canceller object. By default initialized to not having a canceller
/// in it, however you can ask it for a cancellation receiver to send to Fetch
/// in which case it will store the sender. You can manually cancel it
/// or let it cancel on Drop in that case.
/// RAII fetch canceller object.
/// By default initialized to having a
/// request associated with it, which can be manually cancelled with `cancel`,
/// or automatically cancelled on drop.
/// Calling `ignore` will sever the relationship with the request,
/// meaning it cannot be cancelled through this canceller from that point on.
#[derive(Default, JSTraceable, MallocSizeOf)]
pub(crate) struct FetchCanceller {
#[no_trace]
request_id: Option<RequestId>,
#[no_trace]
core_resource_thread: Option<CoreResourceThread>,
}
impl FetchCanceller {
/// Create an empty FetchCanceller
pub(crate) fn new(request_id: RequestId) -> Self {
/// Create a FetchCanceller associated with a request,
// and a particular(public vs private) resource thread.
pub(crate) fn new(request_id: RequestId, core_resource_thread: CoreResourceThread) -> Self {
Self {
request_id: Some(request_id),
core_resource_thread: Some(core_resource_thread),
}
}
@ -72,9 +78,11 @@ impl FetchCanceller {
// stop trying to make fetch happen
// it's not going to happen
// No error handling here. Cancellation is a courtesy call,
// we don't actually care if the other side heard.
cancel_async_fetch(vec![request_id]);
if let Some(ref core_resource_thread) = self.core_resource_thread {
// No error handling here. Cancellation is a courtesy call,
// we don't actually care if the other side heard.
cancel_async_fetch(vec![request_id], core_resource_thread);
}
}
}

View file

@ -3638,7 +3638,10 @@ impl ScriptThread {
.push((incomplete.pipeline_id, context));
let request_builder = incomplete.request_builder();
incomplete.canceller = FetchCanceller::new(request_builder.id);
incomplete.canceller = FetchCanceller::new(
request_builder.id,
self.resource_threads.core_thread.clone(),
);
NavigationListener::new(request_builder, self.senders.self_sender.clone())
.initiate_fetch(&self.resource_threads.core_thread, None);
self.incomplete_loads.borrow_mut().push(incomplete);
@ -3771,7 +3774,10 @@ impl ScriptThread {
.unwrap_or(200),
});
incomplete_load.canceller = FetchCanceller::new(request_builder.id);
incomplete_load.canceller = FetchCanceller::new(
request_builder.id,
self.resource_threads.core_thread.clone(),
);
NavigationListener::new(request_builder, self.senders.self_sender.clone())
.initiate_fetch(&self.resource_threads.core_thread, response_init);
}