]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: fix server connection leak on Handler's panic(nil)
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 19 Dec 2012 23:39:19 +0000 (15:39 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 19 Dec 2012 23:39:19 +0000 (15:39 -0800)
If a handler did a panic(nil), the connection was never closed.

Fixes #4050

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/6971049

src/pkg/net/http/serve_test.go
src/pkg/net/http/server.go

index 7167101273246b89aed10c9523753cdf0129c586..1de4171239dee718aee6bdb9b492a5c69c9b3a86 100644 (file)
@@ -918,15 +918,19 @@ func TestZeroLengthPostAndResponse(t *testing.T) {
        }
 }
 
+func TestHandlerPanicNil(t *testing.T) {
+       testHandlerPanic(t, false, nil)
+}
+
 func TestHandlerPanic(t *testing.T) {
-       testHandlerPanic(t, false)
+       testHandlerPanic(t, false, "intentional death for testing")
 }
 
 func TestHandlerPanicWithHijack(t *testing.T) {
-       testHandlerPanic(t, true)
+       testHandlerPanic(t, true, "intentional death for testing")
 }
 
-func testHandlerPanic(t *testing.T, withHijack bool) {
+func testHandlerPanic(t *testing.T, withHijack bool, panicValue interface{}) {
        // Unlike the other tests that set the log output to ioutil.Discard
        // to quiet the output, this test uses a pipe.  The pipe serves three
        // purposes:
@@ -955,7 +959,7 @@ func testHandlerPanic(t *testing.T, withHijack bool) {
                        }
                        defer rwc.Close()
                }
-               panic("intentional death for testing")
+               panic(panicValue)
        }))
        defer ts.Close()
 
@@ -968,7 +972,7 @@ func testHandlerPanic(t *testing.T, withHijack bool) {
                _, err := pr.Read(buf)
                pr.Close()
                if err != nil {
-                       t.Fatal(err)
+                       t.Error(err)
                }
                done <- true
        }()
@@ -978,6 +982,10 @@ func testHandlerPanic(t *testing.T, withHijack bool) {
                t.Logf("expected an error")
        }
 
+       if panicValue == nil {
+               return
+       }
+
        select {
        case <-done:
                return
index 8cd7b11205a6be4ea44ad3f9981c3475213b43f2..3303891f755c6c451c74787262a17039b8bf1885 100644 (file)
@@ -716,6 +716,7 @@ func (c *conn) serve() {
                        c.rwc.Close()
                }
        }()
+       defer c.close()
 
        if tlsConn, ok := c.rwc.(*tls.Conn); ok {
                if err := tlsConn.Handshake(); err != nil {
@@ -791,7 +792,6 @@ func (c *conn) serve() {
                        break
                }
        }
-       c.close()
 }
 
 func (w *response) sendExpectationFailed() {