Skip to content

Commit 73c52e8

Browse files
authored
Merge pull request #1345 from charith-elastic/fix/source-dest-lock
🐛 Prevent source.Channel from shutting down immediately
2 parents 3c5b358 + 25d539c commit 73c52e8

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

pkg/internal/controller/controller.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ func (c *Controller) Start(ctx context.Context) error {
163163
for _, watch := range c.startWatches {
164164
c.Log.Info("Starting EventSource", "source", watch.src)
165165

166-
watchStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
167-
defer cancel()
168-
if err := watch.src.Start(watchStartCtx, watch.handler, c.Queue, watch.predicates...); err != nil {
166+
if err := watch.src.Start(ctx, watch.handler, c.Queue, watch.predicates...); err != nil {
169167
return err
170168
}
171169
}
@@ -179,15 +177,21 @@ func (c *Controller) Start(ctx context.Context) error {
179177
continue
180178
}
181179

182-
// use a context with timeout for launching sources and syncing caches.
183-
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
184-
defer cancel()
180+
if err := func() error {
181+
// use a context with timeout for launching sources and syncing caches.
182+
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
183+
defer cancel()
184+
185+
// WaitForSync waits for a definitive timeout, and returns if there
186+
// is an error or a timeout
187+
if err := syncingSource.WaitForSync(sourceStartCtx); err != nil {
188+
err := fmt.Errorf("failed to wait for %s caches to sync: %w", c.Name, err)
189+
c.Log.Error(err, "Could not wait for Cache to sync")
190+
return err
191+
}
185192

186-
// WaitForSync waits for a definitive timeout, and returns if there
187-
// is an error or a timeout
188-
if err := syncingSource.WaitForSync(sourceStartCtx); err != nil {
189-
err := fmt.Errorf("failed to wait for %s caches to sync: %w", c.Name, err)
190-
c.Log.Error(err, "Could not wait for Cache to sync")
193+
return nil
194+
}(); err != nil {
191195
return err
192196
}
193197
}

pkg/source/source.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ func (cs *Channel) Start(
215215
}
216216

217217
dst := make(chan event.GenericEvent, cs.DestBufferSize)
218+
219+
cs.destLock.Lock()
218220
cs.dest = append(cs.dest, dst)
221+
cs.destLock.Unlock()
219222

220223
cs.once.Do(func() {
221224
// Distribute GenericEvents to all EventHandler / Queue pairs Watching this source
@@ -238,9 +241,6 @@ func (cs *Channel) Start(
238241
}
239242
}()
240243

241-
cs.destLock.Lock()
242-
defer cs.destLock.Unlock()
243-
244244
return nil
245245
}
246246

0 commit comments

Comments
 (0)