Skip to content

Commit

Permalink
fix(shutdown): fix crash when shutting down before server and task sc…
Browse files Browse the repository at this point in the history
…heduler 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 <[email protected]>
  • Loading branch information
eusebiu-constantin-petu-dbk authored Jan 6, 2024
1 parent 59f41ac commit a46e102
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 8 additions & 3 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 0 additions & 4 deletions pkg/cli/server/config_reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func signalHandler(ctlr *api.Controller, sigCh chan os.Signal) {

// gracefully shutdown http server
ctlr.Shutdown() //nolint: contextcheck

close(sigCh)
}
}

Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit a46e102

Please sign in to comment.