From b2e9f425b92cd6b986051a55c24dc96b777d9f28 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 9 Mar 2012 16:27:32 -0800 Subject: [PATCH] net/http: fix crash with Transport.CloseIdleConnections 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 | 2 +- src/pkg/net/http/transport_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/pkg/net/http/transport.go b/src/pkg/net/http/transport.go index 09579f8a093..024975946e5 100644 --- a/src/pkg/net/http/transport.go +++ b/src/pkg/net/http/transport.go @@ -196,7 +196,7 @@ func (t *Transport) CloseIdleConnections() { pconn.close() } } - t.idleConn = nil + t.idleConn = make(map[string][]*persistConn) } // diff --git a/src/pkg/net/http/transport_test.go b/src/pkg/net/http/transport_test.go index cbb3884f9ea..a9e401de58d 100644 --- a/src/pkg/net/http/transport_test.go +++ b/src/pkg/net/http/transport_test.go @@ -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) {