Commit 0cdd2aba authored by Wojciech Tyczynski's avatar Wojciech Tyczynski

Merge pull request #18956 from wojtek-t/debug_etcd_generic

Fix race in EtcdWatcher
parents 4b18fa55 62972321
...@@ -144,23 +144,42 @@ func (w *etcdWatcher) etcdWatch(ctx context.Context, client etcd.KeysAPI, key st ...@@ -144,23 +144,42 @@ func (w *etcdWatcher) etcdWatch(ctx context.Context, client etcd.KeysAPI, key st
defer util.HandleCrash() defer util.HandleCrash()
defer close(w.etcdError) defer close(w.etcdError)
defer close(w.etcdIncoming) defer close(w.etcdIncoming)
if resourceVersion == 0 {
latest, err := etcdGetInitialWatchState(ctx, client, key, w.list, w.etcdIncoming) // We need to be prepared, that Stop() can be called at any time.
if err != nil { // It can potentially also be called, even before this function is called.
w.etcdError <- err // If that is the case, we simply skip all the code here.
return // See #18928 for more details.
var watcher etcd.Watcher
returned := func() bool {
w.stopLock.Lock()
defer w.stopLock.Unlock()
if w.stopped {
// Watcher has already been stopped - don't event initiate it here.
return true
}
// Perform initialization of watcher under lock - we want to avoid situation when
// Stop() is called in the meantime (which in tests can cause etcd termination and
// strange behavior here).
if resourceVersion == 0 {
latest, err := etcdGetInitialWatchState(ctx, client, key, w.list, w.etcdIncoming)
if err != nil {
w.etcdError <- err
return true
}
resourceVersion = latest
} }
resourceVersion = latest
}
opts := etcd.WatcherOptions{ opts := etcd.WatcherOptions{
Recursive: w.list, Recursive: w.list,
AfterIndex: resourceVersion, AfterIndex: resourceVersion,
}
watcher = client.Watcher(key, &opts)
w.ctx, w.cancel = context.WithCancel(ctx)
return false
}()
if returned {
return
} }
watcher := client.Watcher(key, &opts)
w.stopLock.Lock()
w.ctx, w.cancel = context.WithCancel(ctx)
w.stopLock.Unlock()
for { for {
resp, err := watcher.Next(w.ctx) resp, err := watcher.Next(w.ctx)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment