From 1bb632a567937b25dc9f5638c115427f0356fddb Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Tue, 7 Mar 2023 10:12:39 -0500 Subject: [PATCH] chore: prefer to panic instead of os.Exit (#15285) --- server/config/toml.go | 3 +-- server/util.go | 24 ------------------------ simapp/app.go | 7 +++---- simapp/app_v2.go | 4 ++-- testutil/network/network.go | 28 +++++++++++++++++++++++++++- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/server/config/toml.go b/server/config/toml.go index ecf9dab9e4..b15b5c85fd 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -282,7 +282,6 @@ func WriteConfigFile(configFilePath string, config interface{}) { func mustWriteFile(filePath string, contents []byte, mode os.FileMode) { if err := os.WriteFile(filePath, contents, mode); err != nil { - fmt.Printf(fmt.Sprintf("failed to write file: %v", err) + "\n") - os.Exit(1) + panic(fmt.Errorf("failed to write file: %w", err)) } } diff --git a/server/util.go b/server/util.go index 310051cc4a..f15c62fb8e 100644 --- a/server/util.go +++ b/server/util.go @@ -352,30 +352,6 @@ func ExternalIP() (string, error) { return "", errors.New("are you connected to the network?") } -// TrapSignal traps SIGINT and SIGTERM and terminates the server correctly. -func TrapSignal(cleanupFunc func()) { - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - - go func() { - sig := <-sigs - - if cleanupFunc != nil { - cleanupFunc() - } - exitCode := 128 - - switch sig { - case syscall.SIGINT: - exitCode += int(syscall.SIGINT) - case syscall.SIGTERM: - exitCode += int(syscall.SIGTERM) - } - - os.Exit(exitCode) - }() -} - // ListenForQuitSignals listens for SIGINT and SIGTERM. When a signal is received, // the cleanup function is called, indicating the caller can gracefully exit or // return. diff --git a/simapp/app.go b/simapp/app.go index 6133c6b392..befebfbeb6 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -4,6 +4,7 @@ package simapp import ( "encoding/json" + "fmt" "io" "os" "path/filepath" @@ -261,8 +262,7 @@ func NewSimApp( // register the streaming service with the BaseApp if err := bApp.SetStreamingService(appOpts, appCodec, keys); err != nil { - logger.Error("failed to load state streaming", "err", err) - os.Exit(1) + panic(fmt.Errorf("failed to load state streaming: %w", err)) } app := &SimApp{ @@ -517,8 +517,7 @@ func NewSimApp( if loadLatest { if err := app.LoadLatestVersion(); err != nil { - logger.Error("error on loading last version", "err", err) - os.Exit(1) + panic(fmt.Errorf("error loading last version: %w", err)) } } diff --git a/simapp/app_v2.go b/simapp/app_v2.go index d1c19d59b4..059f073482 100644 --- a/simapp/app_v2.go +++ b/simapp/app_v2.go @@ -4,6 +4,7 @@ package simapp import ( _ "embed" + "fmt" "io" "os" "path/filepath" @@ -246,8 +247,7 @@ func NewSimApp( app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...) if err := app.App.BaseApp.SetStreamingService(appOpts, app.appCodec, app.kvStoreKeys()); err != nil { - logger.Error("failed to load state streaming", "err", err) - os.Exit(1) + panic(fmt.Errorf("failed to load state streaming: %w", err)) } /**** Module Options ****/ diff --git a/testutil/network/network.go b/testutil/network/network.go index 4edaa075f0..aaf898023b 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -9,9 +9,11 @@ import ( "net/http" "net/url" "os" + "os/signal" "path/filepath" "strings" "sync" + "syscall" "testing" "time" @@ -612,11 +614,35 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { // Ensure we cleanup incase any test was abruptly halted (e.g. SIGINT) as any // defer in a test would not be called. - server.TrapSignal(network.Cleanup) + trapSignal(network.Cleanup) return network, nil } +// trapSignal traps SIGINT and SIGTERM and calls os.Exit once a signal is received. +func trapSignal(cleanupFunc func()) { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + go func() { + sig := <-sigs + + if cleanupFunc != nil { + cleanupFunc() + } + exitCode := 128 + + switch sig { + case syscall.SIGINT: + exitCode += int(syscall.SIGINT) + case syscall.SIGTERM: + exitCode += int(syscall.SIGTERM) + } + + os.Exit(exitCode) + }() +} + // LatestHeight returns the latest height of the network or an error if the // query fails or no validators exist. func (n *Network) LatestHeight() (int64, error) {