Skip to content

net: Avoid cloning headers twice when filtering response - #49313

Merged
Narfinger merged 1 commit into
servo:mainfrom
yezhizhen:net
Oct 28, 2026
Merged

Narfinger merged 1 commit into
servo:mainfrom
yezhizhen:net

Conversation

@yezhizhen

Copy link
Copy Markdown
Member

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.

Signed-off-by: Euclid Ye <[email protected]>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label 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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I still see clone, what did you improve here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@yezhizhen
yezhizhen requested review from Taym95 and kkoyung October 27, 2026 12:38

@Narfinger Narfinger 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.

I think it might be good to have a comment in the code that explains that the headers in in response all get replaced.

@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 28, 2026
@yezhizhen

yezhizhen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member Author

I think it might be good to have a comment in the code that explains that the headers in in response all get replaced.

It is already there right? I think that comment was written in my previous PR.
// This is ok as we always set the response.headers in all branches in the following.

@Narfinger
Narfinger added this pull request to the merge queue Oct 28, 2026
@servo-highfive servo-highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Oct 28, 2026
Merged via the queue into servo:main with commit daaf169 Oct 28, 2026
41 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 28, 2026
@yezhizhen
yezhizhen deleted the net branch October 28, 2026 18:28
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants