Skip to content

script: Clear FetchThread callbacks when tearing down Document - #49049

Merged
mrobinson merged 2 commits into
servo:mainfrom
mrobinson:script-plug-ongoing-fetch-leak-2
Oct 20, 2026
Merged

mrobinson merged 2 commits into
servo:mainfrom
mrobinson:script-plug-ongoing-fetch-leak-2

Conversation

@mrobinson

Copy link
Copy Markdown
Member

When a Document is being destroyed, ensure that any outstanding
FetchThread callbacks are cleared. This ensures that any Trusted
references to the DOM are dropped allowing them to be garbage collected.

This change adds much more thorough leak testing. There are a number of
leaks that still need to be addressed. Test expectations are updated.

Testing: This change fixes a Servo-style WPT leak test.

@mrobinson
mrobinson force-pushed the script-plug-ongoing-fetch-leak-2 branch 3 times, most recently from ceafc78 to de9c630 Compare October 20, 2026 12:42
When a `Document` is being destroyed, ensure that any outstanding
`FetchThread` callbacks are cleared. This ensures that any `Trusted`
references to the DOM are dropped allowing them to be garbage collected.

This change adds much more thorough leak testing. There are a number of
leaks that still need to be addressed. Test expectations are updated.

Signed-off-by: Martin Robinson <[email protected]>
Co-authored-by: Simon Sapin <[email protected]>
@mrobinson
mrobinson force-pushed the script-plug-ongoing-fetch-leak-2 branch from de9c630 to eec46ef Compare October 20, 2026 12:46
@mrobinson
mrobinson marked this pull request as ready for review October 20, 2026 13:28
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 20, 2026
@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Oct 20, 2026
@mrobinson
mrobinson added this pull request to the merge queue Oct 20, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 20, 2026
Comment thread components/shared/net/lib.rs Outdated
#[expect(clippy::large_enum_variant)]
enum ToFetchThreadMessage {
Cancel(Vec<RequestId>, CoreResourceThread),
Cancel(Vec<RequestId>, CoreResourceThread, bool /* forget */),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this enum variant to used named fields?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I agree this is nicer.

Comment thread components/shared/net/lib.rs Outdated

active_fetch.get_mut()(fetch_response_msg);
if fetch_finished {
self.active_fetches.remove(&request_id);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s take advantage of this new OccupiedEntry, otherwise we might as well stick with get_mut()

Suggested change
self.active_fetches.remove(&request_id);
active_fetch.remove();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I had meant to do this, but forgot. Nice catch.

let Some(request_id) = self.request_id.take() else {
return;
};
let Some(ref core_resource_thread) = self.core_resource_thread else {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same but in the brand new 2018 style ;)

Suggested change
let Some(ref core_resource_thread) = self.core_resource_thread else {
let Some(core_resource_thread) = &self.core_resource_thread else {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@mrobinson
mrobinson removed this pull request from the merge queue due to a manual request Oct 20, 2026
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 20, 2026
Signed-off-by: Martin Robinson <[email protected]>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 20, 2026
@mrobinson
mrobinson enabled auto-merge October 20, 2026 14:09
@mrobinson
mrobinson added this pull request to the merge queue Oct 20, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 20, 2026
Merged via the queue into servo:main with commit db49f29 Oct 20, 2026
38 checks passed
@mrobinson
mrobinson deleted the script-plug-ongoing-fetch-leak-2 branch October 20, 2026 17:15
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 20, 2026
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 22, 2026
A fetch can only be cancelled. When its player is full, a media element
cancels its fetch and later starts a new Range request. That throws away
the bytes in flight (about 2.5 MB per refill on an OpenHarmony board),
costs a new request and, on HTTP/1.1, a new connection, because a body
that is not read to its end cannot be drained. Gecko suspends the
channel instead, and Chromium defers loading.

Add FetchController::suspend and FetchController::resume. Like a
cancellation, they go through the fetch thread of the content process,
so they stay ordered after the start of the fetch, and reach the
resource thread as CoreResourceMsg::Suspend and CoreResourceMsg::Resume.
There they set the state of the request's CancellationListener, which
now keeps running, suspended or cancelled in a tokio watch channel.

The HTTP network fetch reads the body in a loop that, while the fetch
is suspended, waits for the state to change instead of polling the
body, so that TCP or HTTP/2 flow control holds back the server. Data
received before the suspension is still delivered, and after a resume
the body continues with the next byte. A cancellation wakes the loop at
once, also while it waits for data from the network; before, it was
only noticed when the next chunk arrived. If the connection fails or
closes while the fetch is suspended, the fetch ends after the resume as
an interrupted body does. The loop keeps what it did before: media
bodies are not kept, and a cancellation marks the response aborted
under the body lock.

Net bounds suspension itself, since it learns of no content process
that goes away and an unread body never sees its connection close. A
fetch that stays suspended for longer than the new pref
network_fetch_suspend_timeout_ms (60 s) ends with a network error, as
if its connection was lost. At most two fetches per origin are
suspended at once in each HTTP state, and a further suspension is
ignored: the fetches to an origin share an HTTP/2 connection, and every
suspended stream holds its stream window of the connection window, 2 of
5 MiB with hyper's defaults, so three would stall every other request
to the origin. A suspended fetch that is redirected moves to the new
origin, or is resumed if that has no place left. Suspension is advisory
and only takes effect while a body is read from the network: cache
hits, which are delivered in one chunk, data, file, blob and protocol
handler responses, and fetches that ended ignore it.

A fetch that waits in the HTTP cache for a body that another fetch is
still receiving treats a suspension of that fetch like an abort, and
goes to the network instead of waiting for the resume.

The responses of the redirects, authentication retries and CORS
preflight of a fetch share its listener, so while it is suspended, the
unread bodies of such responses hold their connection until the resume
or the deadline instead of being drained.

Fork note: meant for upstream. Drop once upstream has it.

Testing: unit tests with servers that stream a 64 MiB body and count
what they hand to the connection: a suspended fetch holds back the
server and receives the whole body intact after the resume, over
HTTP/1.1 and over HTTP/2 (TLS with ALPN, a new test server); a
cancellation ends a suspended fetch, and one that waits for a stalled
server, within a second; a fetch whose server closes the connection
while it is suspended ends after the resume with what was sent; the
deadline ends suspended fetches with a network error and frees their
places; a third suspension per origin is ignored and its server keeps
sending; fetches waiting in the HTTP cache for a suspended response go
to the network; FetchController suspends and resumes a fetch through
the fetch thread and the resource thread, and does nothing after the
end.

Validation: `./mach test-unit -p servo-net`: 412 tests pass (403 before), also with the suspension tests in one process. While a fetch was suspended, the server handed 4.9 MiB of a 64 MiB body to the connection over HTTP/1.1 and 2.1 MiB over HTTP/2. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` fetch/api/basic fetch/api/abort fetch/range fetch/http-cache xhr html/semantics/embedded-content/media-elements: 1034 tests, 1027 as expected; the other seven behave the same with the parent commit when run alone three times.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent, reworked after a review by another agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#49049 (fetch-thread `Cancel` with `forget`, `cancel_and_forget_async_fetch`) and servo#49040 (timing info in the fetch params): `Suspend` and `Resume` sit next to the new `Cancel` variant, the imports are joined, and the body loop takes its timing from `fetch_params.timing_info`. `./mach test-unit -p servo-net` at this commit: 412 tests pass)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 22, 2026
Net ignores a suspension when it does not read the body from the
network (a cache hit, another scheme, or after the end) and when two
fetches to the origin of the request are suspended already, and nothing
told the consumer. A media element that suspended its fetch then kept
queueing a body that kept arriving, up to its 32 MiB cap, which it
discarded to restart the fetch, while its networkState said the
download was suspended.

Let the suspension carry an optional GenericCallback, to which the
resource thread sends the result of CancellationListener::suspend: true
if the fetch is suspended now, false if the suspension was ignored. A
GenericCallback works in both single-process and multi-process mode,
and runs in the process that suspended the fetch, so the consumer can
queue a task from it.

Fork note: meant for upstream. Drop once upstream has it.

Testing: a unit test suspends three fetches to one origin through
FetchController and the resource thread: the first two are reported as
suspended and the third not, and a suspension after the end of a fetch
is reported as ignored.

Validation: `./mach test-unit -p servo-net`: 413 tests pass (412 before); the new test passed five runs in a row, and fails when the resource thread always reports a suspension. `./mach check --ohos` passes.
AI-assisted: Yes (agent session; implemented and tested by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- mechanical conflict resolution, no semantic change: the `Suspend` reply next to upstream's `Cancel` struct variant (servo#49049))
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 30, 2026
A fetch can only be cancelled. When its player is full, a media element
cancels its fetch and later starts a new Range request. That throws away
the bytes in flight (about 2.5 MB per refill on an OpenHarmony board),
costs a new request and, on HTTP/1.1, a new connection, because a body
that is not read to its end cannot be drained. Gecko suspends the
channel instead, and Chromium defers loading.

Add FetchController::suspend and FetchController::resume. Like a
cancellation, they go through the fetch thread of the content process,
so they stay ordered after the start of the fetch, and reach the
resource thread as CoreResourceMsg::Suspend and CoreResourceMsg::Resume.
There they set the state of the request's CancellationListener, which
now keeps running, suspended or cancelled in a tokio watch channel.

The HTTP network fetch reads the body in a loop that, while the fetch
is suspended, waits for the state to change instead of polling the
body, so that TCP or HTTP/2 flow control holds back the server. Data
received before the suspension is still delivered, and after a resume
the body continues with the next byte. A cancellation wakes the loop at
once, also while it waits for data from the network; before, it was
only noticed when the next chunk arrived. If the connection fails or
closes while the fetch is suspended, the fetch ends after the resume as
an interrupted body does. The loop keeps what it did before: media
bodies are not kept, and a cancellation marks the response aborted
under the body lock.

Net bounds suspension itself, since it learns of no content process
that goes away and an unread body never sees its connection close. A
fetch that stays suspended for longer than the new pref
network_fetch_suspend_timeout_ms (60 s) ends with a network error, as
if its connection was lost. At most two fetches per origin are
suspended at once in each HTTP state, and a further suspension is
ignored: the fetches to an origin share an HTTP/2 connection, and every
suspended stream holds its stream window of the connection window, 2 of
5 MiB with hyper's defaults, so three would stall every other request
to the origin. A suspended fetch that is redirected moves to the new
origin, or is resumed if that has no place left. Suspension is advisory
and only takes effect while a body is read from the network: cache
hits, which are delivered in one chunk, data, file, blob and protocol
handler responses, and fetches that ended ignore it.

A fetch that waits in the HTTP cache for a body that another fetch is
still receiving treats a suspension of that fetch like an abort, and
goes to the network instead of waiting for the resume.

The responses of the redirects, authentication retries and CORS
preflight of a fetch share its listener, so while it is suspended, the
unread bodies of such responses hold their connection until the resume
or the deadline instead of being drained.

Fork note: meant for upstream. Drop once upstream has it.

Testing: unit tests with servers that stream a 64 MiB body and count
what they hand to the connection: a suspended fetch holds back the
server and receives the whole body intact after the resume, over
HTTP/1.1 and over HTTP/2 (TLS with ALPN, a new test server); a
cancellation ends a suspended fetch, and one that waits for a stalled
server, within a second; a fetch whose server closes the connection
while it is suspended ends after the resume with what was sent; the
deadline ends suspended fetches with a network error and frees their
places; a third suspension per origin is ignored and its server keeps
sending; fetches waiting in the HTTP cache for a suspended response go
to the network; FetchController suspends and resumes a fetch through
the fetch thread and the resource thread, and does nothing after the
end.

Validation: `./mach test-unit -p servo-net`: 412 tests pass (403 before), also with the suspension tests in one process. While a fetch was suspended, the server handed 4.9 MiB of a 64 MiB body to the connection over HTTP/1.1 and 2.1 MiB over HTTP/2. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` fetch/api/basic fetch/api/abort fetch/range fetch/http-cache xhr html/semantics/embedded-content/media-elements: 1034 tests, 1027 as expected; the other seven behave the same with the parent commit when run alone three times.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent, reworked after a review by another agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#49049 (fetch-thread `Cancel` with `forget`, `cancel_and_forget_async_fetch`) and servo#49040 (timing info in the fetch params): `Suspend` and `Resume` sit next to the new `Cancel` variant, the imports are joined, and the body loop takes its timing from `fetch_params.timing_info`. `./mach test-unit -p servo-net` at this commit: 412 tests pass)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 30, 2026
Net ignores a suspension when it does not read the body from the
network (a cache hit, another scheme, or after the end) and when two
fetches to the origin of the request are suspended already, and nothing
told the consumer. A media element that suspended its fetch then kept
queueing a body that kept arriving, up to its 32 MiB cap, which it
discarded to restart the fetch, while its networkState said the
download was suspended.

Let the suspension carry an optional GenericCallback, to which the
resource thread sends the result of CancellationListener::suspend: true
if the fetch is suspended now, false if the suspension was ignored. A
GenericCallback works in both single-process and multi-process mode,
and runs in the process that suspended the fetch, so the consumer can
queue a task from it.

Fork note: meant for upstream. Drop once upstream has it.

Testing: a unit test suspends three fetches to one origin through
FetchController and the resource thread: the first two are reported as
suspended and the third not, and a suspension after the end of a fetch
is reported as ignored.

Validation: `./mach test-unit -p servo-net`: 413 tests pass (412 before); the new test passed five runs in a row, and fails when the resource thread always reports a suspension. `./mach check --ohos` passes.
AI-assisted: Yes (agent session; implemented and tested by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- mechanical conflict resolution, no semantic change: the `Suspend` reply next to upstream's `Cancel` struct variant (servo#49049))
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 9, 2026
A fetch can only be cancelled. When its player is full, a media element
cancels its fetch and later starts a new Range request. That throws away
the bytes in flight (about 2.5 MB per refill on an OpenHarmony board),
costs a new request and, on HTTP/1.1, a new connection, because a body
that is not read to its end cannot be drained. Gecko suspends the
channel instead, and Chromium defers loading.

Add FetchController::suspend and FetchController::resume. Like a
cancellation, they go through the fetch thread of the content process,
so they stay ordered after the start of the fetch, and reach the
resource thread as CoreResourceMsg::Suspend and CoreResourceMsg::Resume.
There they set the state of the request's CancellationListener, which
now keeps running, suspended or cancelled in a tokio watch channel.

The HTTP network fetch reads the body in a loop that, while the fetch
is suspended, waits for the state to change instead of polling the
body, so that TCP or HTTP/2 flow control holds back the server. Data
received before the suspension is still delivered, and after a resume
the body continues with the next byte. A cancellation wakes the loop at
once, also while it waits for data from the network; before, it was
only noticed when the next chunk arrived. If the connection fails or
closes while the fetch is suspended, the fetch ends after the resume as
an interrupted body does. The loop keeps what it did before: media
bodies are not kept, and a cancellation marks the response aborted
under the body lock.

Net bounds suspension itself, since it learns of no content process
that goes away and an unread body never sees its connection close. A
fetch that stays suspended for longer than the new pref
network_fetch_suspend_timeout_ms (60 s) ends with a network error, as
if its connection was lost. At most two fetches per origin are
suspended at once in each HTTP state, and a further suspension is
ignored: the fetches to an origin share an HTTP/2 connection, and every
suspended stream holds its stream window of the connection window, 2 of
5 MiB with hyper's defaults, so three would stall every other request
to the origin. A suspended fetch that is redirected moves to the new
origin, or is resumed if that has no place left. Suspension is advisory
and only takes effect while a body is read from the network: cache
hits, which are delivered in one chunk, data, file, blob and protocol
handler responses, and fetches that ended ignore it.

A fetch that waits in the HTTP cache for a body that another fetch is
still receiving treats a suspension of that fetch like an abort, and
goes to the network instead of waiting for the resume.

The responses of the redirects, authentication retries and CORS
preflight of a fetch share its listener, so while it is suspended, the
unread bodies of such responses hold their connection until the resume
or the deadline instead of being drained.

Fork note: meant for upstream. Drop once upstream has it.

Testing: unit tests with servers that stream a 64 MiB body and count
what they hand to the connection: a suspended fetch holds back the
server and receives the whole body intact after the resume, over
HTTP/1.1 and over HTTP/2 (TLS with ALPN, a new test server); a
cancellation ends a suspended fetch, and one that waits for a stalled
server, within a second; a fetch whose server closes the connection
while it is suspended ends after the resume with what was sent; the
deadline ends suspended fetches with a network error and frees their
places; a third suspension per origin is ignored and its server keeps
sending; fetches waiting in the HTTP cache for a suspended response go
to the network; FetchController suspends and resumes a fetch through
the fetch thread and the resource thread, and does nothing after the
end.

Validation: `./mach test-unit -p servo-net`: 412 tests pass (403 before), also with the suspension tests in one process. While a fetch was suspended, the server handed 4.9 MiB of a 64 MiB body to the connection over HTTP/1.1 and 2.1 MiB over HTTP/2. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` fetch/api/basic fetch/api/abort fetch/range fetch/http-cache xhr html/semantics/embedded-content/media-elements: 1034 tests, 1027 as expected; the other seven behave the same with the parent commit when run alone three times.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent, reworked after a review by another agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#49049 (fetch-thread `Cancel` with `forget`, `cancel_and_forget_async_fetch`) and servo#49040 (timing info in the fetch params): `Suspend` and `Resume` sit next to the new `Cancel` variant, the imports are joined, and the body loop takes its timing from `fetch_params.timing_info`. `./mach test-unit -p servo-net` at this commit: 412 tests pass)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 9, 2026
Net ignores a suspension when it does not read the body from the
network (a cache hit, another scheme, or after the end) and when two
fetches to the origin of the request are suspended already, and nothing
told the consumer. A media element that suspended its fetch then kept
queueing a body that kept arriving, up to its 32 MiB cap, which it
discarded to restart the fetch, while its networkState said the
download was suspended.

Let the suspension carry an optional GenericCallback, to which the
resource thread sends the result of CancellationListener::suspend: true
if the fetch is suspended now, false if the suspension was ignored. A
GenericCallback works in both single-process and multi-process mode,
and runs in the process that suspended the fetch, so the consumer can
queue a task from it.

Fork note: meant for upstream. Drop once upstream has it.

Testing: a unit test suspends three fetches to one origin through
FetchController and the resource thread: the first two are reported as
suspended and the third not, and a suspension after the end of a fetch
is reported as ignored.

Validation: `./mach test-unit -p servo-net`: 413 tests pass (412 before); the new test passed five runs in a row, and fails when the resource thread always reports a suspension. `./mach check --ohos` passes.
AI-assisted: Yes (agent session; implemented and tested by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- mechanical conflict resolution, no semantic change: the `Suspend` reply next to upstream's `Cancel` struct variant (servo#49049))
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 10, 2026
A fetch can only be cancelled. When its player is full, a media element
cancels its fetch and later starts a new Range request. That throws away
the bytes in flight (about 2.5 MB per refill on an OpenHarmony board),
costs a new request and, on HTTP/1.1, a new connection, because a body
that is not read to its end cannot be drained. Gecko suspends the
channel instead, and Chromium defers loading.

Add FetchController::suspend and FetchController::resume. Like a
cancellation, they go through the fetch thread of the content process,
so they stay ordered after the start of the fetch, and reach the
resource thread as CoreResourceMsg::Suspend and CoreResourceMsg::Resume.
There they set the state of the request's CancellationListener, which
now keeps running, suspended or cancelled in a tokio watch channel.

The HTTP network fetch reads the body in a loop that, while the fetch
is suspended, waits for the state to change instead of polling the
body, so that TCP or HTTP/2 flow control holds back the server. Data
received before the suspension is still delivered, and after a resume
the body continues with the next byte. A cancellation wakes the loop at
once, also while it waits for data from the network; before, it was
only noticed when the next chunk arrived. If the connection fails or
closes while the fetch is suspended, the fetch ends after the resume as
an interrupted body does. The loop keeps what it did before: media
bodies are not kept, and a cancellation marks the response aborted
under the body lock.

Net bounds suspension itself, since it learns of no content process
that goes away and an unread body never sees its connection close. A
fetch that stays suspended for longer than the new pref
network_fetch_suspend_timeout_ms (60 s) ends with a network error, as
if its connection was lost. At most two fetches per origin are
suspended at once in each HTTP state, and a further suspension is
ignored: the fetches to an origin share an HTTP/2 connection, and every
suspended stream holds its stream window of the connection window, 2 of
5 MiB with hyper's defaults, so three would stall every other request
to the origin. A suspended fetch that is redirected moves to the new
origin, or is resumed if that has no place left. Suspension is advisory
and only takes effect while a body is read from the network: cache
hits, which are delivered in one chunk, data, file, blob and protocol
handler responses, and fetches that ended ignore it.

A fetch that waits in the HTTP cache for a body that another fetch is
still receiving treats a suspension of that fetch like an abort, and
goes to the network instead of waiting for the resume.

The responses of the redirects, authentication retries and CORS
preflight of a fetch share its listener, so while it is suspended, the
unread bodies of such responses hold their connection until the resume
or the deadline instead of being drained.

Fork note: meant for upstream. Drop once upstream has it.

Testing: unit tests with servers that stream a 64 MiB body and count
what they hand to the connection: a suspended fetch holds back the
server and receives the whole body intact after the resume, over
HTTP/1.1 and over HTTP/2 (TLS with ALPN, a new test server); a
cancellation ends a suspended fetch, and one that waits for a stalled
server, within a second; a fetch whose server closes the connection
while it is suspended ends after the resume with what was sent; the
deadline ends suspended fetches with a network error and frees their
places; a third suspension per origin is ignored and its server keeps
sending; fetches waiting in the HTTP cache for a suspended response go
to the network; FetchController suspends and resumes a fetch through
the fetch thread and the resource thread, and does nothing after the
end.

Folded in "net: Report whether a fetch was suspended":

Net ignores a suspension when it does not read the body from the
network (a cache hit, another scheme, or after the end) and when two
fetches to the origin of the request are suspended already, and nothing
told the consumer. A media element that suspended its fetch then kept
queueing a body that kept arriving, up to its 32 MiB cap, which it
discarded to restart the fetch, while its networkState said the
download was suspended.

Let the suspension carry an optional GenericCallback, to which the
resource thread sends the result of CancellationListener::suspend: true
if the fetch is suspended now, false if the suspension was ignored. A
GenericCallback works in both single-process and multi-process mode,
and runs in the process that suspended the fetch, so the consumer can
queue a task from it.

Fork note: meant for upstream. Drop once upstream has it.

Testing: a unit test suspends three fetches to one origin through
FetchController and the resource thread: the first two are reported as
suspended and the third not, and a suspension after the end of a fetch
is reported as ignored.

Validation: `./mach test-unit -p servo-net`: 412 tests pass (403 before), also with the suspension tests in one process. While a fetch was suspended, the server handed 4.9 MiB of a 64 MiB body to the connection over HTTP/1.1 and 2.1 MiB over HTTP/2. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` fetch/api/basic fetch/api/abort fetch/range fetch/http-cache xhr html/semantics/embedded-content/media-elements: 1034 tests, 1027 as expected; the other seven behave the same with the parent commit when run alone three times.
Validation: ("net: Report whether a fetch was suspended") `./mach test-unit -p servo-net`: 413 tests pass (412 before); the new test passed five runs in a row, and fails when the resource thread always reports a suspension. `./mach check --ohos` passes.
AI-assisted: Yes (agent session; implemented, tested and measured by the agent, reworked after a review by another agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#49049 (fetch-thread `Cancel` with `forget`, `cancel_and_forget_async_fetch`) and servo#49040 (timing info in the fetch params): `Suspend` and `Resume` sit next to the new `Cancel` variant, the imports are joined, and the body loop takes its timing from `fetch_params.timing_info`. `./mach test-unit -p servo-net` at this commit: 412 tests pass)
AI-assisted: Yes (agent session; "net: Report whether a fetch was suspended": implemented and tested by the agent)
AI-assisted: Yes (agent session; "net: Report whether a fetch was suspended": 2026-10-22 sync -- mechanical conflict resolution, no semantic change: the `Suspend` reply next to upstream's `Cancel` struct variant (servo#49049))
AI-assisted: Yes (agent session; 2026-11-09 restack -- folded in "net: Report whether a fetch was suspended", which tells the consumer when net ignores a suspension of this patch's API)
Signed-off-by: Jonathan Schwender <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants