diff --git a/pkg/schedule/schedulers/scheduler_controller.go b/pkg/schedule/schedulers/scheduler_controller.go index 4d72699b0feb..ab4a6d32a93d 100644 --- a/pkg/schedule/schedulers/scheduler_controller.go +++ b/pkg/schedule/schedulers/scheduler_controller.go @@ -41,6 +41,7 @@ var denySchedulersByLabelerCounter = labeler.LabelerEventCounter.WithLabelValues type Controller struct { sync.RWMutex wg sync.WaitGroup + wgLock sync.Mutex ctx context.Context cluster sche.SchedulerCluster storage endpoint.ConfigStorage @@ -67,7 +68,9 @@ func NewController(ctx context.Context, cluster sche.SchedulerCluster, storage e // Wait waits on all schedulers to exit. func (c *Controller) Wait() { + c.wgLock.Lock() c.wg.Wait() + c.wgLock.Unlock() } // GetScheduler returns a schedule controller by name. @@ -184,7 +187,10 @@ func (c *Controller) AddScheduler(scheduler Scheduler, args ...string) error { return err } + c.wgLock.Lock() c.wg.Add(1) + c.wgLock.Unlock() + go c.runScheduler(s) c.schedulers[s.Scheduler.GetName()] = s c.cluster.GetSchedulerConfig().AddSchedulerCfg(s.Scheduler.GetType(), args) @@ -320,7 +326,11 @@ func (c *Controller) IsSchedulerExisted(name string) (bool, error) { func (c *Controller) runScheduler(s *ScheduleController) { defer logutil.LogPanic() - defer c.wg.Done() + defer func() { + c.wgLock.Lock() + c.wg.Done() + c.wgLock.Unlock() + }() defer s.Scheduler.Cleanup(c.cluster) ticker := time.NewTicker(s.GetInterval())