Skip to content

Commit 5d6afe9

Browse files
fraenkelbradfitz
authored andcommitted
http2: send a nil error if we cancel a delayed body write
Once a request body is scheduled to be written, a result of the write is always expected. If the body writer is cancelled, and the write was never started, send a successful result. The test included is a modified version of the TestNoSniffExpectRequestBody_h2 found in net/http. Updates golang/go#42498 Change-Id: If3f23993170bdf10e9ae4244ec13ae269bd3877a Reviewed-on: https://go-review.googlesource.com/c/net/+/269058 Trust: Dmitri Shuralyov <[email protected]> Trust: Brad Fitzpatrick <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 69a7880 commit 5d6afe9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

http2/transport.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,9 @@ func (t *Transport) getBodyWriterState(cs *clientStream, body io.Reader) (s body
26322632

26332633
func (s bodyWriterState) cancel() {
26342634
if s.timer != nil {
2635-
s.timer.Stop()
2635+
if s.timer.Stop() {
2636+
s.resc <- nil
2637+
}
26362638
}
26372639
}
26382640

http2/transport_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4882,3 +4882,39 @@ func TestTransportBodyRewindRace(t *testing.T) {
48824882

48834883
wg.Wait()
48844884
}
4885+
4886+
// Issue 42498: A request with a body will never be sent if the stream is
4887+
// reset prior to sending any data.
4888+
func TestTransportServerResetStreamAtHeaders(t *testing.T) {
4889+
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
4890+
w.WriteHeader(http.StatusUnauthorized)
4891+
return
4892+
}, optOnlyServer)
4893+
defer st.Close()
4894+
4895+
tr := &http.Transport{
4896+
TLSClientConfig: tlsConfigInsecure,
4897+
MaxConnsPerHost: 1,
4898+
ExpectContinueTimeout: 10 * time.Second,
4899+
}
4900+
4901+
err := ConfigureTransport(tr)
4902+
if err != nil {
4903+
t.Fatal(err)
4904+
}
4905+
client := &http.Client{
4906+
Transport: tr,
4907+
}
4908+
4909+
req, err := http.NewRequest("POST", st.ts.URL, errorReader{io.EOF})
4910+
if err != nil {
4911+
t.Fatalf("unexpect new request error: %v", err)
4912+
}
4913+
req.ContentLength = 0 // so transport is tempted to sniff it
4914+
req.Header.Set("Expect", "100-continue")
4915+
res, err := client.Do(req)
4916+
if err != nil {
4917+
t.Fatal(err)
4918+
}
4919+
res.Body.Close()
4920+
}

0 commit comments

Comments
 (0)