From: Brad Fitzpatrick Date: Tue, 20 Sep 2011 02:56:51 +0000 (-0700) Subject: http: fix TLS handshake blocking server accept loop X-Git-Tag: weekly.2011-09-21~22 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3c3a86ccc72853a3e7bb1783922d5303e54adcd7;p=gostls13.git http: fix TLS handshake blocking server accept loop Fixes #2263 R=golang-dev, adg CC=golang-dev https://golang.org/cl/5076042 --- diff --git a/src/pkg/http/serve_test.go b/src/pkg/http/serve_test.go index 17439110f0..86653216fc 100644 --- a/src/pkg/http/serve_test.go +++ b/src/pkg/http/serve_test.go @@ -545,6 +545,19 @@ func TestTLSServer(t *testing.T) { } })) defer ts.Close() + + // Connect an idle TCP connection to this server before we run + // our real tests. This idle connection used to block forever + // in the TLS handshake, preventing future connections from + // being accepted. It may prevent future accidental blocking + // in newConn. + idleConn, err := net.Dial("tcp", ts.Listener.Addr().String()) + if err != nil { + t.Fatalf("Dial: %v", err) + } + defer idleConn.Close() + time.AfterFunc(10e9, func() { t.Fatalf("Timeout") }) + if !strings.HasPrefix(ts.URL, "https://") { t.Fatalf("expected test TLS server to start with https://, got %q", ts.URL) } diff --git a/src/pkg/http/server.go b/src/pkg/http/server.go index 654af378a1..6be3611f0f 100644 --- a/src/pkg/http/server.go +++ b/src/pkg/http/server.go @@ -178,13 +178,6 @@ func (srv *Server) newConn(rwc net.Conn) (c *conn, err os.Error) { br := bufio.NewReader(c.lr) bw := bufio.NewWriter(rwc) c.buf = bufio.NewReadWriter(br, bw) - - if tlsConn, ok := rwc.(*tls.Conn); ok { - tlsConn.Handshake() - c.tlsState = new(tls.ConnectionState) - *c.tlsState = tlsConn.ConnectionState() - } - return c, nil } @@ -562,6 +555,12 @@ func (c *conn) serve() { log.Print(buf.String()) }() + if tlsConn, ok := c.rwc.(*tls.Conn); ok { + tlsConn.Handshake() + c.tlsState = new(tls.ConnectionState) + *c.tlsState = tlsConn.ConnectionState() + } + for { w, err := c.readRequest() if err != nil {