0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

fix(shutdown): fix crash when shutting down before server and task scheduler have started. (#2148)

init shutdown routine after controller.Init()
check for nil values before stopping http server and task scheduler.

Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
peusebiu 2024-01-06 03:50:48 +02:00 committed by GitHub
parent 59f41ac17d
commit a46e10269a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View file

@ -370,13 +370,18 @@ func (c *Controller) LoadNewConfig(newConfig *config.Config) {
func (c *Controller) Shutdown() {
c.StopBackgroundTasks()
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
if c.Server != nil {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}
}
// Will stop scheduler and wait for all tasks to finish their work.
func (c *Controller) StopBackgroundTasks() {
c.taskScheduler.Shutdown()
if c.taskScheduler != nil {
c.taskScheduler.Shutdown()
}
}
func (c *Controller) StartBackgroundTasks() {

View file

@ -41,8 +41,6 @@ func signalHandler(ctlr *api.Controller, sigCh chan os.Signal) {
// gracefully shutdown http server
ctlr.Shutdown() //nolint: contextcheck
close(sigCh)
}
}
@ -61,8 +59,6 @@ func initShutDownRoutine(ctlr *api.Controller) {
func (hr *HotReloader) Start() {
done := make(chan bool)
initShutDownRoutine(hr.ctlr)
// run watcher
go func() {
defer hr.watcher.Close()

View file

@ -69,6 +69,8 @@ func newServeCmd(conf *config.Config) *cobra.Command {
return err
}
initShutDownRoutine(ctlr)
if err := ctlr.Run(); err != nil {
log.Error().Err(err).Msg("failed to start controller, exiting")
}