Skip to content

Commit

Permalink
net/http: never cancel r.Context() after Hijack
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jun 10, 2019
1 parent 350f71f commit 395d0e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/net/http/serve_test.go
Expand Up @@ -6066,6 +6066,7 @@ func TestServerContexts(t *testing.T) {
}
}


func TestServerContextsHTTP2(t *testing.T) {
setParallel(t)
defer afterTest(t)
Expand Down Expand Up @@ -6166,6 +6167,46 @@ func fetchWireResponse(host string, http1ReqBody []byte) ([]byte, error) {
return ioutil.ReadAll(conn)
}

func TestServerContextAfterHijack(t *testing.T) {
done := make(chan struct{})
s := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
defer close(done)

w.WriteHeader(StatusOK)

hj, ok := w.(Hijacker)
if !ok {
t.Errorf("w not a hijacker? %T", w)
return
}

c, brw, err := hj.Hijack()
if err != nil {
t.Errorf("failed to hijack: %v", err)
return
}

c.Close()
brw.Read(nil)

if r.Context().Err() != nil {
t.Errorf("context cancelled after call to Hijack")
return
}
}))
defer s.Close()

cl := s.Client()
cl.Timeout = time.Second*30

resp, err := cl.Get(s.URL)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
<-done
}

func BenchmarkResponseStatusLine(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
Expand Down
4 changes: 4 additions & 0 deletions src/net/http/server.go
Expand Up @@ -742,6 +742,10 @@ func (cr *connReader) hitReadLimit() bool { return cr.remain <= 0 }
//
// It may be called from multiple goroutines.
func (cr *connReader) handleReadError(_ error) {
if cr.conn.hijacked() {
// https://github.com/golang/go/issues/32314
return
}
cr.conn.cancelCtx()
cr.closeNotify()
}
Expand Down

0 comments on commit 395d0e3

Please sign in to comment.