Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bradfitz opened this issue Dec 8, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@bradfitz
Copy link
Member

commented Dec 8, 2016

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

@bradfitz bradfitz added the NeedsFix label Dec 8, 2016

@bradfitz bradfitz added this to the Go1.8 milestone Dec 8, 2016

@bradfitz bradfitz self-assigned this Dec 8, 2016

@cpuguy83

This comment has been minimized.

Copy link

commented Dec 8, 2016

Thanks for opening, was going to do this once the kids went to bed!

@gopherbot

This comment has been minimized.

Copy link

commented Dec 8, 2016

CL https://golang.org/cl/34134 mentions this issue.

@cpuguy83

This comment has been minimized.

Copy link

commented Dec 8, 2016

Confirmed the changes in the CL fix our issue.

@gopherbot gopherbot closed this in 9296d4e Dec 8, 2016

@cpuguy83

This comment has been minimized.

Copy link

commented Dec 8, 2016

🎉

@golang golang locked and limited conversation to collaborators Dec 8, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.