1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:50:05 -07:00

net/http/httptest: fix possible race on historyListener.history

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6845077
This commit is contained in:
Dave Cheney 2012-11-24 15:50:43 +11:00
parent ec2460a314
commit d4775a7814

View File

@ -36,13 +36,16 @@ type Server struct {
// accepted.
type historyListener struct {
net.Listener
history []net.Conn
sync.Mutex // protects history
history []net.Conn
}
func (hs *historyListener) Accept() (c net.Conn, err error) {
c, err = hs.Listener.Accept()
if err == nil {
hs.Lock()
hs.history = append(hs.history, c)
hs.Unlock()
}
return
}
@ -96,7 +99,7 @@ func (s *Server) Start() {
if s.URL != "" {
panic("Server already started")
}
s.Listener = &historyListener{s.Listener, make([]net.Conn, 0)}
s.Listener = &historyListener{Listener: s.Listener}
s.URL = "http://" + s.Listener.Addr().String()
s.wrapHandler()
go s.Config.Serve(s.Listener)
@ -122,7 +125,7 @@ func (s *Server) StartTLS() {
}
tlsListener := tls.NewListener(s.Listener, s.TLS)
s.Listener = &historyListener{tlsListener, make([]net.Conn, 0)}
s.Listener = &historyListener{Listener: tlsListener}
s.URL = "https://" + s.Listener.Addr().String()
s.wrapHandler()
go s.Config.Serve(s.Listener)
@ -161,9 +164,11 @@ func (s *Server) CloseClientConnections() {
if !ok {
return
}
hl.Lock()
for _, conn := range hl.history {
conn.Close()
}
hl.Unlock()
}
// waitGroupHandler wraps a handler, incrementing and decrementing a