Skip to content

Commit 16c2bbf

Browse files
fraenkeldmitshur
authored andcommitted
[release-branch.go1.15-bundle] 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#42539 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]> (cherry picked from commit 5d6afe9) Reviewed-on: https://go-review.googlesource.com/c/net/+/288013 Trust: Damien Neil <[email protected]> Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 2ac096c commit 16c2bbf

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

http2/transport.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2566,7 +2566,9 @@ func (t *Transport) getBodyWriterState(cs *clientStream, body io.Reader) (s body
25662566

25672567
func (s bodyWriterState) cancel() {
25682568
if s.timer != nil {
2569-
s.timer.Stop()
2569+
if s.timer.Stop() {
2570+
s.resc <- nil
2571+
}
25702572
}
25712573
}
25722574

http2/transport_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -4675,3 +4675,39 @@ func TestTransportBodyRewindRace(t *testing.T) {
46754675

46764676
wg.Wait()
46774677
}
4678+
4679+
// Issue 42498: A request with a body will never be sent if the stream is
4680+
// reset prior to sending any data.
4681+
func TestTransportServerResetStreamAtHeaders(t *testing.T) {
4682+
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
4683+
w.WriteHeader(http.StatusUnauthorized)
4684+
return
4685+
}, optOnlyServer)
4686+
defer st.Close()
4687+
4688+
tr := &http.Transport{
4689+
TLSClientConfig: tlsConfigInsecure,
4690+
MaxConnsPerHost: 1,
4691+
ExpectContinueTimeout: 10 * time.Second,
4692+
}
4693+
4694+
err := ConfigureTransport(tr)
4695+
if err != nil {
4696+
t.Fatal(err)
4697+
}
4698+
client := &http.Client{
4699+
Transport: tr,
4700+
}
4701+
4702+
req, err := http.NewRequest("POST", st.ts.URL, errorReader{io.EOF})
4703+
if err != nil {
4704+
t.Fatalf("unexpect new request error: %v", err)
4705+
}
4706+
req.ContentLength = 0 // so transport is tempted to sniff it
4707+
req.Header.Set("Expect", "100-continue")
4708+
res, err := client.Do(req)
4709+
if err != nil {
4710+
t.Fatal(err)
4711+
}
4712+
res.Body.Close()
4713+
}

0 commit comments

Comments
 (0)