Skip to content

Commit bca3e2a

Browse files
committed
Guard access to c.Queue explicitly.
1 parent 730b30e commit bca3e2a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pkg/internal/controller/controller.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,15 @@ func (c *Controller[request]) startEventSourcesLocked(ctx context.Context) error
332332
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
333333
defer cancel()
334334

335-
sourceStartErrChan := make(chan error, 1) // Buffer chan to not leak goroutine if we time out
335+
sourceStartErrChan := make(chan error, 1) // Buffer chan to not leak goroutine if we time out
336+
hasAccessedQueueChan := make(chan struct{}) //
336337
go func() {
337338
defer close(sourceStartErrChan)
338339
log.Info("Starting EventSource")
339-
if err := watch.Start(ctx, c.Queue); err != nil {
340+
341+
q := c.Queue
342+
close(hasAccessedQueueChan)
343+
if err := watch.Start(ctx, q); err != nil {
340344
sourceStartErrChan <- err
341345
return
342346
}
@@ -356,8 +360,8 @@ func (c *Controller[request]) startEventSourcesLocked(ctx context.Context) error
356360
case err := <-sourceStartErrChan:
357361
return err
358362
case <-sourceStartCtx.Done():
359-
defer func() { <-sourceStartErrChan }() // Ensure that watch.Start has been called to avoid prematurely releasing lock before accessing c.Queue
360-
if didStartSyncingSource.Load() { // We are racing with WaitForSync, wait for it to let it tell us what happened
363+
defer func() { <-hasAccessedQueueChan }() // Ensure that watch.Start has been called to avoid prematurely releasing lock before accessing c.Queue
364+
if didStartSyncingSource.Load() { // We are racing with WaitForSync, wait for it to let it tell us what happened
361365
return <-sourceStartErrChan
362366
}
363367
if ctx.Err() != nil { // Don't return an error if the root context got cancelled

0 commit comments

Comments
 (0)