From: Brad Fitzpatrick Date: Wed, 19 Dec 2012 23:39:19 +0000 (-0800) Subject: net/http: fix server connection leak on Handler's panic(nil) X-Git-Tag: go1.1rc2~1585 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=91934ff5d83807228d021925ed9d9d78d2b777e6;p=gostls13.git net/http: fix server connection leak on Handler's panic(nil) 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 --- diff --git a/src/pkg/net/http/serve_test.go b/src/pkg/net/http/serve_test.go index 7167101273..1de4171239 100644 --- a/src/pkg/net/http/serve_test.go +++ b/src/pkg/net/http/serve_test.go @@ -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 diff --git a/src/pkg/net/http/server.go b/src/pkg/net/http/server.go index 8cd7b11205..3303891f75 100644 --- a/src/pkg/net/http/server.go +++ b/src/pkg/net/http/server.go @@ -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() {