]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/httptest: fix deadlock in TestIssue7264
authorRuss Cox <rsc@golang.org>
Mon, 8 Sep 2014 00:13:35 +0000 (20:13 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 8 Sep 2014 00:13:35 +0000 (20:13 -0400)
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

src/pkg/net/http/httptest/server_test.go

index 501cc8a999562881be5fa81bd5e24857e418860f..a1c38c50ba3a0f8bdf966f01323e252a84ce6eca 100644 (file)
@@ -32,10 +32,7 @@ func TestServer(t *testing.T) {
 func TestIssue7264(t *testing.T) {
        for i := 0; i < 1000; i++ {
                func() {
-                       inHandler := make(chan bool, 1)
-                       ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-                               inHandler <- true
-                       }))
+                       ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
                        defer ts.Close()
                        tr := &http.Transport{
                                ResponseHeaderTimeout: time.Nanosecond,
@@ -43,7 +40,10 @@ func TestIssue7264(t *testing.T) {
                        defer tr.CloseIdleConnections()
                        c := &http.Client{Transport: tr}
                        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 {
                                res.Body.Close()
                        }