Skip to content

Response.clone() does not properly clone the underlying streams #36503

@sebsebmc

Description

@sebsebmc

if let Some(stream) = self.body_stream.get().clone() {
simply clones the body_stream but the stream clone steps require teeing the body stream. https://fetch.spec.whatwg.org/#concept-body-clone I attempted to make this change

diff --git a/components/script/dom/response.rs b/components/script/dom/response.rs
index 283b7d615a..a76076a780 100644
--- a/components/script/dom/response.rs
+++ b/components/script/dom/response.rs
@@ -341,8 +341,13 @@ impl ResponseMethods<crate::DomTypeHolder> for Response {
             .borrow_mut()
             .clone_from(&self.url_list.borrow());
 
-        if let Some(stream) = self.body_stream.get().clone() {
-            new_response.body_stream.set(Some(&*stream));
+        // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body.
+        if let Some(stream) = self.body_stream.get() {
+            // Let out1, out2 be the result of teeing body’s stream.
+            let teed_streams = stream.tee(true, can_gc)?;
+
+            self.body_stream.set(Some(&*teed_streams[0]));
+            new_response.body_stream.set(Some(&*teed_streams[1]));
         }

and this resulted in a new pass in /fetch/api/response/response-clone.any.html but also caused most of the tests to timeout. I was not able to figure out why this causes a hang.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions