Skip to content

Commit 8abc43a

Browse files
committed
🐛 Prevent race when informers are started more than once
If `Informers` are started a second time, there is a possibility for a data race because it sets a `ctx` field on itself. This write is protected by a mutex, but reads from that field are not.
1 parent a17fd58 commit 8abc43a

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

pkg/cache/cache_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,12 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
18491849
)
18501850
})
18511851
Describe("as an Informer", func() {
1852+
It("should error when starting the cache a second time", func() {
1853+
err := informerCache.Start(context.Background())
1854+
Expect(err).To(HaveOccurred())
1855+
Expect(err.Error()).To(ContainSubstring("Informer already started"))
1856+
})
1857+
18521858
Context("with structured objects", func() {
18531859
It("should be able to get informer for the object", func() {
18541860
By("getting a shared index informer for a pod")

pkg/cache/internal/informers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package internal
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"math/rand"
2324
"net/http"
@@ -186,6 +187,12 @@ type Informers struct {
186187
// Start calls Run on each of the informers and sets started to true. Blocks on the context.
187188
// It doesn't return start because it can't return an error, and it's not a runnable directly.
188189
func (ip *Informers) Start(ctx context.Context) error {
190+
select {
191+
case <-ip.startWait:
192+
return errors.New("Informer already started") //nolint:sylecheck
193+
default:
194+
// continue
195+
}
189196
func() {
190197
ip.mu.Lock()
191198
defer ip.mu.Unlock()

pkg/cache/multi_namespace_cache.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,13 @@ func (c *multiNamespaceCache) GetInformerForKind(ctx context.Context, gvk schema
163163
}
164164

165165
func (c *multiNamespaceCache) Start(ctx context.Context) error {
166+
errs := make(chan error, 0)
166167
// start global cache
167168
if c.clusterCache != nil {
168169
go func() {
169170
err := c.clusterCache.Start(ctx)
170171
if err != nil {
171-
log.Error(err, "cluster scoped cache failed to start")
172+
errs <- fmt.Errorf("failed to start cluster-scoped cache: %w", err)
172173
}
173174
}()
174175
}
@@ -177,13 +178,16 @@ func (c *multiNamespaceCache) Start(ctx context.Context) error {
177178
for ns, cache := range c.namespaceToCache {
178179
go func(ns string, cache Cache) {
179180
if err := cache.Start(ctx); err != nil {
180-
log.Error(err, "multi-namespace cache failed to start namespaced informer", "namespace", ns)
181+
errs <- fmt.Errorf("failed to start cache for namespace %s: %w", ns, err)
181182
}
182183
}(ns, cache)
183184
}
184-
185-
<-ctx.Done()
186-
return nil
185+
select {
186+
case <-ctx.Done():
187+
return nil
188+
case err := <-errs:
189+
return err
190+
}
187191
}
188192

189193
func (c *multiNamespaceCache) WaitForCacheSync(ctx context.Context) bool {

0 commit comments

Comments
 (0)