fix(commands): potential nil ptr error on shutdown (#3947)

pull/3956/head
James Elliott 2022-09-04 22:26:03 +10:00 committed by GitHub
parent fbda31f897
commit 6cc182de08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 9 deletions

View File

@ -86,6 +86,7 @@ func cmdRootRun(_ *cobra.Command, _ []string) {
runServers(config, providers, logger) runServers(config, providers, logger)
} }
//nolint:gocyclo // Complexity is required in this function.
func runServers(config *schema.Configuration, providers middlewares.Providers, log *logrus.Logger) { func runServers(config *schema.Configuration, providers middlewares.Providers, log *logrus.Logger) {
ctx := context.Background() ctx := context.Background()
@ -108,8 +109,8 @@ func runServers(config *schema.Configuration, providers middlewares.Providers, l
g.Go(func() (err error) { g.Go(func() (err error) {
defer func() { defer func() {
if rec := recover(); rec != nil { if r := recover(); r != nil {
log.WithError(recoverErr(rec)).Errorf("Critical error in server caught (recovered)") log.WithError(recoverErr(r)).Errorf("Critical error in server caught (recovered)")
} }
}() }()
@ -118,6 +119,8 @@ func runServers(config *schema.Configuration, providers middlewares.Providers, l
} }
if err = mainServer.Serve(mainListener); err != nil { if err = mainServer.Serve(mainListener); err != nil {
log.WithError(err).Error("Server (main) returned error")
return err return err
} }
@ -130,8 +133,8 @@ func runServers(config *schema.Configuration, providers middlewares.Providers, l
} }
defer func() { defer func() {
if rec := recover(); rec != nil { if r := recover(); r != nil {
log.WithError(recoverErr(rec)).Errorf("Critical error in metrics server caught (recovered)") log.WithError(recoverErr(r)).Errorf("Critical error in metrics server caught (recovered)")
} }
}() }()
@ -140,6 +143,8 @@ func runServers(config *schema.Configuration, providers middlewares.Providers, l
} }
if err = metricsServer.Serve(metricsListener); err != nil { if err = metricsServer.Serve(metricsListener); err != nil {
log.WithError(err).Error("Server (metrics) returned error")
return err return err
} }
@ -147,10 +152,15 @@ func runServers(config *schema.Configuration, providers middlewares.Providers, l
}) })
select { select {
case <-quit: case s := <-quit:
break switch s {
case syscall.SIGINT:
log.Debugf("Shutdown started due to SIGINT")
case syscall.SIGQUIT:
log.Debugf("Shutdown started due to SIGQUIT")
}
case <-ctx.Done(): case <-ctx.Done():
break log.Debugf("Shutdown started due to context completion")
} }
cancel() cancel()
@ -159,8 +169,10 @@ func runServers(config *schema.Configuration, providers middlewares.Providers, l
var err error var err error
if err = mainServer.Shutdown(); err != nil { if mainServer != nil {
log.WithError(err).Errorf("Error occurred shutting down the server") if err = mainServer.Shutdown(); err != nil {
log.WithError(err).Errorf("Error occurred shutting down the server")
}
} }
if metricsServer != nil { if metricsServer != nil {