From e7cc3c22109fa302204f44dc9d469fe37335ca5b Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sat, 8 Aug 2020 00:57:57 +0100 Subject: [PATCH] protect etcd watcher stop against race condition --- registry/etcd/watcher.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/registry/etcd/watcher.go b/registry/etcd/watcher.go index 6b5d7224..ba7df9bc 100644 --- a/registry/etcd/watcher.go +++ b/registry/etcd/watcher.go @@ -3,6 +3,7 @@ package etcd import ( "context" "errors" + "sync" "time" "github.com/coreos/etcd/clientv3" @@ -10,10 +11,12 @@ import ( ) type etcdWatcher struct { - stop chan bool w clientv3.WatchChan client *clientv3.Client timeout time.Duration + + mtx sync.Mutex + stop chan bool } func newEtcdWatcher(r *etcdRegistry, timeout time.Duration, opts ...registry.WatchOption) (registry.Watcher, error) { @@ -90,6 +93,9 @@ func (ew *etcdWatcher) Next() (*registry.Result, error) { } func (ew *etcdWatcher) Stop() { + ew.mtx.Lock() + defer ew.mtx.Unlock() + select { case <-ew.stop: return