mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
fix(server): avoid race conditions in Run/Stop
- don't close ready channel until PeerServer is listening. avoids possible panic in Stop() if PeerServer is nil. - avoid data race in Run() (err variable was shared between 2 goroutines) - avoid data race in PeerServer Start/Stop (PeerServer.closeChan)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/etcd/third_party/github.com/goraft/raft"
|
||||
@@ -72,6 +73,7 @@ type PeerServer struct {
|
||||
proxyClientURL string
|
||||
|
||||
metrics *metrics.Bucket
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// TODO: find a good policy to do snapshot
|
||||
@@ -257,6 +259,9 @@ func (s *PeerServer) findCluster(discoverURL string, peers []string) {
|
||||
|
||||
// Start the raft server
|
||||
func (s *PeerServer) Start(snapshot bool, discoverURL string, peers []string) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
// LoadSnapshot
|
||||
if snapshot {
|
||||
err := s.raftServer.LoadSnapshot()
|
||||
@@ -295,6 +300,9 @@ func (s *PeerServer) Start(snapshot bool, discoverURL string, peers []string) er
|
||||
}
|
||||
|
||||
func (s *PeerServer) Stop() {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if s.closeChan != nil {
|
||||
close(s.closeChan)
|
||||
s.closeChan = nil
|
||||
|
||||
Reference in New Issue
Block a user