Skip to content

Commit e488bae

Browse files
committed
Add path to watch before before creating background gorouting
avoid the case where defer of the goroutine is called before w.Add Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
1 parent 8816181 commit e488bae

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

pkg/common/certs.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func NewCertReloader(ctx context.Context, path string, init *tls.Certificate) (*
5050
WithValues("path", path)
5151
traceLogger := logger.V(logutil.TRACE)
5252

53+
if err := w.Add(path); err != nil {
54+
_ = w.Close() // Clean up watcher before returning
55+
return nil, fmt.Errorf("failed to watch %q: %w", path, err)
56+
}
57+
5358
go func() {
5459
defer w.Close()
5560

@@ -90,10 +95,6 @@ func NewCertReloader(ctx context.Context, path string, init *tls.Certificate) (*
9095
}
9196
}()
9297

93-
if err := w.Add(path); err != nil {
94-
return nil, fmt.Errorf("failed to watch %q: %w", path, err)
95-
}
96-
9798
return &CertReloader{cert: certPtr}, nil
9899
}
99100

0 commit comments

Comments
 (0)