Skip to content

net/http: Transport.RoundTrip regression when retrying requests on Request.Body read error #18239

@bradfitz

Description

@bradfitz

Bug report from Docker:

https://twitter.com/cpuguy83/status/806263794368397312 from moby/moby#29030

Repro from the change where I'm working on a fix:

func TestTransportBodyReadError(t *testing.T) {  
        defer afterTest(t)  
        ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {  
                if r.URL.Path == "/ping" {  
                        return  
                }  
                buf := make([]byte, 1)  
                n, err := r.Body.Read(buf)  
                w.Header().Set("X-Body-Read", fmt.Sprintf("%v, %v", n, err))  
        }))  
        defer ts.Close()  
        tr := &Transport{}  
        defer tr.CloseIdleConnections()  
        c := &Client{Transport: tr}  
  
        res, err := c.Get(ts.URL + "/ping")  
        if err != nil {  
                t.Fatal(err)  
        }  
        res.Body.Close()  
  
        r, w := io.Pipe()  
        errClose := errors.New("some error")  
        w.CloseWithError(errClose)  
        if _, err := c.Post(ts.URL+"/test", "", r); err != nil {  
                t.Logf("Got error: %v", err)  
        }  
}  

Go 1.7 always says "some error" as the error. In Go 1.8, we read from the request body, get an error, and then try to send the request again since we never wrote anything. But the first write ends up closing the request body (the pipe), and it can't be read the second time.

We need to do the same thing @tombergan and I did for http2 in golang/net@8dab929 and not retry a request once its Body has been messed with, unless it has a GetBody func that can be used to reset itself.

/cc @cpuguy83

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions