Fix data race w/ stopping admin server

WARNING: DATA RACE
Write by goroutine 9:
  github.com/influxdb/influxdb/admin.func·001()
      /home/ubuntu/.go_project/src/github.com/influxdb/influxdb/admin/admin.go:45 +0x18d

Previous read by goroutine 7:
  github.com/influxdb/influxdb/admin.(*Server).ListenAndServe()
      /home/ubuntu/.go_project/src/github.com/influxdb/influxdb/admin/admin.go:50 +0x3b4
  github.com/influxdb/influxdb/admin_test.func·001()
      /home/ubuntu/.go_project/src/github.com/influxdb/influxdb/admin/admin_test.go:13 +0x4a

Goroutine 9 (running) created at:
  github.com/influxdb/influxdb/admin.(*Server).ListenAndServe()
      /home/ubuntu/.go_project/src/github.com/influxdb/influxdb/admin/admin.go:49 +0x3a4
  github.com/influxdb/influxdb/admin_test.func·001()
      /home/ubuntu/.go_project/src/github.com/influxdb/influxdb/admin/admin_test.go:13 +0x4a

Goroutine 7 (finished) created at:
  github.com/influxdb/influxdb/admin_test.Test_ServesIndexByDefault()
      /home/ubuntu/.go_project/src/github.com/influxdb/influxdb/admin/admin_test.go:13 +0x1af
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:447 +0x133
pull/2229/head
Jason Wilder 2015-04-09 15:23:29 -06:00
parent c22909f984
commit 2e5db51441
1 changed files with 6 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"net"
"net/http"
"strings"
"sync"
"github.com/rakyll/statik/fs"
@ -14,6 +15,7 @@ import (
// Server manages InfluxDB's admin web server.
type Server struct {
mu sync.Mutex
addr string
listener net.Listener
closed bool
@ -28,6 +30,8 @@ func NewServer(addr string) *Server {
// ListenAndServe starts the admin web server and serves requests until
// s.Close() is called.
func (s *Server) ListenAndServe() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.addr == "" {
return nil
}
@ -52,6 +56,8 @@ func (s *Server) ListenAndServe() error {
// Close stops the admin web server.
func (s *Server) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}