From 2c83362e63e6b796ea988442483673056c567455 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Sun, 27 Mar 2016 02:30:39 -0700 Subject: [PATCH] clientv3: fix race in KV reconnection logic --- clientv3/kv.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/clientv3/kv.go b/clientv3/kv.go index 9b7d31167..04e33688b 100644 --- a/clientv3/kv.go +++ b/clientv3/kv.go @@ -183,14 +183,18 @@ func (kv *kv) Do(ctx context.Context, op Op) (OpResponse, error) { } func (kv *kv) switchRemote(prevErr error) error { + // Usually it's a bad idea to lock on network i/o but here it's OK + // since the link is down and new requests can't be processed anyway. + // Likewise, if connecting stalls, closing the Client can break the + // lock via context cancelation. + kv.mu.Lock() + defer kv.mu.Unlock() + newConn, err := kv.c.retryConnection(kv.conn, prevErr) if err != nil { return err } - kv.mu.Lock() - defer kv.mu.Unlock() - kv.conn = newConn kv.remote = pb.NewKVClient(kv.conn) return nil