From e354950b94325ade3d8e1b58d8789ae2107f048b Mon Sep 17 00:00:00 2001 From: Leonard Goodell Date: Wed, 13 Jul 2022 17:38:59 -0700 Subject: [PATCH] fix: Ensure exit with non-zero code when error occurs fixes #318 Signed-off-by: Leonard Goodell --- bootstrap/bootstrap.go | 7 ++++++- bootstrap/handlers/httpserver.go | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 5d76d705..4f073e18 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -181,7 +181,7 @@ func Run( useSecretProvider bool, handlers []interfaces.BootstrapHandler) { - wg, deferred, _ := RunAndReturnWaitGroup( + wg, deferred, success := RunAndReturnWaitGroup( ctx, cancel, commonFlags, @@ -195,6 +195,11 @@ func Run( handlers, ) + if !success { + cancel() + os.Exit(1) + } + defer deferred() // wait for go routines to stop executing. diff --git a/bootstrap/handlers/httpserver.go b/bootstrap/handlers/httpserver.go index 79f10850..50d52318 100644 --- a/bootstrap/handlers/httpserver.go +++ b/bootstrap/handlers/httpserver.go @@ -19,14 +19,16 @@ import ( "context" "encoding/json" "fmt" - "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger" - "github.com/edgexfoundry/go-mod-core-contracts/v2/common" - commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common" "net/http" + "os" "strconv" "sync" "time" + "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger" + "github.com/edgexfoundry/go-mod-core-contracts/v2/common" + commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common" + "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container" "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup" "github.com/edgexfoundry/go-mod-bootstrap/v2/di" @@ -123,7 +125,6 @@ func (b *HttpServer) BootstrapHandler( defer wg.Done() <-ctx.Done() - lc.Info("Web server shutting down") _ = server.Shutdown(context.Background()) lc.Info("Web server shut down") }() @@ -139,10 +140,15 @@ func (b *HttpServer) BootstrapHandler( b.isRunning = true err := server.ListenAndServe() - if err != nil { + // "Server closed" error occurs when Shutdown above is called in the Done processing, so it can be ignored + if err != nil && err.Error() != "http: Server closed" { + // Other errors occur during bootstrapping, like port bind fails, are considered fatal lc.Errorf("Web server failed: %v", err) cancel := container.CancelFuncFrom(dic.Get) - cancel() // this will caused the service to stop + cancel() // this will clean up any long-running go functions that may have started + // Give time for clean up to occur before exiting. + time.Sleep(1 * time.Millisecond) + os.Exit(1) } else { lc.Info("Web server stopped") }