add closure that guarantees mutex unlock in loop
@@ -431,6 +431,7 @@ func (o *ObserveOptions) Run() error {
go func() {
<-time.After(o.exitAfterPeriod)
lock.Lock()
+ defer lock.Unlock()
o.dumpMetrics()
fmt.Fprintf(o.errOut, "Shutting down after %s ...\n", o.exitAfterPeriod)
os.Exit(0)
@@ -467,7 +468,9 @@ func (o *ObserveOptions) Run() error {
deltas := obj.(cache.Deltas)
for _, delta := range deltas {
+ if err := func() error {
lock.Lock()
+ defer lock.Unlock()
// handle before and after observe notification
switch {
@@ -485,7 +488,7 @@ func (o *ObserveOptions) Run() error {
// require the user to provide a name function in order to get events beyond added / updated
if !syncing && o.knownObjects == nil && !(delta.Type == cache.Added || delta.Type == cache.Updated) {
- continue
+ return nil
}
observeCounts.WithLabelValues(string(delta.Type)).Inc()
@@ -499,7 +502,10 @@ func (o *ObserveOptions) Run() error {
return err
}
- lock.Unlock()
+ return nil
+ }(); err != nil {
+ return err
+ }
}
return nil
})
