Repository navigation
net: Avoid cloning headers twice when filtering response - #49313
Merged
Merged
Conversation
Signed-off-by: Euclid Ye <[email protected]>
Taym95
reviewed
Oct 27, 2026
| let old_headers = std::mem::take(&mut response.headers); | ||
| response.internal_response = Some(Box::new(old_response)); | ||
| let old_headers = std::mem::take(&mut old_response.headers); | ||
| let mut response = old_response.clone(); |
Member
There was a problem hiding this comment.
I still see clone, what did you improve here?
Member
Author
There was a problem hiding this comment.
The point is elaborated in PR description. Earlier we would clone a vector of Headers. Now we drain that field before cloning the response, so we are not cloning that vector.
Narfinger
requested changes
Oct 28, 2026
Narfinger
left a comment
Contributor
There was a problem hiding this comment.
I think it might be good to have a comment in the code that explains that the headers in in response all get replaced.
Member
Author
It is already there right? I think that comment was written in my previous PR. |
Narfinger
approved these changes
Oct 28, 2026
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]>
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.
We can drain the headers in response before cloning, and then move it back in the end, since we've already been crafting new headers in every branch later with old ones discarded earlier.
Testing: It builds.