mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
fix(scheduler): fix data race (#2085)
* fix(scheduler): data race when pushing new tasks the problem here is that scheduler can be closed in two ways: - canceling the context given as argument to scheduler.RunScheduler() - running scheduler.Shutdown() because of this shutdown can trigger a data race between calling scheduler.inShutdown() and actually pushing tasks into the pool workers solved that by keeping a quit channel and listening on both quit channel and ctx.Done() and closing the worker chan and scheduler afterwards. Signed-off-by: Petu Eusebiu <peusebiu@cisco.com> * refactor(scheduler): refactor into a single shutdown before this we could stop scheduler either by closing the context provided to RunScheduler(ctx) or by running Shutdown(). simplify things by getting rid of the external context in RunScheduler(). keep an internal context in the scheduler itself and pass it down to all tasks. Signed-off-by: Petu Eusebiu <peusebiu@cisco.com> --------- Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
+14
-11
@@ -94,12 +94,12 @@ func (c *Controller) GetPort() int {
|
||||
return c.chosenPort
|
||||
}
|
||||
|
||||
func (c *Controller) Run(reloadCtx context.Context) error {
|
||||
func (c *Controller) Run() error {
|
||||
if err := c.initCookieStore(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.StartBackgroundTasks(reloadCtx)
|
||||
c.StartBackgroundTasks()
|
||||
|
||||
// setup HTTP API router
|
||||
engine := mux.NewRouter()
|
||||
@@ -216,7 +216,7 @@ func (c *Controller) Run(reloadCtx context.Context) error {
|
||||
return server.Serve(listener)
|
||||
}
|
||||
|
||||
func (c *Controller) Init(reloadCtx context.Context) error {
|
||||
func (c *Controller) Init() error {
|
||||
// print the current configuration, but strip secrets
|
||||
c.Log.Info().Interface("params", c.Config.Sanitize()).Msg("configuration settings")
|
||||
|
||||
@@ -237,7 +237,7 @@ func (c *Controller) Init(reloadCtx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.InitMetaDB(reloadCtx); err != nil {
|
||||
if err := c.InitMetaDB(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ func (c *Controller) initCookieStore() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) InitMetaDB(reloadCtx context.Context) error {
|
||||
func (c *Controller) InitMetaDB() error {
|
||||
// init metaDB if search is enabled or we need to store user profiles, api keys or signatures
|
||||
if c.Config.IsSearchEnabled() || c.Config.IsBasicAuthnEnabled() || c.Config.IsImageTrustEnabled() ||
|
||||
c.Config.IsRetentionEnabled() {
|
||||
@@ -310,7 +310,7 @@ func (c *Controller) InitMetaDB(reloadCtx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) LoadNewConfig(reloadCtx context.Context, newConfig *config.Config) {
|
||||
func (c *Controller) LoadNewConfig(newConfig *config.Config) {
|
||||
// reload access control config
|
||||
c.Config.HTTP.AccessControl = newConfig.HTTP.AccessControl
|
||||
|
||||
@@ -364,21 +364,24 @@ func (c *Controller) LoadNewConfig(reloadCtx context.Context, newConfig *config.
|
||||
|
||||
c.InitCVEInfo()
|
||||
|
||||
c.StartBackgroundTasks(reloadCtx)
|
||||
|
||||
c.Log.Info().Interface("reloaded params", c.Config.Sanitize()).
|
||||
Msg("loaded new configuration settings")
|
||||
}
|
||||
|
||||
func (c *Controller) Shutdown() {
|
||||
c.taskScheduler.Shutdown()
|
||||
c.StopBackgroundTasks()
|
||||
ctx := context.Background()
|
||||
_ = c.Server.Shutdown(ctx)
|
||||
}
|
||||
|
||||
func (c *Controller) StartBackgroundTasks(reloadCtx context.Context) {
|
||||
// Will stop scheduler and wait for all tasks to finish their work.
|
||||
func (c *Controller) StopBackgroundTasks() {
|
||||
c.taskScheduler.Shutdown()
|
||||
}
|
||||
|
||||
func (c *Controller) StartBackgroundTasks() {
|
||||
c.taskScheduler = scheduler.NewScheduler(c.Config, c.Metrics, c.Log)
|
||||
c.taskScheduler.RunScheduler(reloadCtx)
|
||||
c.taskScheduler.RunScheduler()
|
||||
|
||||
// Enable running garbage-collect periodically for DefaultStore
|
||||
if c.Config.Storage.GC {
|
||||
|
||||
Reference in New Issue
Block a user