Skip to content

Commit 6dd03ed

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 6dd03ed

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

pkg/cache/cache.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ import (
3939
"sigs.k8s.io/controller-runtime/pkg/cache/internal"
4040
"sigs.k8s.io/controller-runtime/pkg/client"
4141
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
42-
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
4342
)
4443

4544
var (
46-
log = logf.RuntimeLog.WithName("object-cache")
4745
defaultSyncPeriod = 10 * time.Hour
4846
)
4947

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: 11 additions & 2 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,10 +187,14 @@ 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 {
189-
func() {
190+
if err := func() error {
190191
ip.mu.Lock()
191192
defer ip.mu.Unlock()
192193

194+
if ip.started {
195+
return errors.New("Informer already started") //nolint:stylecheck
196+
}
197+
193198
// Set the context so it can be passed to informers that are added later
194199
ip.ctx = ctx
195200

@@ -207,7 +212,11 @@ func (ip *Informers) Start(ctx context.Context) error {
207212
// Set started to true so we immediately start any informers added later.
208213
ip.started = true
209214
close(ip.startWait)
210-
}()
215+
216+
return nil
217+
}(); err != nil {
218+
return err
219+
}
211220
<-ctx.Done() // Block until the context is done
212221
ip.mu.Lock()
213222
ip.stopped = true // Set stopped to true so we don't start any new informers

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)
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)