Skip to content

Commit 715ebfa

Browse files
committed
fix: wait for work goroutine to exit in Close()
Address review feedback: Close() now waits for the work goroutine to fully exit before returning. Added doneChan that is closed when the work loop exits, and Close() blocks on receiving from it. Also moved work goroutine startup to newPodEventLogger to ensure it's only started once (not per-namespace).
1 parent cc9fb1a commit 715ebfa

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

logger.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ func newPodEventLogger(ctx context.Context, opts podEventLoggerOptions) (*podEve
8686
},
8787
maxRetries: opts.maxRetries,
8888
},
89+
doneChan: make(chan struct{}),
8990
}
9091

92+
// Start the work goroutine once
93+
go reporter.lq.work(reporter.ctx, reporter.doneChan)
94+
9195
// If no namespaces are provided, we listen for events in all namespaces.
9296
if len(opts.namespaces) == 0 {
9397
if err := reporter.initNamespace(""); err != nil {
@@ -122,6 +126,8 @@ type podEventLogger struct {
122126

123127
// closeOnce ensures Close() is idempotent
124128
closeOnce sync.Once
129+
// doneChan is closed when the work goroutine exits
130+
doneChan chan struct{}
125131
}
126132

127133
// resolveEnvValue resolves the value of an environment variable, supporting both
@@ -164,8 +170,6 @@ func (p *podEventLogger) initNamespace(namespace string) error {
164170
// This is to prevent us from sending duplicate events.
165171
startTime := time.Now()
166172

167-
go p.lq.work(p.ctx)
168-
169173
podFactory := informers.NewSharedInformerFactoryWithOptions(p.client, 0, informers.WithNamespace(namespace), informers.WithTweakListOptions(func(lo *v1.ListOptions) {
170174
lo.FieldSelector = p.fieldSelector
171175
lo.LabelSelector = p.labelSelector
@@ -421,6 +425,8 @@ func (p *podEventLogger) Close() error {
421425
close(p.stopChan)
422426
close(p.errChan)
423427
})
428+
// Wait for the work goroutine to exit
429+
<-p.doneChan
424430
return nil
425431
}
426432

@@ -509,8 +515,9 @@ type logQueuer struct {
509515
maxRetries int
510516
}
511517

512-
func (l *logQueuer) work(ctx context.Context) {
518+
func (l *logQueuer) work(ctx context.Context, done chan struct{}) {
513519
defer l.cleanup()
520+
defer close(done)
514521

515522
for ctx.Err() == nil {
516523
select {

logger_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ func Test_logQueuer(t *testing.T) {
675675

676676
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
677677
defer cancel()
678-
go lq.work(ctx)
678+
go lq.work(ctx, make(chan struct{}))
679679

680680
ch <- agentLog{
681681
op: opLog,
@@ -742,7 +742,7 @@ func Test_logQueuer(t *testing.T) {
742742

743743
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
744744
defer cancel()
745-
go lq.work(ctx)
745+
go lq.work(ctx, make(chan struct{}))
746746

747747
token := "retry-token"
748748
ch <- agentLog{
@@ -905,7 +905,7 @@ func Test_logQueuer(t *testing.T) {
905905

906906
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
907907
defer cancel()
908-
go lq.work(ctx)
908+
go lq.work(ctx, make(chan struct{}))
909909

910910
token := "max-retry-token"
911911
ch <- agentLog{
@@ -1111,7 +1111,7 @@ func Test_logCache(t *testing.T) {
11111111

11121112
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
11131113
defer cancel()
1114-
go lq.work(ctx)
1114+
go lq.work(ctx, make(chan struct{}))
11151115

11161116
token := "test-token"
11171117

0 commit comments

Comments
 (0)