Skip to content

Commit 2d9a8e8

Browse files
committed
synchronize errgroup access in manager
1 parent a78198a commit 2d9a8e8

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

manager/manager.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@ func NewManager(debugConfig *componentconfig.DebuggingConfiguration, address str
6262
// health / debug endpoints for them. It stops when the context is cancelled.
6363
// It will only have an effect the first time it is called.
6464
func (m *Manager) Start(ctx context.Context, controllers ...Controller) error {
65+
m.RLock()
6566
if m.errG != nil {
6667
return fmt.Errorf("manager already started")
6768
}
69+
m.RUnlock()
6870

6971
broadcaster := record.NewBroadcaster()
7072

7173
m.once.Do(func() {
74+
m.Lock()
7275
m.errG, ctx = errgroup.WithContext(ctx)
7376
m.errGCtx = ctx
77+
m.Unlock()
7478

7579
// start controllers
7680
if err := m.Go(controllers...); err != nil {
@@ -105,17 +109,19 @@ func (m *Manager) Start(ctx context.Context, controllers ...Controller) error {
105109

106110
// Go adds controllers into the existing manager's errgroup
107111
func (m *Manager) Go(controllers ...Controller) error {
108-
if m.errG == nil {
112+
m.RLock()
113+
errG := m.errG
114+
if errG == nil {
109115
return fmt.Errorf("cannot add controllers to an unstarted manager")
110116
}
111-
112117
ctx := m.errGCtx
118+
m.RUnlock()
113119

114120
// start newly added controllers
115121
for _, c := range controllers {
116122
c := c
117123
m.healthzHandler.AddHealthChecker(controllerhealthz.NamedHealthChecker(c.Name(), c.HealthChecker()))
118-
m.errG.Go(func() error {
124+
errG.Go(func() error {
119125
ctx, cancel := context.WithCancel(ctx)
120126
m.Lock()
121127
m.cancelFuncs[c] = cancel

0 commit comments

Comments
 (0)