Skip to content

net: Clear relevant CORS cache entries on network error - #49055

Merged
TimvdLippe merged 1 commit into
servo:mainfrom
sabbCodes:issue-48970
Oct 23, 2026
Merged

TimvdLippe merged 1 commit into
servo:mainfrom
sabbCodes:issue-48970

Conversation

@sabbCodes

@sabbCodes sabbCodes commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Cleared matching entries in CORS preflight cache when corsWithPreflightResponse is a network error.
Stored cors_cache: RwLock<CorsCache> on HttpState so preflight cache entries persist across separate fetch() calls in the session.
Then added WPT test in cors-preflight-cache.any.js to verify fixes.

Testing: ./mach test-unit -p servo-net - (392/392 passed), ./mach test-wpt tests/wpt/tests/fetch/api/cors/cors-preflight-cache.any.js - (8/8 checks passed) & ./mach test-tidy - (passed)

Fixes: #48970
Supersedes and closes #49054

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 19, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

When I added the WPT test, it failed because request 2 always sent an OPTIONS preflight rather than reusing the cache, I found out that methods::fetch was instantiating a fresh, temporary &mut CorsCache::default() on every single fetch() call.

I had to move CorsCache into HttpState, so we now have a proper session-wide preflight cache. Now, even the existing WPT test also turned from FAIL to PASS.

@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

