Skip to content

Commit

Permalink
clientv3: fix race condition in "Endpoints" methods
Browse files Browse the repository at this point in the history
From #10595.

Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed May 2, 2019
1 parent f179d4d commit b7001c0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Client struct {
cfg Config
creds *credentials.TransportCredentials
balancer *healthBalancer
mu *sync.Mutex
mu *sync.RWMutex

ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -110,11 +110,13 @@ func (c *Client) Close() error {
func (c *Client) Ctx() context.Context { return c.ctx }

// Endpoints lists the registered endpoints for the client.
func (c *Client) Endpoints() (eps []string) {
func (c *Client) Endpoints() []string {
c.mu.RLock()
defer c.mu.RUnlock()
// copy the slice; protect original endpoints from being changed
eps = make([]string, len(c.cfg.Endpoints))
eps := make([]string, len(c.cfg.Endpoints))
copy(eps, c.cfg.Endpoints)
return
return eps
}

// SetEndpoints updates client's endpoints.
Expand Down Expand Up @@ -387,7 +389,7 @@ func newClient(cfg *Config) (*Client, error) {
creds: creds,
ctx: ctx,
cancel: cancel,
mu: new(sync.Mutex),
mu: new(sync.RWMutex),
callOpts: defaultCallOpts,
}
if cfg.Username != "" && cfg.Password != "" {
Expand Down

0 comments on commit b7001c0

Please sign in to comment.