]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: fix crash with Transport.CloseIdleConnections
authorBrad Fitzpatrick <bradfitz@golang.org>
Sat, 10 Mar 2012 00:27:32 +0000 (16:27 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 10 Mar 2012 00:27:32 +0000 (16:27 -0800)
Thanks Michael Lore for the bug report!

Fixes #3266

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5754068

src/pkg/net/http/transport.go
src/pkg/net/http/transport_test.go

index 09579f8a0934916a53cb6cadd91a0cf4bd65149e..024975946e52b2073800191a5bc41f917ce5743c 100644 (file)
@@ -196,7 +196,7 @@ func (t *Transport) CloseIdleConnections() {
                        pconn.close()
                }
        }
-       t.idleConn = nil
+       t.idleConn = make(map[string][]*persistConn)
 }
 
 //
index cbb3884f9eac87e18baad5d965dd12e0db69b61f..a9e401de58da441207775d8bb32ae2d1f9efb4e4 100644 (file)
@@ -698,6 +698,32 @@ func TestTransportPersistConnLeak(t *testing.T) {
        }
 }
 
+// This used to crash; http://golang.org/issue/3266
+func TestTransportIdleConnCrash(t *testing.T) {
+       tr := &Transport{}
+       c := &Client{Transport: tr}
+
+       unblockCh := make(chan bool, 1)
+       ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+               <-unblockCh
+               tr.CloseIdleConnections()
+       }))
+       defer ts.Close()
+
+       didreq := make(chan bool)
+       go func() {
+               res, err := c.Get(ts.URL)
+               if err != nil {
+                       t.Error(err)
+               } else {
+                       res.Body.Close() // returns idle conn
+               }
+               didreq <- true
+       }()
+       unblockCh <- true
+       <-didreq
+}
+
 type fooProto struct{}
 
 func (fooProto) RoundTrip(req *Request) (*Response, error) {