Skip to content

Commit

Permalink
etcdserver: fix racey StopNotify
Browse files Browse the repository at this point in the history
Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Sep 21, 2017
1 parent 2c22589 commit 260eae2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ type EtcdServer struct {
// stopping is closed by run goroutine on shutdown.
stopping chan struct{}
// done is closed when all goroutines from start() complete.
done chan struct{}
doneMu sync.Mutex
done chan struct{}

errorc chan error
id types.ID
Expand Down Expand Up @@ -549,7 +550,9 @@ func (s *EtcdServer) start() {
}
s.w = wait.New()
s.applyWait = wait.NewTimeList()
s.doneMu.Lock()
s.done = make(chan struct{})
s.doneMu.Unlock()
s.stop = make(chan struct{})
s.stopping = make(chan struct{})
s.ctx, s.cancel = context.WithCancel(context.Background())
Expand Down Expand Up @@ -1043,7 +1046,11 @@ func (s *EtcdServer) stopWithDelay(d time.Duration, err error) {

// StopNotify returns a channel that receives a empty struct
// when the server is stopped.
func (s *EtcdServer) StopNotify() <-chan struct{} { return s.done }
func (s *EtcdServer) StopNotify() <-chan struct{} {
s.doneMu.Lock()
defer s.doneMu.Unlock()
return s.done
}

func (s *EtcdServer) SelfStats() []byte { return s.stats.JSON() }

Expand Down

0 comments on commit 260eae2

Please sign in to comment.