🤖 Opened new upstream WPT pull request (web-platform-tests/wpt#63891) with upstreamable changes.

@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@TimvdLippe

Copy link
Copy Markdown
Contributor

Ah this appears to be clashing with #49054 I don't mind closing my PR, but I do think it would make sense to remove fetch_with_cors_cache and instead pass the CorsCache to fetch. Since fetch_with_cors_cache is only called in unit tests, we can do the setup that you have in

fn fetch_with_cors_cache(request: Request, cache: &mut CorsCache) -> Response {

@sabbCodes

Copy link
Copy Markdown
Contributor Author

Ah this appears to be clashing with #49054 I don't mind closing my PR, but I do think it would make sense to remove fetch_with_cors_cache and instead pass the CorsCache to fetch. Since fetch_with_cors_cache is only called in unit tests, we can do the setup that you have in

fn fetch_with_cors_cache(request: Request, cache: &mut CorsCache) -> Response {

Okay, lemme try that

@servo-highfive servo-highfive added the S-needs-rebase There are merge conflict errors. label Oct 19, 2026
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@servo-highfive servo-highfive removed the S-needs-rebase There are merge conflict errors. label Oct 19, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

Hi @TimvdLippe I accepted my own changes from the conflict since I worked on it on a broader perspective.

But I refactored it as you requested.

@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

1 similar comment
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only a couple of nits regarding style and how you resolved the merge conflict. Rest LGTM thank you for tackling this and the extensive new test!

That said, seems like Safari fails the test as written: https://wpt.fyi/results/fetch/api/cors/cors-preflight-cache.any.html?label=pr_head&pr=63891 Can you test in Chrome and Firefox what the status is and confirm that these would pass? If not, we would need to comply with what they are doing, or file a spec issue to discuss the resolution.

Comment thread components/net/fetch/cors_cache.rs Resolved
Comment thread components/net/fetch/methods.rs Resolved
Comment thread components/net/fetch/methods.rs Resolved
Comment thread components/net/fetch/methods.rs Resolved
Comment thread components/net/fetch/methods.rs Resolved
Comment thread components/net/fetch/methods.rs Resolved
Comment thread components/net/tests/main.rs Resolved
Comment thread components/net/http_loader.rs Resolved
Comment thread components/net/http_loader.rs Resolved
@servo-highfive servo-highfive added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Oct 20, 2026
@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Oct 21, 2026
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@sabbCodes

Copy link
Copy Markdown
Contributor Author

That said, seems like Safari fails the test as written: https://wpt.fyi/results/fetch/api/cors/cors-preflight-cache.any.html?label=pr_head&pr=63891 Can you test in Chrome and Firefox what the status is and confirm that these would pass? If not, we would need to comply with what they are doing, or file a spec issue to discuss the resolution.

Checked wpt.fyi — Firefox passes our new test, but Chrome 158 and Safari 254 both fail with !EQ("2", "1"). I'm thinking since we followed the WHATWG spec, maybe it's chrome and safari that's not implemented theirs based on the specs.

Should we file a spec issue, or should we really look into what they're doing?

@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@TimvdLippe

Copy link
Copy Markdown
Contributor

Looked into it and the original spec has been there for 8 years. Probably nobody actually wrote the test and noticed browsers don't comply. Given that Firefox does pass the test and the spec has not been contented (I searched a lot through fetch spec issues and found nothing), let's add the test as-is. Thanks for writing it!

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Oct 21, 2026
@TimvdLippe
TimvdLippe added this pull request to the merge queue Oct 21, 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 21, 2026
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

✍ Updated existing upstream WPT pull request (web-platform-tests/wpt#63891) title and body.

@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. S-needs-rebase There are merge conflict errors. and removed S-tests-failed The changes caused existing tests to fail. labels Oct 23, 2026
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@TimvdLippe

Copy link
Copy Markdown
Contributor

This needs a rebase on main to resolve the merge conflicts. Then it is ready to go : 🎉

@servo-highfive servo-highfive removed the S-needs-rebase There are merge conflict errors. label Oct 23, 2026
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891).

@TimvdLippe
TimvdLippe enabled auto-merge October 23, 2026 19:33
@TimvdLippe
TimvdLippe added this pull request to the merge queue Oct 23, 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 23, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

It's really insightful working on this one.

Learnt a lot, thank you everyone who commented, respect.

Merged via the queue into servo:main with commit e933f9e Oct 23, 2026
35 checks passed
@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 23, 2026
@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

⛔ Failed to properly merge the upstream pull request (web-platform-tests/wpt#63891). Please address any CI issues and try to merge manually.

@TimvdLippe

Copy link
Copy Markdown
Contributor

Thanks a lot for tackling this! It would be lovely if you could also investigate that new test failure if you are up for the challenge. Would love to review another PR of yours.

@sabbCodes

Copy link
Copy Markdown
Contributor Author

Thanks a lot for tackling this! It would be lovely if you could also investigate that new test failure if you are up for the challenge. Would love to review another PR of yours.

Thank you.

Which test failure are you talking about, please?
There were many failures we had to tackle on this.

@TimvdLippe

Copy link
Copy Markdown
Contributor

The new one in /cors/preflight-cache-partitioning.sub.window.html

@sabbCodes

Copy link
Copy Markdown
Contributor Author

The new one in /cors/preflight-cache-partitioning.sub.window.html

Oh ok, I'll look into it.

@sabbCodes

Copy link
Copy Markdown
Contributor Author

If you don't mind, here are some of my other PRs that have yet to be merged; #49199 #48979 #48909

Looks like everyone is busy, lol.

@sabbCodes

Copy link
Copy Markdown
Contributor Author

Hi @TimvdLippe I think I now understand the reason for the failure, I think the error happens because the fix lacks network partitioning which is causing the second test window hit the preflight entry cached by the first window and skip the preflight.

I also realise it's actually already in TODO to implement network partitioning like commented here;

// > whose key equals the result of determining the network partition key

and here;

// TODO Step 5. Let networkPartitionKey be the result of determining the network partition key

Should I open a new issue to address this?

@TimvdLippe

Copy link
Copy Markdown
Contributor

Yes please, thank you!

@sabbCodes

Copy link
Copy Markdown
Contributor Author

Yes please, thank you!

Done, #49228

@sabbCodes

Copy link
Copy Markdown
Contributor Author

Opened the PR at #49252

jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 30, 2026
A response body was held twice: as a state in a shared
`Arc<Mutex<ResponseBody>>`, and as a `done_chan` that every fetch function
passed along and had to reset whenever it replaced the response. Keeping
the two paired by convention went wrong in several ways:

- A fetch cancelled right after its headers returned a network error while
  `done_chan` still held a sender, so `wait_for_response` waited forever
  and the fetch never ended.
- A response replaced by a network error, after a failed CORS check or a
  CORP, CSP, nosniff, MIME type or mixed content block, kept its channel,
  so its body was still delivered to the consumer.
- Step 22 of main fetch (null body status, HEAD) blanked the state, but not
  the chunks already queued in the channel.
- A navigation through a `web+` protocol handler returned a response whose
  body an inner fetch had already handed to `DiscardFetch`.
- Producers had to know whether a consumer inside net (subresource
  integrity, preloads) needed the whole body, and `file:` and `blob:`
  bodies were kept whole in net in any case. An in-memory `blob:` body was
  never kept, so its integrity check always failed.

`ResponseBody` is now `Null`, `Bytes`, or a `Stream` of the body stream
type added in the previous patch, and the response owns it.
`wait_for_response` is the one place that hands a body over, and it keeps
the body as `Bytes` for preloads and for subresource integrity. A body whose
producer goes away without finishing ends as failed instead of hanging. The
body of a redirect, or of a response replaced after a 401 or 407, is
discarded instead of being queued while the next request runs; readers of
files and of the cache stop when nobody reads their body.
`ProtocolHandler::load` loses its `done_chan` parameter, and
`servo::protocol_handler` exports the body stream. `Response` is no longer
`Clone`: `try_clone` copies a response whose body is not a stream. The page
content protocol handler runs main fetch recursively, so the outer fetch
hands the handler URL's body over.

Otherwise behaviour does not change: a body cut short by the connection
still ends as aborted with a successful end of file, a discarded network
body is still read to its end so that its connection can be reused and the
cache can store it, and subresource integrity still forwards the body
before checking it. One exception: a preload whose body is cut short is
now kept without a body instead of with the bytes that arrived; both are
wrong, and a later patch makes it a network error.

Validation: `./mach test-unit -p servo-net`: 490 tests pass, 7 of them new. On ohos-main 67173a4, `test_fetch_cancelled_while_its_headers_are_held_ends` fails with "the fetch did not end", `test_body_of_a_response_that_fails_the_cors_check_is_not_delivered` with all 19 body bytes delivered, `test_body_of_a_response_blocked_by_nosniff_is_not_delivered` with all 12, and `test_navigation_through_a_page_content_protocol_handler_delivers_the_body` with none. Negative controls on this patch: without applying step 22's null body, `test_body_of_a_null_body_status_is_not_delivered` gets 7 bytes; with a test producer that keeps its sender alive, `test_body_whose_producer_stops_without_finishing_ends_the_fetch` does not end; without keeping preloaded bodies, `test_fetch_of_a_preload_keeps_its_body` finds no body. `./mach check`, `./mach clippy --locked -- -- --deny warnings` and `./mach test-tidy` pass. 2026-10-29 sync (CORS cache now in the fetch context): `./mach test-unit --profile checked-release -p servo-net` at this commit, 490 tests pass.
AI-assisted: Yes (agent session; the agent wrote this patch and its tests following docs/fetch-body-stream-plan-2026-10-22.md in the workspace, found the cancellation hang, the delivery of blocked bodies and the protocol handler regression, and applied an independent review's findings)
AI-assisted: Yes (agent session; 2026-10-29 sync -- conflict resolution, no semantic change: upstream moved the CORS preflight cache into the fetch context (servo#49055, servo#49252), so `main_fetch`, `scheme_fetch`, `http_fetch` and `http_redirect_fetch` lose both the cache and `done_chan`, and the page content protocol handler's inner main fetch no longer passes a fresh cache; the preflight fetch keeps upstream's devtools reporting (servo#49151); `to_filtered` takes the headers out of the internal response before its body-less copy, as upstream now does (servo#49313))
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 response body was held twice: as a state in a shared
`Arc<Mutex<ResponseBody>>`, and as a `done_chan` that every fetch function
passed along and had to reset whenever it replaced the response. Keeping
the two paired by convention went wrong in several ways:

- A fetch cancelled right after its headers returned a network error while
  `done_chan` still held a sender, so `wait_for_response` waited forever
  and the fetch never ended.
- A response replaced by a network error, after a failed CORS check or a
  CORP, CSP, nosniff, MIME type or mixed content block, kept its channel,
  so its body was still delivered to the consumer.
- Step 22 of main fetch (null body status, HEAD) blanked the state, but not
  the chunks already queued in the channel.
- A navigation through a `web+` protocol handler returned a response whose
  body an inner fetch had already handed to `DiscardFetch`.
- Producers had to know whether a consumer inside net (subresource
  integrity, preloads) needed the whole body, and `file:` and `blob:`
  bodies were kept whole in net in any case. An in-memory `blob:` body was
  never kept, so its integrity check always failed.

`ResponseBody` is now `Null`, `Bytes`, or a `Stream` of the body stream
type added in the previous patch, and the response owns it.
`wait_for_response` is the one place that hands a body over, and it keeps
the body as `Bytes` for preloads and for subresource integrity. A body whose
producer goes away without finishing ends as failed instead of hanging. The
body of a redirect, or of a response replaced after a 401 or 407, is
discarded instead of being queued while the next request runs; readers of
files and of the cache stop when nobody reads their body.
`ProtocolHandler::load` loses its `done_chan` parameter, and
`servo::protocol_handler` exports the body stream. `Response` is no longer
`Clone`: `try_clone` copies a response whose body is not a stream. The page
content protocol handler runs main fetch recursively, so the outer fetch
hands the handler URL's body over.

Otherwise behaviour does not change: a body cut short by the connection
still ends as aborted with a successful end of file, a discarded network
body is still read to its end so that its connection can be reused and the
cache can store it, and subresource integrity still forwards the body
before checking it. One exception: a preload whose body is cut short is
now kept without a body instead of with the bytes that arrived; both are
wrong, and a later patch makes it a network error.

Validation: `./mach test-unit -p servo-net`: 490 tests pass, 7 of them new. On ohos-main 67173a4, `test_fetch_cancelled_while_its_headers_are_held_ends` fails with "the fetch did not end", `test_body_of_a_response_that_fails_the_cors_check_is_not_delivered` with all 19 body bytes delivered, `test_body_of_a_response_blocked_by_nosniff_is_not_delivered` with all 12, and `test_navigation_through_a_page_content_protocol_handler_delivers_the_body` with none. Negative controls on this patch: without applying step 22's null body, `test_body_of_a_null_body_status_is_not_delivered` gets 7 bytes; with a test producer that keeps its sender alive, `test_body_whose_producer_stops_without_finishing_ends_the_fetch` does not end; without keeping preloaded bodies, `test_fetch_of_a_preload_keeps_its_body` finds no body. `./mach check`, `./mach clippy --locked -- -- --deny warnings` and `./mach test-tidy` pass. 2026-10-29 sync (CORS cache now in the fetch context): `./mach test-unit --profile checked-release -p servo-net` at this commit, 490 tests pass.
AI-assisted: Yes (agent session; the agent wrote this patch and its tests following docs/fetch-body-stream-plan-2026-10-22.md in the workspace, found the cancellation hang, the delivery of blocked bodies and the protocol handler regression, and applied an independent review's findings)
AI-assisted: Yes (agent session; 2026-10-29 sync -- conflict resolution, no semantic change: upstream moved the CORS preflight cache into the fetch context (servo#49055, servo#49252), so `main_fetch`, `scheme_fetch`, `http_fetch` and `http_redirect_fetch` lose both the cache and `done_chan`, and the page content protocol handler's inner main fetch no longer passes a fresh cache; the preflight fetch keeps upstream's devtools reporting (servo#49151); `to_filtered` takes the headers out of the internal response before its body-less copy, as upstream now does (servo#49313))
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 response body was held twice: as a state in a shared
`Arc<Mutex<ResponseBody>>`, and as a `done_chan` that every fetch function
passed along and had to reset whenever it replaced the response. Keeping
the two paired by convention went wrong in several ways:

- A fetch cancelled right after its headers returned a network error while
  `done_chan` still held a sender, so `wait_for_response` waited forever
  and the fetch never ended.
- A response replaced by a network error, after a failed CORS check or a
  CORP, CSP, nosniff, MIME type or mixed content block, kept its channel,
  so its body was still delivered to the consumer.
- Step 22 of main fetch (null body status, HEAD) blanked the state, but not
  the chunks already queued in the channel.
- A navigation through a `web+` protocol handler returned a response whose
  body an inner fetch had already handed to `DiscardFetch`.
- Producers had to know whether a consumer inside net (subresource
  integrity, preloads) needed the whole body, and `file:` and `blob:`
  bodies were kept whole in net in any case. An in-memory `blob:` body was
  never kept, so its integrity check always failed.

`ResponseBody` is now `Null`, `Bytes`, or a `Stream` of the body stream
type added in the previous patch, and the response owns it.
`wait_for_response` is the one place that hands a body over, and it keeps
the body as `Bytes` for preloads and for subresource integrity. A body whose
producer goes away without finishing ends as failed instead of hanging. The
body of a redirect, or of a response replaced after a 401 or 407, is
discarded instead of being queued while the next request runs; readers of
files and of the cache stop when nobody reads their body.
`ProtocolHandler::load` loses its `done_chan` parameter, and
`servo::protocol_handler` exports the body stream. `Response` is no longer
`Clone`: `try_clone` copies a response whose body is not a stream. The page
content protocol handler runs main fetch recursively, so the outer fetch
hands the handler URL's body over.

Otherwise behaviour does not change: a body cut short by the connection
still ends as aborted with a successful end of file, a discarded network
body is still read to its end so that its connection can be reused and the
cache can store it, and subresource integrity still forwards the body
before checking it. One exception: a preload whose body is cut short is
now kept without a body instead of with the bytes that arrived; both are
wrong, and a later patch makes it a network error.

Validation: `./mach test-unit -p servo-net`: 490 tests pass, 7 of them new. On ohos-main 67173a4, `test_fetch_cancelled_while_its_headers_are_held_ends` fails with "the fetch did not end", `test_body_of_a_response_that_fails_the_cors_check_is_not_delivered` with all 19 body bytes delivered, `test_body_of_a_response_blocked_by_nosniff_is_not_delivered` with all 12, and `test_navigation_through_a_page_content_protocol_handler_delivers_the_body` with none. Negative controls on this patch: without applying step 22's null body, `test_body_of_a_null_body_status_is_not_delivered` gets 7 bytes; with a test producer that keeps its sender alive, `test_body_whose_producer_stops_without_finishing_ends_the_fetch` does not end; without keeping preloaded bodies, `test_fetch_of_a_preload_keeps_its_body` finds no body. `./mach check`, `./mach clippy --locked -- -- --deny warnings` and `./mach test-tidy` pass. 2026-10-29 sync (CORS cache now in the fetch context): `./mach test-unit --profile checked-release -p servo-net` at this commit, 490 tests pass.
AI-assisted: Yes (agent session; the agent wrote this patch and its tests following docs/fetch-body-stream-plan-2026-10-22.md in the workspace, found the cancellation hang, the delivery of blocked bodies and the protocol handler regression, and applied an independent review's findings)
AI-assisted: Yes (agent session; 2026-10-29 sync -- conflict resolution, no semantic change: upstream moved the CORS preflight cache into the fetch context (servo#49055, servo#49252), so `main_fetch`, `scheme_fetch`, `http_fetch` and `http_redirect_fetch` lose both the cache and `done_chan`, and the page content protocol handler's inner main fetch no longer passes a fresh cache; the preflight fetch keeps upstream's devtools reporting (servo#49151); `to_filtered` takes the headers out of the internal response before its body-less copy, as upstream now does (servo#49313))
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.

Clear relevant CORS cache entries on network error

5 participants