Repository navigation
net: Replace all matching HTTP cached resources when revalidating - #48927
Merged
Merged
Conversation
3 of 4 tasks
Collaborator
|
🤖 Opened new upstream WPT pull request (web-platform-tests/wpt#63692) with upstreamable changes. |
mrobinson
force-pushed
the
net-improve-revalidation-errors
branch
2 times, most recently
from
October 15, 2026 09:04
0bbb9db to
85990f2
Compare
|
🔨 Triggering try run (#38710406631) for Linux (WPT) |
Collaborator
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63692). |
1 similar comment
Collaborator
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63692). |
|
Test results for linux-wpt from try job (#38710406631): Flaky unexpected result (17)
Stable unexpected results that are known to be intermittent (13)
|
|
✨ Try run (#38710406631) succeeded. |
Narfinger
reviewed
Oct 15, 2026
| .iter() | ||
| .filter(|resource| resource.can_be_used_to_answer(request)) | ||
| .collect(); | ||
| handle_range_request(request, cached_resources.as_slice(), &range_spec, done_chan) |
Narfinger
approved these changes
Oct 15, 2026
Narfinger
left a comment
Contributor
There was a problem hiding this comment.
Other then the (perhaps) nit this looks ok to me.
This change fixes issues with cache revalidation where an older response would be returned from the cache. Now when a new response comes in, it replaces all *matching* older ones using the same logic that is used to find candidate cached resources during load. In addition, when there is more than one matching resource, the *newest matching* is returned. Signed-off-by: Martin Robinson <[email protected]> Co-authored-by: Josh Bowman-Matthews <[email protected]>
mrobinson
force-pushed
the
net-improve-revalidation-errors
branch
from
October 15, 2026 11:52
85990f2 to
8d76843
Compare
Member
Author
Oh, nice catch! |
mrobinson
enabled auto-merge
October 15, 2026 11:52
Collaborator
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63692). |
jschwe
added a commit
to openharmony-rs/servo-ohos
that referenced
this pull request
Oct 22, 2026
The HTTP cache stores a response when its headers arrive and shares its body with the fetch that receives it. When the body failed with an error other than a cancellation, net still marked it as done, so the cache kept the truncated body as a complete response and could answer later requests with it. A cancelled body was emptied and marked as done before the response was marked as aborted, so a lookup in between could answer with an empty body, and a range cut from a stored response did not share its aborted flag. A fetch that waited for a body still being received got the incomplete body when the receiving fetch was aborted, and waited forever when that fetch ended in a network error of its own, such as a failed integrity check, because then only responses with an empty body were woken up. And the cache never removed the entries of aborted fetches, so every cancelled fetch left one more unusable entry in the list of responses for its URL; media elements cancel a fetch on every seek. Mark the response as aborted when its body fails or is cancelled, before the body is replaced. Constructing a response from the cache, or refreshing it after a 304, checks the flag while holding the body, ranges cut from a stored response share its flag, and the refresh skips aborted responses. When the receiving fetch ends, wake every fetch that waits for a complete or aborted body, whatever its status; one that found the response by a cache lookup and whose body was aborted now fetches from the network instead, or fails if it is only-if-cached. A fetch that waits for the body through the refresh of a 304 is unchanged: it still ends with an empty body when the receiving fetch is aborted. Remove the entries of aborted fetches that nobody waits for when their fetch ends and whenever a response is stored for the same URL, and the URL once it has no entries left. The fetch that receives a failed body still gets it as before, and only the cache reads the aborted flag of a response. Fork note: meant for upstream. Drop once upstream has it. Testing: unit tests that ranges cut from a stored response share its aborted flag, that revalidation skips aborted responses, and that the cache removes aborted entries when it stores a response unless a fetch waits for them; fetch tests that a truncated response is neither stored nor served, that a cancelled fetch leaves neither an entry nor its URL in the cache, that a fetch waiting for an aborted response fetches it from the network, and that one waiting for a response whose fetch failed its integrity check is woken up. `CancellationListener::cancel` is now public, and the cache has test-only counts of stored responses and of waiting fetches. Validation: `./mach test-unit -p servo-net`: 403 tests pass. Each new test fails when its part of the fix is reverted: without the shared flag, a range keeps a flag of its own; without the skip, revalidation uses the aborted response; without the network fallback, the waiting fetch delivers the empty body of the aborted one; without waking every waiting fetch, the one waiting for the response that failed its integrity check never ends; without removing the URL, a cancelled fetch leaves it in the cache; and, checked on the first revision, the truncated response is stored and served without the aborted flag on errors, the aborted entries stay without their removal when a fetch ends or when a response is stored, and a waited-for entry is removed without the check for waiting fetches. Holding the body while checking the flag closes a race that no deterministic test reproduces. Playing, reloading and playing again a 90 s, 42 MB video as in the previous patch, VmRSS after the first play / at the end: 311/378, 328/388 and 329/389 MiB with both patches, 323/384 and 314/383 with the previous one alone, no measurable difference. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` of fetch/http-cache, fetch/range, fetch/api/basic and xhr: all 619 tests as expected; html/semantics/embedded-content/media-elements as in the previous patch. AI-assisted: Yes (agent session; written and tested by the agent, reworked after a review by another agent) AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#48927 (most recent matching response, replacement of matching complete responses): `refresh` keeps upstream's `can_be_used_to_answer` lookup, which already skips aborted resources, plus this patch's aborted check under the body lock; `is_abandoned` sits next to upstream's new methods; the insert drops abandoned resources before upstream's replacement of matching complete ones. `./mach test-unit -p servo-net` at this commit: 403 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
The HTTP cache kept every stored body as a `Vec<u8>` in the net process,
shared with the response handed to the consumer, so net also kept the
decoded body of every response until its fetch ended. Its RFC 9111
rules were hand-written for RFC 7234.
The rules now come from the `http-cache-rules` crate: storability, the
stored and updated header fields, freshness (with `max-age`, `Expires`,
the heuristic, `Age` and `Date`), `Vary`, `stale-while-revalidate`,
`immutable`, the request directives, the validation request and the
merge of a 304. The crate decides and stores nothing; its requirements
are traced to the RFC text and tested, model checked and fuzzed in its
own repository. `http_cache::policy` configures it as a private cache
that stores 206 responses and their bodies encoded.
Servo keeps what belongs to the fetch layer: the cache modes, background
revalidation (only for requests with a client), invalidation after
unsafe methods, ranges, and its own refusals on top of the crate: `Vary:
*`, entries over the store's size limit, 206 without `Content-Range`,
and the storage rule for audio and video of patch 79.
Storage moves behind `CacheStore`, which streams bodies in and out:
* `TeeBody` copies each frame into the store below the content
decoder, so the stored body is what came off the wire, and the copy
is a reference count, not a second allocation. The store's bounded
queue back-pressures the network.
* A hit streams the entry through the decoder in chunks.
* `ResponseBody::Streamed` marks a body delivered to the consumer and
kept nowhere. Only subresource integrity and preloads still keep the
whole decoded body (`retains_whole_body`), so net no longer keeps
media bodies, which patch 79 had to arrange separately.
* An entry is visible only once its decoded body ended cleanly, so a
failed, cancelled or truncated body never becomes an entry, which
patch 80 had to arrange separately. The fetch commits it through a
`TeeCommit`, not `TeeBody` at the end of the network body: the
decoder stops reading at the end of a gzip or brotli stream.
* Every entry carries a random stamp, and the store opens, updates
and removes an entry only for the stamp it was looked up with, since
a later entry of the key may reuse its id.
* `http_cache::inflight` lets one fetch write a key. Once its headers
show a fresh response, others wait for it and read the committed
entry, or go to the network if it aborts or, as patch 92 requires,
while it is suspended. Before that, or if the response needs
validation, they go to the network as with the old cache, so a slow
or long-polled response holds up no other fetch. Once the whole body
has arrived, they wait for any response while the store commits it,
so a fetch right after another one finds its entry. A fetch does
not wait for a response that varies on a field in which its own
request differs, or whose body outgrows what the store accepts. It
stops waiting when it is cancelled, and goes on from there like any
cancelled fetch, and it waits 5 s at most.
* `MemoryStore` is the only store in this patch: byte-bounded, LRU,
for private browsing, temporary storage, no cache directory, and
the tests. The SQLite cache goes, and `rusqlite`, `sea-query` and
`quick_cache` leave `components/net`.
Fixes over the cache of the `http-cache-v2` branch, which this replaces:
a 304 no longer collapses repeated stored fields (a two-line CSP lost a
line) nor freshens on a weak or mismatching strong validator, `Age`
takes `Date` and the request time into account, the RFC 850 and asctime
date formats are understood, and `Vary` compares all field lines. That
branch also never stored a compressed response but stored a gzip body
cut short under clean framing, let a late 304 freshen or remove the
entry that had replaced its own, and made a fetch wait for the whole
body of another fetch's response even when it could not use it, and
wrote the URL's credentials into every entry: the key now leaves them
and the fragment out, and a stored response is served with the
request's URL. An
invalidation through `Location` or `Content-Location` reaches only URIs
of the target's origin (RFC 9111 4.4). Of several matching stored
responses the most recent is used (RFC 9111 4). When a 304 is about
another response than the stored one, the request is made again without
preconditions, and that response is stored.
Prefs: `network_http_cache_size` (an entry count) and
`network_http_disk_cache` (a path) are replaced by
`network_http_memory_cache_size` and `network_http_cache_max_entry_size`,
in bytes; `network_http_disk_cache_size` gains 0 = automatic.
`apply_algorithm_to_response` returned through `unreachable!()` when
the body was not `Done`, which a null body status makes it, and a
streamed body would reach reliably. It now returns a digest that cannot
match, so the fetch fails as `response-null-body.any.js` expects.
`preload/prefetch-document.html`: "different-site document prefetch with
'as=document' should not be consumed" now also fails at times, like its
two siblings. Servo has no HTTP cache partition (servo#33616), so the fresh
response of a cross-site prefetch answers the later navigation, unless
the navigation starts before the prefetch's response headers arrive and
goes to the network. With `network_http_cache_disabled=true` the subtest
passes and the "same origin ... should be consumed" sibling fails
instead.
Fork note: supersedes patches 79 and 80 (their cache code goes; their
fetch tests stay), and moves the cache side of patch 92 into
`http_cache::inflight`. 79 and 80 stayed on the stack at the 2026-10-22
sync: patch 92 builds on both (it calls 79's `may_store_media_response`
and sets 80's abort flag), so dropping them means reworking 92. Uses
`http-cache-rules` 0.1.0 from crates.io.
Testing: tests/http_cache.rs covers storability, Vary, freshness,
stale-while-revalidate with and without a client, partial entries and
ranges (with the five range tests of the `http-cache-range` branch),
the encoded body, the media storage rule, same-origin invalidation and
the most recent variant. tests/fetch.rs covers a 304 with another strong
validator, a 304 resetting Age, and a 304 with no-store.
tests/http_loader.rs covers a compressed response answered from the
cache, a compressed body cut short, and fetches that do not wait for the
headers of another fetch, a response that needs validation, a variant
of another request or a body too large to store, and that stop waiting
when cancelled or after a while; tests/http_cache.rs also a URL with
credentials and a fragment; tests/cache_store.rs an id reused by another
entry. The fetch tests of
patches 79, 80 and 92 are adapted to entries that appear on commit.
Validation: `./mach test-unit -p servo-net`: 435 tests pass. Negative controls, each run once: without the origin check, the most-recent selection, the 304 exception in `tee` or the suspended-producer wait, `test_invalidation_only_reaches_the_same_origin`, `test_the_most_recent_matching_response_is_used`, `test_a_304_for_another_response_makes_the_request_again_without_preconditions` and `test_fetch_waiting_for_a_suspended_response_uses_the_network` fail. Without the commit on the decoded end, the stamps and the waiting rule, `test_compressed_response_is_answered_from_cache`, `test_compressed_response_cut_short_is_not_stored`, `a_reused_id_is_not_touched_on_behalf_of_its_old_entry` (memory and disk store) and `test_fetch_does_not_wait_for_a_response_that_needs_validation` fail; with waiters waiting from the writer's registration on, `test_fetch_does_not_wait_for_the_headers_of_another_fetch` fails as well, and with waiters waiting for any response the validation one does. With patches 121 to 123: `./mach test-unit --profile checked-release` (1212 tests) and `--doc` pass; `./mach clippy --locked -- -- --deny warnings`, the crown `checked-release` build and the OHOS `checked-release` build with `RUSTFLAGS="-D warnings"` are clean, as is `./mach test-tidy`. WPT, `checked-release`, 8 processes, fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, cors, xhr, preload, html/semantics/embedded-content/media-elements and html/semantics/scripting-1, 2140 tests: 2134 as expected, and "Response with Cache-Control: max-age=2592000, public and Pragma: no-cache should not be cached" now passes (expectation removed; the crate's deviation D-2). The other six behave the same alone on ohos-main 091e100215c: src_object_blob.html times out 3 of 8 runs (2 of 8 there), load-removes-queued-error-event.html crashes 5 of 8 (4 of 8 there), media_fragment_seek.html fails and autoplay-default-permissions-policy.https.sub.html passes every time on both, and seek-to-max-value.htm and track-remove-by-setting-innerHTML.html pass 3 of 3 on both. Full WPT, `checked-release`, 12 processes, on the build before the waiting rule was fixed: 58273 of 58348 tests as expected. The other 75 were run again serially, those still unexpected on ohos-main 091e100215c too, and the 12 that differed 3 times alone on both: async_003.htm, async_004.htm and async_005.htm failed every time on the branch only (their script's headers are delayed, and a second fetch of it waited for them), and pass 3 of 3 with the fix; the other 9 behave the same on both. The focused set without scripting-1 on QuickJS (`--js-engine quickjs`), before the review fixes: 1540 of 1545 as expected, three of the same media tests, and two track-element timeouts that pass 3 of 3 alone on both builds. On ohos-main deaca97 with `http-cache-rules` 0.1.0 from crates.io (the same files as the path dependency tested above): `./mach test-unit -p servo-net` passes, 435 tests here and 472 with patch 121, and the CI-parity checks above pass again at patch 123, except `indexeddb::engines::sqlite::tests::test_async_operations` of servo-storage, which timed out once and passes 3 of 3 alone. `test_response_cache_status_is_validated` failed 5 of 320 runs, 32 at a time, while the commit of the first response was not waited for, and 0 of 960 after. WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, preload and html/semantics/scripting-1, 756 tests: as expected but for execution-timing/077.html, which passes 3 of 3 alone here and fails 1 of 3 alone on ohos-main 091e100215c, and the prefetch-document subtest above, which passed 2 of 3 runs alone; its expectation now allows both. Review fixes (the wait ends on cancellation, does not start for another variant or a body too large to store, and times out; the key leaves out the credentials and fragment of the URL): 440 tests pass here, and 481 with patch 121. Negative controls, each run once: without the cancellation of the wait, `test_a_waiting_fetch_stops_waiting_when_it_is_cancelled` fails; without the `Vary` check, the cap in `TeeBody`, the timeout or the password left out of the key, `test_fetch_does_not_wait_for_a_response_that_varies_from_its_request`, `test_fetch_does_not_wait_for_a_response_too_large_to_store`, `test_a_waiting_fetch_goes_to_the_network_after_a_while` and `test_url_credentials_and_fragment_are_not_part_of_the_key` fail. At patch 123 the CI-parity checks pass again (`test-unit --profile checked-release`: 1221 tests), and WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, preload, html/semantics/scripting-1 and html/semantics/embedded-content/media-elements, 1679 tests, run as expected but for four of the media tests above and seek-to-currentTime.html, which passes 3 of 3 alone on both builds. A first version also returned a network error from step 9 of HTTP-network-or-cache fetch for any fetch cancelled before it reached the network. PR CI caught that: requests sent while a document unloads were dropped, and fetch/fetch-later/send-on-discard/send-multiple, both sandbox-top-navigation-child-delivered tests, navigation-timing/nested-unload-timing and navigation-timing/prefetch-transfer-size-executor failed in both attempts of that CI run and in a serial local run. Without it they run as expected in 2 of 2 serial local runs; of the other stable unexpected results of that CI run, window-resize-event.html times out alone on ohos-main 091e100215c too, and test_resource_timing and media-loading-pseudo-classes-in-has.sub.html behave the same alone on both builds and are flaky upstream.
AI-assisted: Yes (agent session; the agent ported the storage half of the http-cache-v2 branch, wrote the glue over http-cache-rules and the merge with patches 79, 80 and 92, and wrote and ran the tests)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: `http_cache.rs` deleted as before, which drops upstream's edits to it (servo#48927 most recent matching response and replacement of matching responses, servo#48842); this cache uses the most recent matching response already and makes the request again when a 304 is about another response. The old cache steps of HTTP-network-or-cache fetch and `block_for_cache_ready` go as before, with upstream's edits to them (servo#48927, servo#48867); `obtain_response` keeps upstream's new `FetchParams` signature (servo#49040) and returns the body below the decoder as before; test imports merged. `./mach test-unit -p servo-net` at this commit: 440 tests pass)
AI-assisted: Yes (agent session; 2026-10-22 sync -- the fork note says why patches 79 and 80 were not dropped)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe
added a commit
to openharmony-rs/servo-ohos
that referenced
this pull request
Oct 30, 2026
The HTTP cache stores a response when its headers arrive and shares its body with the fetch that receives it. When the body failed with an error other than a cancellation, net still marked it as done, so the cache kept the truncated body as a complete response and could answer later requests with it. A cancelled body was emptied and marked as done before the response was marked as aborted, so a lookup in between could answer with an empty body, and a range cut from a stored response did not share its aborted flag. A fetch that waited for a body still being received got the incomplete body when the receiving fetch was aborted, and waited forever when that fetch ended in a network error of its own, such as a failed integrity check, because then only responses with an empty body were woken up. And the cache never removed the entries of aborted fetches, so every cancelled fetch left one more unusable entry in the list of responses for its URL; media elements cancel a fetch on every seek. Mark the response as aborted when its body fails or is cancelled, before the body is replaced. Constructing a response from the cache, or refreshing it after a 304, checks the flag while holding the body, ranges cut from a stored response share its flag, and the refresh skips aborted responses. When the receiving fetch ends, wake every fetch that waits for a complete or aborted body, whatever its status; one that found the response by a cache lookup and whose body was aborted now fetches from the network instead, or fails if it is only-if-cached. A fetch that waits for the body through the refresh of a 304 is unchanged: it still ends with an empty body when the receiving fetch is aborted. Remove the entries of aborted fetches that nobody waits for when their fetch ends and whenever a response is stored for the same URL, and the URL once it has no entries left. The fetch that receives a failed body still gets it as before, and only the cache reads the aborted flag of a response. Fork note: meant for upstream. Drop once upstream has it. Testing: unit tests that ranges cut from a stored response share its aborted flag, that revalidation skips aborted responses, and that the cache removes aborted entries when it stores a response unless a fetch waits for them; fetch tests that a truncated response is neither stored nor served, that a cancelled fetch leaves neither an entry nor its URL in the cache, that a fetch waiting for an aborted response fetches it from the network, and that one waiting for a response whose fetch failed its integrity check is woken up. `CancellationListener::cancel` is now public, and the cache has test-only counts of stored responses and of waiting fetches. Validation: `./mach test-unit -p servo-net`: 403 tests pass. Each new test fails when its part of the fix is reverted: without the shared flag, a range keeps a flag of its own; without the skip, revalidation uses the aborted response; without the network fallback, the waiting fetch delivers the empty body of the aborted one; without waking every waiting fetch, the one waiting for the response that failed its integrity check never ends; without removing the URL, a cancelled fetch leaves it in the cache; and, checked on the first revision, the truncated response is stored and served without the aborted flag on errors, the aborted entries stay without their removal when a fetch ends or when a response is stored, and a waited-for entry is removed without the check for waiting fetches. Holding the body while checking the flag closes a race that no deterministic test reproduces. Playing, reloading and playing again a 90 s, 42 MB video as in the previous patch, VmRSS after the first play / at the end: 311/378, 328/388 and 329/389 MiB with both patches, 323/384 and 314/383 with the previous one alone, no measurable difference. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` of fetch/http-cache, fetch/range, fetch/api/basic and xhr: all 619 tests as expected; html/semantics/embedded-content/media-elements as in the previous patch. AI-assisted: Yes (agent session; written and tested by the agent, reworked after a review by another agent) AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#48927 (most recent matching response, replacement of matching complete responses): `refresh` keeps upstream's `can_be_used_to_answer` lookup, which already skips aborted resources, plus this patch's aborted check under the body lock; `is_abandoned` sits next to upstream's new methods; the insert drops abandoned resources before upstream's replacement of matching complete ones. `./mach test-unit -p servo-net` at this commit: 403 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
The HTTP cache kept every stored body as a `Vec<u8>` in the net process,
shared with the response handed to the consumer, so net also kept the
decoded body of every response until its fetch ended. Its RFC 9111
rules were hand-written for RFC 7234.
The rules now come from the `http-cache-rules` crate: storability, the
stored and updated header fields, freshness (with `max-age`, `Expires`,
the heuristic, `Age` and `Date`), `Vary`, `stale-while-revalidate`,
`immutable`, the request directives, the validation request and the
merge of a 304. The crate decides and stores nothing; its requirements
are traced to the RFC text and tested, model checked and fuzzed in its
own repository. `http_cache::policy` configures it as a private cache
that stores 206 responses and their bodies encoded.
Servo keeps what belongs to the fetch layer: the cache modes, background
revalidation (only for requests with a client), invalidation after
unsafe methods, ranges, and its own refusals on top of the crate: `Vary:
*`, entries over the store's size limit, 206 without `Content-Range`,
and the storage rule for audio and video of patch 79.
Storage moves behind `CacheStore`, which streams bodies in and out:
* `TeeBody` copies each frame into the store below the content
decoder, so the stored body is what came off the wire, and the copy
is a reference count, not a second allocation. The store's bounded
queue back-pressures the network.
* A hit streams the entry through the decoder in chunks.
* `ResponseBody::Streamed` marks a body delivered to the consumer and
kept nowhere. Only subresource integrity and preloads still keep the
whole decoded body (`retains_whole_body`), so net no longer keeps
media bodies, which patch 79 had to arrange separately.
* An entry is visible only once its decoded body ended cleanly, so a
failed, cancelled or truncated body never becomes an entry, which
patch 80 had to arrange separately. The fetch commits it through a
`TeeCommit`, not `TeeBody` at the end of the network body: the
decoder stops reading at the end of a gzip or brotli stream.
* Every entry carries a random stamp, and the store opens, updates
and removes an entry only for the stamp it was looked up with, since
a later entry of the key may reuse its id.
* `http_cache::inflight` lets one fetch write a key. Once its headers
show a fresh response, others wait for it and read the committed
entry, or go to the network if it aborts or, as patch 92 requires,
while it is suspended. Before that, or if the response needs
validation, they go to the network as with the old cache, so a slow
or long-polled response holds up no other fetch. Once the whole body
has arrived, they wait for any response while the store commits it,
so a fetch right after another one finds its entry. A fetch does
not wait for a response that varies on a field in which its own
request differs, or whose body outgrows what the store accepts. It
stops waiting when it is cancelled, and goes on from there like any
cancelled fetch, and it waits 5 s at most.
* `MemoryStore` is the only store in this patch: byte-bounded, LRU,
for private browsing, temporary storage, no cache directory, and
the tests. The SQLite cache goes, and `rusqlite`, `sea-query` and
`quick_cache` leave `components/net`.
Fixes over the cache of the `http-cache-v2` branch, which this replaces:
a 304 no longer collapses repeated stored fields (a two-line CSP lost a
line) nor freshens on a weak or mismatching strong validator, `Age`
takes `Date` and the request time into account, the RFC 850 and asctime
date formats are understood, and `Vary` compares all field lines. That
branch also never stored a compressed response but stored a gzip body
cut short under clean framing, let a late 304 freshen or remove the
entry that had replaced its own, and made a fetch wait for the whole
body of another fetch's response even when it could not use it, and
wrote the URL's credentials into every entry: the key now leaves them
and the fragment out, and a stored response is served with the
request's URL. An
invalidation through `Location` or `Content-Location` reaches only URIs
of the target's origin (RFC 9111 4.4). Of several matching stored
responses the most recent is used (RFC 9111 4). When a 304 is about
another response than the stored one, the request is made again without
preconditions, and that response is stored.
Prefs: `network_http_cache_size` (an entry count) and
`network_http_disk_cache` (a path) are replaced by
`network_http_memory_cache_size` and `network_http_cache_max_entry_size`,
in bytes; `network_http_disk_cache_size` gains 0 = automatic.
`apply_algorithm_to_response` returned through `unreachable!()` when
the body was not `Done`, which a null body status makes it, and a
streamed body would reach reliably. It now returns a digest that cannot
match, so the fetch fails as `response-null-body.any.js` expects.
`preload/prefetch-document.html`: "different-site document prefetch with
'as=document' should not be consumed" now also fails at times, like its
two siblings. Servo has no HTTP cache partition (servo#33616), so the fresh
response of a cross-site prefetch answers the later navigation, unless
the navigation starts before the prefetch's response headers arrive and
goes to the network. With `network_http_cache_disabled=true` the subtest
passes and the "same origin ... should be consumed" sibling fails
instead.
Fork note: supersedes patches 79 and 80 (their cache code goes; their
fetch tests stay), and moves the cache side of patch 92 into
`http_cache::inflight`. 79 and 80 stayed on the stack at the 2026-10-22
sync: patch 92 builds on both (it calls 79's `may_store_media_response`
and sets 80's abort flag), so dropping them means reworking 92. Uses
`http-cache-rules` 0.1.0 from crates.io.
Testing: tests/http_cache.rs covers storability, Vary, freshness,
stale-while-revalidate with and without a client, partial entries and
ranges (with the five range tests of the `http-cache-range` branch),
the encoded body, the media storage rule, same-origin invalidation and
the most recent variant. tests/fetch.rs covers a 304 with another strong
validator, a 304 resetting Age, and a 304 with no-store.
tests/http_loader.rs covers a compressed response answered from the
cache, a compressed body cut short, and fetches that do not wait for the
headers of another fetch, a response that needs validation, a variant
of another request or a body too large to store, and that stop waiting
when cancelled or after a while; tests/http_cache.rs also a URL with
credentials and a fragment; tests/cache_store.rs an id reused by another
entry. The fetch tests of
patches 79, 80 and 92 are adapted to entries that appear on commit.
Validation: `./mach test-unit -p servo-net`: 435 tests pass. Negative controls, each run once: without the origin check, the most-recent selection, the 304 exception in `tee` or the suspended-producer wait, `test_invalidation_only_reaches_the_same_origin`, `test_the_most_recent_matching_response_is_used`, `test_a_304_for_another_response_makes_the_request_again_without_preconditions` and `test_fetch_waiting_for_a_suspended_response_uses_the_network` fail. Without the commit on the decoded end, the stamps and the waiting rule, `test_compressed_response_is_answered_from_cache`, `test_compressed_response_cut_short_is_not_stored`, `a_reused_id_is_not_touched_on_behalf_of_its_old_entry` (memory and disk store) and `test_fetch_does_not_wait_for_a_response_that_needs_validation` fail; with waiters waiting from the writer's registration on, `test_fetch_does_not_wait_for_the_headers_of_another_fetch` fails as well, and with waiters waiting for any response the validation one does. With patches 121 to 123: `./mach test-unit --profile checked-release` (1212 tests) and `--doc` pass; `./mach clippy --locked -- -- --deny warnings`, the crown `checked-release` build and the OHOS `checked-release` build with `RUSTFLAGS="-D warnings"` are clean, as is `./mach test-tidy`. WPT, `checked-release`, 8 processes, fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, cors, xhr, preload, html/semantics/embedded-content/media-elements and html/semantics/scripting-1, 2140 tests: 2134 as expected, and "Response with Cache-Control: max-age=2592000, public and Pragma: no-cache should not be cached" now passes (expectation removed; the crate's deviation D-2). The other six behave the same alone on ohos-main 091e100215c: src_object_blob.html times out 3 of 8 runs (2 of 8 there), load-removes-queued-error-event.html crashes 5 of 8 (4 of 8 there), media_fragment_seek.html fails and autoplay-default-permissions-policy.https.sub.html passes every time on both, and seek-to-max-value.htm and track-remove-by-setting-innerHTML.html pass 3 of 3 on both. Full WPT, `checked-release`, 12 processes, on the build before the waiting rule was fixed: 58273 of 58348 tests as expected. The other 75 were run again serially, those still unexpected on ohos-main 091e100215c too, and the 12 that differed 3 times alone on both: async_003.htm, async_004.htm and async_005.htm failed every time on the branch only (their script's headers are delayed, and a second fetch of it waited for them), and pass 3 of 3 with the fix; the other 9 behave the same on both. The focused set without scripting-1 on QuickJS (`--js-engine quickjs`), before the review fixes: 1540 of 1545 as expected, three of the same media tests, and two track-element timeouts that pass 3 of 3 alone on both builds. On ohos-main deaca97 with `http-cache-rules` 0.1.0 from crates.io (the same files as the path dependency tested above): `./mach test-unit -p servo-net` passes, 435 tests here and 472 with patch 121, and the CI-parity checks above pass again at patch 123, except `indexeddb::engines::sqlite::tests::test_async_operations` of servo-storage, which timed out once and passes 3 of 3 alone. `test_response_cache_status_is_validated` failed 5 of 320 runs, 32 at a time, while the commit of the first response was not waited for, and 0 of 960 after. WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, preload and html/semantics/scripting-1, 756 tests: as expected but for execution-timing/077.html, which passes 3 of 3 alone here and fails 1 of 3 alone on ohos-main 091e100215c, and the prefetch-document subtest above, which passed 2 of 3 runs alone; its expectation now allows both. Review fixes (the wait ends on cancellation, does not start for another variant or a body too large to store, and times out; the key leaves out the credentials and fragment of the URL): 440 tests pass here, and 481 with patch 121. Negative controls, each run once: without the cancellation of the wait, `test_a_waiting_fetch_stops_waiting_when_it_is_cancelled` fails; without the `Vary` check, the cap in `TeeBody`, the timeout or the password left out of the key, `test_fetch_does_not_wait_for_a_response_that_varies_from_its_request`, `test_fetch_does_not_wait_for_a_response_too_large_to_store`, `test_a_waiting_fetch_goes_to_the_network_after_a_while` and `test_url_credentials_and_fragment_are_not_part_of_the_key` fail. At patch 123 the CI-parity checks pass again (`test-unit --profile checked-release`: 1221 tests), and WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, preload, html/semantics/scripting-1 and html/semantics/embedded-content/media-elements, 1679 tests, run as expected but for four of the media tests above and seek-to-currentTime.html, which passes 3 of 3 alone on both builds. A first version also returned a network error from step 9 of HTTP-network-or-cache fetch for any fetch cancelled before it reached the network. PR CI caught that: requests sent while a document unloads were dropped, and fetch/fetch-later/send-on-discard/send-multiple, both sandbox-top-navigation-child-delivered tests, navigation-timing/nested-unload-timing and navigation-timing/prefetch-transfer-size-executor failed in both attempts of that CI run and in a serial local run. Without it they run as expected in 2 of 2 serial local runs; of the other stable unexpected results of that CI run, window-resize-event.html times out alone on ohos-main 091e100215c too, and test_resource_timing and media-loading-pseudo-classes-in-has.sub.html behave the same alone on both builds and are flaky upstream.
AI-assisted: Yes (agent session; the agent ported the storage half of the http-cache-v2 branch, wrote the glue over http-cache-rules and the merge with patches 79, 80 and 92, and wrote and ran the tests)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: `http_cache.rs` deleted as before, which drops upstream's edits to it (servo#48927 most recent matching response and replacement of matching responses, servo#48842); this cache uses the most recent matching response already and makes the request again when a 304 is about another response. The old cache steps of HTTP-network-or-cache fetch and `block_for_cache_ready` go as before, with upstream's edits to them (servo#48927, servo#48867); `obtain_response` keeps upstream's new `FetchParams` signature (servo#49040) and returns the body below the decoder as before; test imports merged. `./mach test-unit -p servo-net` at this commit: 440 tests pass)
AI-assisted: Yes (agent session; 2026-10-22 sync -- the fork note says why patches 79 and 80 were not dropped)
AI-assisted: Yes (agent session; 2026-10-29 sync -- test compile fix: `request_with_client()` sets the new `RequestClient::network_partition_key` (servo#49252))
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe
added a commit
to openharmony-rs/servo-ohos
that referenced
this pull request
Nov 9, 2026
The HTTP cache stores a response when its headers arrive and shares its body with the fetch that receives it. When the body failed with an error other than a cancellation, net still marked it as done, so the cache kept the truncated body as a complete response and could answer later requests with it. A cancelled body was emptied and marked as done before the response was marked as aborted, so a lookup in between could answer with an empty body, and a range cut from a stored response did not share its aborted flag. A fetch that waited for a body still being received got the incomplete body when the receiving fetch was aborted, and waited forever when that fetch ended in a network error of its own, such as a failed integrity check, because then only responses with an empty body were woken up. And the cache never removed the entries of aborted fetches, so every cancelled fetch left one more unusable entry in the list of responses for its URL; media elements cancel a fetch on every seek. Mark the response as aborted when its body fails or is cancelled, before the body is replaced. Constructing a response from the cache, or refreshing it after a 304, checks the flag while holding the body, ranges cut from a stored response share its flag, and the refresh skips aborted responses. When the receiving fetch ends, wake every fetch that waits for a complete or aborted body, whatever its status; one that found the response by a cache lookup and whose body was aborted now fetches from the network instead, or fails if it is only-if-cached. A fetch that waits for the body through the refresh of a 304 is unchanged: it still ends with an empty body when the receiving fetch is aborted. Remove the entries of aborted fetches that nobody waits for when their fetch ends and whenever a response is stored for the same URL, and the URL once it has no entries left. The fetch that receives a failed body still gets it as before, and only the cache reads the aborted flag of a response. Fork note: meant for upstream. Drop once upstream has it. Testing: unit tests that ranges cut from a stored response share its aborted flag, that revalidation skips aborted responses, and that the cache removes aborted entries when it stores a response unless a fetch waits for them; fetch tests that a truncated response is neither stored nor served, that a cancelled fetch leaves neither an entry nor its URL in the cache, that a fetch waiting for an aborted response fetches it from the network, and that one waiting for a response whose fetch failed its integrity check is woken up. `CancellationListener::cancel` is now public, and the cache has test-only counts of stored responses and of waiting fetches. Validation: `./mach test-unit -p servo-net`: 403 tests pass. Each new test fails when its part of the fix is reverted: without the shared flag, a range keeps a flag of its own; without the skip, revalidation uses the aborted response; without the network fallback, the waiting fetch delivers the empty body of the aborted one; without waking every waiting fetch, the one waiting for the response that failed its integrity check never ends; without removing the URL, a cancelled fetch leaves it in the cache; and, checked on the first revision, the truncated response is stored and served without the aborted flag on errors, the aborted entries stay without their removal when a fetch ends or when a response is stored, and a waited-for entry is removed without the check for waiting fetches. Holding the body while checking the flag closes a race that no deterministic test reproduces. Playing, reloading and playing again a 90 s, 42 MB video as in the previous patch, VmRSS after the first play / at the end: 311/378, 328/388 and 329/389 MiB with both patches, 323/384 and 314/383 with the previous one alone, no measurable difference. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` of fetch/http-cache, fetch/range, fetch/api/basic and xhr: all 619 tests as expected; html/semantics/embedded-content/media-elements as in the previous patch. AI-assisted: Yes (agent session; written and tested by the agent, reworked after a review by another agent) AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#48927 (most recent matching response, replacement of matching complete responses): `refresh` keeps upstream's `can_be_used_to_answer` lookup, which already skips aborted resources, plus this patch's aborted check under the body lock; `is_abandoned` sits next to upstream's new methods; the insert drops abandoned resources before upstream's replacement of matching complete ones. `./mach test-unit -p servo-net` at this commit: 403 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
The HTTP cache kept every stored body as a `Vec<u8>` in the net process,
shared with the response handed to the consumer, so net also kept the
decoded body of every response until its fetch ended. Its RFC 9111
rules were hand-written for RFC 7234.
The rules now come from the `http-cache-rules` crate: storability, the
stored and updated header fields, freshness (with `max-age`, `Expires`,
the heuristic, `Age` and `Date`), `Vary`, `stale-while-revalidate`,
`immutable`, the request directives, the validation request and the
merge of a 304. The crate decides and stores nothing; its requirements
are traced to the RFC text and tested, model checked and fuzzed in its
own repository. `http_cache::policy` configures it as a private cache
that stores 206 responses and their bodies encoded.
Servo keeps what belongs to the fetch layer: the cache modes, background
revalidation (only for requests with a client), invalidation after
unsafe methods, ranges, and its own refusals on top of the crate: `Vary:
*`, entries over the store's size limit, 206 without `Content-Range`,
and the storage rule for audio and video of patch 79.
Storage moves behind `CacheStore`, which streams bodies in and out:
* `TeeBody` copies each frame into the store below the content
decoder, so the stored body is what came off the wire, and the copy
is a reference count, not a second allocation. The store's bounded
queue back-pressures the network.
* A hit streams the entry through the decoder in chunks.
* `ResponseBody::Streamed` marks a body delivered to the consumer and
kept nowhere. Only subresource integrity and preloads still keep the
whole decoded body (`retains_whole_body`), so net no longer keeps
media bodies, which patch 79 had to arrange separately.
* An entry is visible only once its decoded body ended cleanly, so a
failed, cancelled or truncated body never becomes an entry, which
patch 80 had to arrange separately. The fetch commits it through a
`TeeCommit`, not `TeeBody` at the end of the network body: the
decoder stops reading at the end of a gzip or brotli stream.
* Every entry carries a random stamp, and the store opens, updates
and removes an entry only for the stamp it was looked up with, since
a later entry of the key may reuse its id.
* `http_cache::inflight` lets one fetch write a key. Once its headers
show a fresh response, others wait for it and read the committed
entry, or go to the network if it aborts or, as patch 92 requires,
while it is suspended. Before that, or if the response needs
validation, they go to the network as with the old cache, so a slow
or long-polled response holds up no other fetch. Once the whole body
has arrived, they wait for any response while the store commits it,
so a fetch right after another one finds its entry. A fetch does
not wait for a response that varies on a field in which its own
request differs, or whose body outgrows what the store accepts. It
stops waiting when it is cancelled, and goes on from there like any
cancelled fetch, and it waits 5 s at most.
* `MemoryStore` is the only store in this patch: byte-bounded, LRU,
for private browsing, temporary storage, no cache directory, and
the tests. The SQLite cache goes, and `rusqlite`, `sea-query` and
`quick_cache` leave `components/net`.
Fixes over the cache of the `http-cache-v2` branch, which this replaces:
a 304 no longer collapses repeated stored fields (a two-line CSP lost a
line) nor freshens on a weak or mismatching strong validator, `Age`
takes `Date` and the request time into account, the RFC 850 and asctime
date formats are understood, and `Vary` compares all field lines. That
branch also never stored a compressed response but stored a gzip body
cut short under clean framing, let a late 304 freshen or remove the
entry that had replaced its own, and made a fetch wait for the whole
body of another fetch's response even when it could not use it, and
wrote the URL's credentials into every entry: the key now leaves them
and the fragment out, and a stored response is served with the
request's URL. An
invalidation through `Location` or `Content-Location` reaches only URIs
of the target's origin (RFC 9111 4.4). Of several matching stored
responses the most recent is used (RFC 9111 4). When a 304 is about
another response than the stored one, the request is made again without
preconditions, and that response is stored.
Prefs: `network_http_cache_size` (an entry count) and
`network_http_disk_cache` (a path) are replaced by
`network_http_memory_cache_size` and `network_http_cache_max_entry_size`,
in bytes; `network_http_disk_cache_size` gains 0 = automatic.
`apply_algorithm_to_response` returned through `unreachable!()` when
the body was not `Done`, which a null body status makes it, and a
streamed body would reach reliably. It now returns a digest that cannot
match, so the fetch fails as `response-null-body.any.js` expects.
`preload/prefetch-document.html`: "different-site document prefetch with
'as=document' should not be consumed" now also fails at times, like its
two siblings. Servo has no HTTP cache partition (servo#33616), so the fresh
response of a cross-site prefetch answers the later navigation, unless
the navigation starts before the prefetch's response headers arrive and
goes to the network. With `network_http_cache_disabled=true` the subtest
passes and the "same origin ... should be consumed" sibling fails
instead.
Fork note: supersedes patches 79 and 80 (their cache code goes; their
fetch tests stay), and moves the cache side of patch 92 into
`http_cache::inflight`. 79 and 80 stayed on the stack at the 2026-10-22
sync: patch 92 builds on both (it calls 79's `may_store_media_response`
and sets 80's abort flag), so dropping them means reworking 92. Uses
`http-cache-rules` 0.1.0 from crates.io.
Testing: tests/http_cache.rs covers storability, Vary, freshness,
stale-while-revalidate with and without a client, partial entries and
ranges (with the five range tests of the `http-cache-range` branch),
the encoded body, the media storage rule, same-origin invalidation and
the most recent variant. tests/fetch.rs covers a 304 with another strong
validator, a 304 resetting Age, and a 304 with no-store.
tests/http_loader.rs covers a compressed response answered from the
cache, a compressed body cut short, and fetches that do not wait for the
headers of another fetch, a response that needs validation, a variant
of another request or a body too large to store, and that stop waiting
when cancelled or after a while; tests/http_cache.rs also a URL with
credentials and a fragment; tests/cache_store.rs an id reused by another
entry. The fetch tests of
patches 79, 80 and 92 are adapted to entries that appear on commit.
Validation: `./mach test-unit -p servo-net`: 435 tests pass. Negative controls, each run once: without the origin check, the most-recent selection, the 304 exception in `tee` or the suspended-producer wait, `test_invalidation_only_reaches_the_same_origin`, `test_the_most_recent_matching_response_is_used`, `test_a_304_for_another_response_makes_the_request_again_without_preconditions` and `test_fetch_waiting_for_a_suspended_response_uses_the_network` fail. Without the commit on the decoded end, the stamps and the waiting rule, `test_compressed_response_is_answered_from_cache`, `test_compressed_response_cut_short_is_not_stored`, `a_reused_id_is_not_touched_on_behalf_of_its_old_entry` (memory and disk store) and `test_fetch_does_not_wait_for_a_response_that_needs_validation` fail; with waiters waiting from the writer's registration on, `test_fetch_does_not_wait_for_the_headers_of_another_fetch` fails as well, and with waiters waiting for any response the validation one does. With patches 121 to 123: `./mach test-unit --profile checked-release` (1212 tests) and `--doc` pass; `./mach clippy --locked -- -- --deny warnings`, the crown `checked-release` build and the OHOS `checked-release` build with `RUSTFLAGS="-D warnings"` are clean, as is `./mach test-tidy`. WPT, `checked-release`, 8 processes, fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, cors, xhr, preload, html/semantics/embedded-content/media-elements and html/semantics/scripting-1, 2140 tests: 2134 as expected, and "Response with Cache-Control: max-age=2592000, public and Pragma: no-cache should not be cached" now passes (expectation removed; the crate's deviation D-2). The other six behave the same alone on ohos-main 091e100215c: src_object_blob.html times out 3 of 8 runs (2 of 8 there), load-removes-queued-error-event.html crashes 5 of 8 (4 of 8 there), media_fragment_seek.html fails and autoplay-default-permissions-policy.https.sub.html passes every time on both, and seek-to-max-value.htm and track-remove-by-setting-innerHTML.html pass 3 of 3 on both. Full WPT, `checked-release`, 12 processes, on the build before the waiting rule was fixed: 58273 of 58348 tests as expected. The other 75 were run again serially, those still unexpected on ohos-main 091e100215c too, and the 12 that differed 3 times alone on both: async_003.htm, async_004.htm and async_005.htm failed every time on the branch only (their script's headers are delayed, and a second fetch of it waited for them), and pass 3 of 3 with the fix; the other 9 behave the same on both. The focused set without scripting-1 on QuickJS (`--js-engine quickjs`), before the review fixes: 1540 of 1545 as expected, three of the same media tests, and two track-element timeouts that pass 3 of 3 alone on both builds. On ohos-main deaca97 with `http-cache-rules` 0.1.0 from crates.io (the same files as the path dependency tested above): `./mach test-unit -p servo-net` passes, 435 tests here and 472 with patch 121, and the CI-parity checks above pass again at patch 123, except `indexeddb::engines::sqlite::tests::test_async_operations` of servo-storage, which timed out once and passes 3 of 3 alone. `test_response_cache_status_is_validated` failed 5 of 320 runs, 32 at a time, while the commit of the first response was not waited for, and 0 of 960 after. WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, preload and html/semantics/scripting-1, 756 tests: as expected but for execution-timing/077.html, which passes 3 of 3 alone here and fails 1 of 3 alone on ohos-main 091e100215c, and the prefetch-document subtest above, which passed 2 of 3 runs alone; its expectation now allows both. Review fixes (the wait ends on cancellation, does not start for another variant or a body too large to store, and times out; the key leaves out the credentials and fragment of the URL): 440 tests pass here, and 481 with patch 121. Negative controls, each run once: without the cancellation of the wait, `test_a_waiting_fetch_stops_waiting_when_it_is_cancelled` fails; without the `Vary` check, the cap in `TeeBody`, the timeout or the password left out of the key, `test_fetch_does_not_wait_for_a_response_that_varies_from_its_request`, `test_fetch_does_not_wait_for_a_response_too_large_to_store`, `test_a_waiting_fetch_goes_to_the_network_after_a_while` and `test_url_credentials_and_fragment_are_not_part_of_the_key` fail. At patch 123 the CI-parity checks pass again (`test-unit --profile checked-release`: 1221 tests), and WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, preload, html/semantics/scripting-1 and html/semantics/embedded-content/media-elements, 1679 tests, run as expected but for four of the media tests above and seek-to-currentTime.html, which passes 3 of 3 alone on both builds. A first version also returned a network error from step 9 of HTTP-network-or-cache fetch for any fetch cancelled before it reached the network. PR CI caught that: requests sent while a document unloads were dropped, and fetch/fetch-later/send-on-discard/send-multiple, both sandbox-top-navigation-child-delivered tests, navigation-timing/nested-unload-timing and navigation-timing/prefetch-transfer-size-executor failed in both attempts of that CI run and in a serial local run. Without it they run as expected in 2 of 2 serial local runs; of the other stable unexpected results of that CI run, window-resize-event.html times out alone on ohos-main 091e100215c too, and test_resource_timing and media-loading-pseudo-classes-in-has.sub.html behave the same alone on both builds and are flaky upstream.
AI-assisted: Yes (agent session; the agent ported the storage half of the http-cache-v2 branch, wrote the glue over http-cache-rules and the merge with patches 79, 80 and 92, and wrote and ran the tests)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: `http_cache.rs` deleted as before, which drops upstream's edits to it (servo#48927 most recent matching response and replacement of matching responses, servo#48842); this cache uses the most recent matching response already and makes the request again when a 304 is about another response. The old cache steps of HTTP-network-or-cache fetch and `block_for_cache_ready` go as before, with upstream's edits to them (servo#48927, servo#48867); `obtain_response` keeps upstream's new `FetchParams` signature (servo#49040) and returns the body below the decoder as before; test imports merged. `./mach test-unit -p servo-net` at this commit: 440 tests pass)
AI-assisted: Yes (agent session; 2026-10-22 sync -- the fork note says why patches 79 and 80 were not dropped)
AI-assisted: Yes (agent session; 2026-10-29 sync -- test compile fix: `request_with_client()` sets the new `RequestClient::network_partition_key` (servo#49252))
AI-assisted: Yes (agent session; 2026-11-09 sync -- components/net/http_cache.rs stays deleted; upstream's cleanup of it (servo#49434) has no counterpart in the new cache)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe
added a commit
to openharmony-rs/servo-ohos
that referenced
this pull request
Nov 10, 2026
The HTTP cache stores a response when its headers arrive and shares its body with the fetch that receives it. When the body failed with an error other than a cancellation, net still marked it as done, so the cache kept the truncated body as a complete response and could answer later requests with it. A cancelled body was emptied and marked as done before the response was marked as aborted, so a lookup in between could answer with an empty body, and a range cut from a stored response did not share its aborted flag. A fetch that waited for a body still being received got the incomplete body when the receiving fetch was aborted, and waited forever when that fetch ended in a network error of its own, such as a failed integrity check, because then only responses with an empty body were woken up. And the cache never removed the entries of aborted fetches, so every cancelled fetch left one more unusable entry in the list of responses for its URL; media elements cancel a fetch on every seek. Mark the response as aborted when its body fails or is cancelled, before the body is replaced. Constructing a response from the cache, or refreshing it after a 304, checks the flag while holding the body, ranges cut from a stored response share its flag, and the refresh skips aborted responses. When the receiving fetch ends, wake every fetch that waits for a complete or aborted body, whatever its status; one that found the response by a cache lookup and whose body was aborted now fetches from the network instead, or fails if it is only-if-cached. A fetch that waits for the body through the refresh of a 304 is unchanged: it still ends with an empty body when the receiving fetch is aborted. Remove the entries of aborted fetches that nobody waits for when their fetch ends and whenever a response is stored for the same URL, and the URL once it has no entries left. The fetch that receives a failed body still gets it as before, and only the cache reads the aborted flag of a response. Fork note: meant for upstream. Drop once upstream has it. Testing: unit tests that ranges cut from a stored response share its aborted flag, that revalidation skips aborted responses, and that the cache removes aborted entries when it stores a response unless a fetch waits for them; fetch tests that a truncated response is neither stored nor served, that a cancelled fetch leaves neither an entry nor its URL in the cache, that a fetch waiting for an aborted response fetches it from the network, and that one waiting for a response whose fetch failed its integrity check is woken up. `CancellationListener::cancel` is now public, and the cache has test-only counts of stored responses and of waiting fetches. Validation: `./mach test-unit -p servo-net`: 403 tests pass. Each new test fails when its part of the fix is reverted: without the shared flag, a range keeps a flag of its own; without the skip, revalidation uses the aborted response; without the network fallback, the waiting fetch delivers the empty body of the aborted one; without waking every waiting fetch, the one waiting for the response that failed its integrity check never ends; without removing the URL, a cancelled fetch leaves it in the cache; and, checked on the first revision, the truncated response is stored and served without the aborted flag on errors, the aborted entries stay without their removal when a fetch ends or when a response is stored, and a waited-for entry is removed without the check for waiting fetches. Holding the body while checking the flag closes a race that no deterministic test reproduces. Playing, reloading and playing again a 90 s, 42 MB video as in the previous patch, VmRSS after the first play / at the end: 311/378, 328/388 and 329/389 MiB with both patches, 323/384 and 314/383 with the previous one alone, no measurable difference. `./mach test-wpt --release --processes 6 --timeout-multiplier 2` of fetch/http-cache, fetch/range, fetch/api/basic and xhr: all 619 tests as expected; html/semantics/embedded-content/media-elements as in the previous patch. AI-assisted: Yes (agent session; written and tested by the agent, reworked after a review by another agent) AI-assisted: Yes (agent session; 2026-10-22 sync -- merged with upstream servo#48927 (most recent matching response, replacement of matching complete responses): `refresh` keeps upstream's `can_be_used_to_answer` lookup, which already skips aborted resources, plus this patch's aborted check under the body lock; `is_abandoned` sits next to upstream's new methods; the insert drops abandoned resources before upstream's replacement of matching complete ones. `./mach test-unit -p servo-net` at this commit: 403 tests pass) Signed-off-by: Jonathan Schwender <[email protected]>
jschwe
added a commit
to openharmony-rs/servo-ohos
that referenced
this pull request
Nov 10, 2026
The HTTP cache kept every stored body as a `Vec<u8>` in the net process,
shared with the response handed to the consumer, so net also kept the
decoded body of every response until its fetch ended. Its RFC 9111
rules were hand-written for RFC 7234.
The rules now come from the `http-cache-rules` crate: storability, the
stored and updated header fields, freshness (with `max-age`, `Expires`,
the heuristic, `Age` and `Date`), `Vary`, `stale-while-revalidate`,
`immutable`, the request directives, the validation request and the
merge of a 304. The crate decides and stores nothing; its requirements
are traced to the RFC text and tested, model checked and fuzzed in its
own repository. `http_cache::policy` configures it as a private cache
that stores 206 responses and their bodies encoded.
Servo keeps what belongs to the fetch layer: the cache modes, background
revalidation (only for requests with a client), invalidation after
unsafe methods, ranges, and its own refusals on top of the crate: `Vary:
*`, entries over the store's size limit, 206 without `Content-Range`,
and the storage rule for audio and video of patch 79.
Storage moves behind `CacheStore`, which streams bodies in and out:
* `TeeBody` copies each frame into the store below the content
decoder, so the stored body is what came off the wire, and the copy
is a reference count, not a second allocation. The store's bounded
queue back-pressures the network.
* A hit streams the entry through the decoder in chunks.
* `ResponseBody::Streamed` marks a body delivered to the consumer and
kept nowhere. Only subresource integrity and preloads still keep the
whole decoded body (`retains_whole_body`), so net no longer keeps
media bodies, which patch 79 had to arrange separately.
* An entry is visible only once its decoded body ended cleanly, so a
failed, cancelled or truncated body never becomes an entry, which
patch 80 had to arrange separately. The fetch commits it through a
`TeeCommit`, not `TeeBody` at the end of the network body: the
decoder stops reading at the end of a gzip or brotli stream.
* Every entry carries a random stamp, and the store opens, updates
and removes an entry only for the stamp it was looked up with, since
a later entry of the key may reuse its id.
* `http_cache::inflight` lets one fetch write a key. Once its headers
show a fresh response, others wait for it and read the committed
entry, or go to the network if it aborts or, as patch 92 requires,
while it is suspended. Before that, or if the response needs
validation, they go to the network as with the old cache, so a slow
or long-polled response holds up no other fetch. Once the whole body
has arrived, they wait for any response while the store commits it,
so a fetch right after another one finds its entry. A fetch does
not wait for a response that varies on a field in which its own
request differs, or whose body outgrows what the store accepts. It
stops waiting when it is cancelled, and goes on from there like any
cancelled fetch, and it waits 5 s at most.
* `MemoryStore` is the only store in this patch: byte-bounded, LRU,
for private browsing, temporary storage, no cache directory, and
the tests. The SQLite cache goes, and `rusqlite`, `sea-query` and
`quick_cache` leave `components/net`.
Fixes over the cache of the `http-cache-v2` branch, which this replaces:
a 304 no longer collapses repeated stored fields (a two-line CSP lost a
line) nor freshens on a weak or mismatching strong validator, `Age`
takes `Date` and the request time into account, the RFC 850 and asctime
date formats are understood, and `Vary` compares all field lines. That
branch also never stored a compressed response but stored a gzip body
cut short under clean framing, let a late 304 freshen or remove the
entry that had replaced its own, and made a fetch wait for the whole
body of another fetch's response even when it could not use it, and
wrote the URL's credentials into every entry: the key now leaves them
and the fragment out, and a stored response is served with the
request's URL. An
invalidation through `Location` or `Content-Location` reaches only URIs
of the target's origin (RFC 9111 4.4). Of several matching stored
responses the most recent is used (RFC 9111 4). When a 304 is about
another response than the stored one, the request is made again without
preconditions, and that response is stored.
Prefs: `network_http_cache_size` (an entry count) and
`network_http_disk_cache` (a path) are replaced by
`network_http_memory_cache_size` and `network_http_cache_max_entry_size`,
in bytes; `network_http_disk_cache_size` gains 0 = automatic.
`apply_algorithm_to_response` returned through `unreachable!()` when
the body was not `Done`, which a null body status makes it, and a
streamed body would reach reliably. It now returns a digest that cannot
match, so the fetch fails as `response-null-body.any.js` expects.
`preload/prefetch-document.html`: "different-site document prefetch with
'as=document' should not be consumed" now also fails at times, like its
two siblings. Servo has no HTTP cache partition (servo#33616), so the fresh
response of a cross-site prefetch answers the later navigation, unless
the navigation starts before the prefetch's response headers arrive and
goes to the network. With `network_http_cache_disabled=true` the subtest
passes and the "same origin ... should be consumed" sibling fails
instead.
Fork note: supersedes patches 79 and 80 (their cache code goes; their
fetch tests stay), and moves the cache side of patch 92 into
`http_cache::inflight`. 79 and 80 stayed on the stack at the 2026-10-22
sync: patch 92 builds on both (it calls 79's `may_store_media_response`
and sets 80's abort flag), so dropping them means reworking 92. Uses
`http-cache-rules` 0.1.0 from crates.io.
Testing: tests/http_cache.rs covers storability, Vary, freshness,
stale-while-revalidate with and without a client, partial entries and
ranges (with the five range tests of the `http-cache-range` branch),
the encoded body, the media storage rule, same-origin invalidation and
the most recent variant. tests/fetch.rs covers a 304 with another strong
validator, a 304 resetting Age, and a 304 with no-store.
tests/http_loader.rs covers a compressed response answered from the
cache, a compressed body cut short, and fetches that do not wait for the
headers of another fetch, a response that needs validation, a variant
of another request or a body too large to store, and that stop waiting
when cancelled or after a while; tests/http_cache.rs also a URL with
credentials and a fragment; tests/cache_store.rs an id reused by another
entry. The fetch tests of
patches 79, 80 and 92 are adapted to entries that appear on commit.
Validation: `./mach test-unit -p servo-net`: 435 tests pass. Negative controls, each run once: without the origin check, the most-recent selection, the 304 exception in `tee` or the suspended-producer wait, `test_invalidation_only_reaches_the_same_origin`, `test_the_most_recent_matching_response_is_used`, `test_a_304_for_another_response_makes_the_request_again_without_preconditions` and `test_fetch_waiting_for_a_suspended_response_uses_the_network` fail. Without the commit on the decoded end, the stamps and the waiting rule, `test_compressed_response_is_answered_from_cache`, `test_compressed_response_cut_short_is_not_stored`, `a_reused_id_is_not_touched_on_behalf_of_its_old_entry` (memory and disk store) and `test_fetch_does_not_wait_for_a_response_that_needs_validation` fail; with waiters waiting from the writer's registration on, `test_fetch_does_not_wait_for_the_headers_of_another_fetch` fails as well, and with waiters waiting for any response the validation one does. With patches 121 to 123: `./mach test-unit --profile checked-release` (1212 tests) and `--doc` pass; `./mach clippy --locked -- -- --deny warnings`, the crown `checked-release` build and the OHOS `checked-release` build with `RUSTFLAGS="-D warnings"` are clean, as is `./mach test-tidy`. WPT, `checked-release`, 8 processes, fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, cors, xhr, preload, html/semantics/embedded-content/media-elements and html/semantics/scripting-1, 2140 tests: 2134 as expected, and "Response with Cache-Control: max-age=2592000, public and Pragma: no-cache should not be cached" now passes (expectation removed; the crate's deviation D-2). The other six behave the same alone on ohos-main 091e100215c: src_object_blob.html times out 3 of 8 runs (2 of 8 there), load-removes-queued-error-event.html crashes 5 of 8 (4 of 8 there), media_fragment_seek.html fails and autoplay-default-permissions-policy.https.sub.html passes every time on both, and seek-to-max-value.htm and track-remove-by-setting-innerHTML.html pass 3 of 3 on both. Full WPT, `checked-release`, 12 processes, on the build before the waiting rule was fixed: 58273 of 58348 tests as expected. The other 75 were run again serially, those still unexpected on ohos-main 091e100215c too, and the 12 that differed 3 times alone on both: async_003.htm, async_004.htm and async_005.htm failed every time on the branch only (their script's headers are delayed, and a second fetch of it waited for them), and pass 3 of 3 with the fix; the other 9 behave the same on both. The focused set without scripting-1 on QuickJS (`--js-engine quickjs`), before the review fixes: 1540 of 1545 as expected, three of the same media tests, and two track-element timeouts that pass 3 of 3 alone on both builds. On ohos-main deaca97 with `http-cache-rules` 0.1.0 from crates.io (the same files as the path dependency tested above): `./mach test-unit -p servo-net` passes, 435 tests here and 472 with patch 121, and the CI-parity checks above pass again at patch 123, except `indexeddb::engines::sqlite::tests::test_async_operations` of servo-storage, which timed out once and passes 3 of 3 alone. `test_response_cache_status_is_validated` failed 5 of 320 runs, 32 at a time, while the commit of the first response was not waited for, and 0 of 960 after. WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, preload and html/semantics/scripting-1, 756 tests: as expected but for execution-timing/077.html, which passes 3 of 3 alone here and fails 1 of 3 alone on ohos-main 091e100215c, and the prefetch-document subtest above, which passed 2 of 3 runs alone; its expectation now allows both. Review fixes (the wait ends on cancellation, does not start for another variant or a body too large to store, and times out; the key leaves out the credentials and fragment of the URL): 440 tests pass here, and 481 with patch 121. Negative controls, each run once: without the cancellation of the wait, `test_a_waiting_fetch_stops_waiting_when_it_is_cancelled` fails; without the `Vary` check, the cap in `TeeBody`, the timeout or the password left out of the key, `test_fetch_does_not_wait_for_a_response_that_varies_from_its_request`, `test_fetch_does_not_wait_for_a_response_too_large_to_store`, `test_a_waiting_fetch_goes_to_the_network_after_a_while` and `test_url_credentials_and_fragment_are_not_part_of_the_key` fail. At patch 123 the CI-parity checks pass again (`test-unit --profile checked-release`: 1221 tests), and WPT fetch/http-cache, fetch/range, fetch/stale-while-revalidate, fetch/api, preload, html/semantics/scripting-1 and html/semantics/embedded-content/media-elements, 1679 tests, run as expected but for four of the media tests above and seek-to-currentTime.html, which passes 3 of 3 alone on both builds. A first version also returned a network error from step 9 of HTTP-network-or-cache fetch for any fetch cancelled before it reached the network. PR CI caught that: requests sent while a document unloads were dropped, and fetch/fetch-later/send-on-discard/send-multiple, both sandbox-top-navigation-child-delivered tests, navigation-timing/nested-unload-timing and navigation-timing/prefetch-transfer-size-executor failed in both attempts of that CI run and in a serial local run. Without it they run as expected in 2 of 2 serial local runs; of the other stable unexpected results of that CI run, window-resize-event.html times out alone on ohos-main 091e100215c too, and test_resource_timing and media-loading-pseudo-classes-in-has.sub.html behave the same alone on both builds and are flaky upstream.
AI-assisted: Yes (agent session; the agent ported the storage half of the http-cache-v2 branch, wrote the glue over http-cache-rules and the merge with patches 79, 80 and 92, and wrote and ran the tests)
AI-assisted: Yes (agent session; 2026-10-22 sync -- conflict resolution: `http_cache.rs` deleted as before, which drops upstream's edits to it (servo#48927 most recent matching response and replacement of matching responses, servo#48842); this cache uses the most recent matching response already and makes the request again when a 304 is about another response. The old cache steps of HTTP-network-or-cache fetch and `block_for_cache_ready` go as before, with upstream's edits to them (servo#48927, servo#48867); `obtain_response` keeps upstream's new `FetchParams` signature (servo#49040) and returns the body below the decoder as before; test imports merged. `./mach test-unit -p servo-net` at this commit: 440 tests pass)
AI-assisted: Yes (agent session; 2026-10-22 sync -- the fork note says why patches 79 and 80 were not dropped)
AI-assisted: Yes (agent session; 2026-10-29 sync -- test compile fix: `request_with_client()` sets the new `RequestClient::network_partition_key` (servo#49252))
AI-assisted: Yes (agent session; 2026-11-09 sync -- components/net/http_cache.rs stays deleted; upstream's cleanup of it (servo#49434) has no counterpart in the new cache)
Signed-off-by: Jonathan Schwender <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change fixes issues with cache revalidation where an older response
would be returned from the cache. Now when a new response comes in, it
replaces all matching older ones using the same logic that is used to
find candidate cached resources during load. In addition, when there is
more than one matching resource, the newest matching is returned.
Testing: This change adds a new WPT test.
Fixes: #24385.
Closes: #24388.