Repository navigation
net: Clear relevant CORS cache entries on network error - #49055
Conversation
|
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 I had to move |
|
🤖 Opened new upstream WPT pull request (web-platform-tests/wpt#63891) with upstreamable changes. |
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
|
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 servo/components/net/tests/main.rs Line 176 in 22b97d2 |
Okay, lemme try that |
1f5713a to
c13dfd3
Compare
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
|
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. |
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
1 similar comment
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
TimvdLippe
left a comment
There was a problem hiding this comment.
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.
567fd29 to
3ce9176
Compare
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
Checked wpt.fyi — Firefox passes our new test, but Chrome 158 and Safari 254 both fail with Should we file a spec issue, or should we really look into what they're doing? |
3ce9176 to
20d584f
Compare
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
|
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! |
|
✍ Updated existing upstream WPT pull request (web-platform-tests/wpt#63891) title and body. |
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
|
This needs a rebase on main to resolve the merge conflicts. Then it is ready to go : 🎉 |
Signed-off-by: Sabb <[email protected]>
06a7872 to
71a162a
Compare
|
📝 Transplanted new upstreamable changes to existing upstream WPT pull request (web-platform-tests/wpt#63891). |
|
It's really insightful working on this one. Learnt a lot, thank you everyone who commented, respect. |
|
⛔ Failed to properly merge the upstream pull request (web-platform-tests/wpt#63891). Please address any CI issues and try to merge manually. |
|
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? |
|
The new one in |
Oh ok, I'll look into it. |
|
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; servo/components/net/fetch/cors_cache.rs Line 192 in e78fc91 and here; servo/components/net/http_loader.rs Line 2184 in e78fc91 Should I open a new issue to address this? |
|
Yes please, thank you! |
Done, #49228 |
|
Opened the PR at #49252 |
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]>
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]>
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]>
Cleared matching entries in CORS preflight cache when
corsWithPreflightResponseis a network error.Stored
cors_cache: RwLock<CorsCache>onHttpStateso preflight cache entries persist across separatefetch()calls in the session.Then added WPT test in
cors-preflight-cache.any.jsto 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