1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:04:46 -07:00

net/http/httptest: fix deadlock in TestIssue7264

I am seeing deadlocks waiting on <-inHandler.
It seems to me that there is no guarantee that the
handler actually runs, if the client does

        write header
        close connection

fast enough. The server might see the EOF on the
connection before it manages to invoke the handler.

This change fixes the deadlock, but it may make
the test not actually test anything. Not sure.

LGTM=bradfitz
R=bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/140970043
This commit is contained in:
Russ Cox 2014-09-07 20:13:35 -04:00
parent b4bfa6c964
commit 902f8d9ca0

View File

@ -32,10 +32,7 @@ func TestServer(t *testing.T) {
func TestIssue7264(t *testing.T) { func TestIssue7264(t *testing.T) {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
func() { func() {
inHandler := make(chan bool, 1) ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
inHandler <- true
}))
defer ts.Close() defer ts.Close()
tr := &http.Transport{ tr := &http.Transport{
ResponseHeaderTimeout: time.Nanosecond, ResponseHeaderTimeout: time.Nanosecond,
@ -43,7 +40,10 @@ func TestIssue7264(t *testing.T) {
defer tr.CloseIdleConnections() defer tr.CloseIdleConnections()
c := &http.Client{Transport: tr} c := &http.Client{Transport: tr}
res, err := c.Get(ts.URL) res, err := c.Get(ts.URL)
<-inHandler // err can be non-nil here.
// If the client writes the header and then immediately observes
// the timeout and closes the connection, the server might never
// have gotten a chance to send a response. That's okay.
if err == nil { if err == nil {
res.Body.Close() res.Body.Close()
} }