Skip to content

Commit

Permalink
shutdown gracefully without os.Exit (#7480)
Browse files Browse the repository at this point in the history
* shutdown gracefully without os.Exit

* Update server/util.go

Co-authored-by: Alessio Treglia <quadrispro@ubuntu.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
  • Loading branch information
3 people authored Oct 14, 2020
1 parent 652d5d9 commit f260ca4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
16 changes: 8 additions & 8 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error {
tmos.Exit(err.Error())
}

TrapSignal(func() {
defer func() {
if err = svr.Stop(); err != nil {
tmos.Exit(err.Error())
}
})
}()

// run forever (the node will not be returned)
select {}
// Wait for SIGINT or SIGTERM signal
return WaitForQuitSignals()
}

// legacyAminoCdc is used for the legacy REST API
Expand Down Expand Up @@ -290,7 +290,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
}
}

TrapSignal(func() {
defer func() {
if tmNode.IsRunning() {
_ = tmNode.Stop()
}
Expand All @@ -308,8 +308,8 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
}

ctx.Logger.Info("exiting...")
})
}()

// run forever (the node will not be returned)
select {}
// Wait for SIGINT or SIGTERM signal
return WaitForQuitSignals()
}
18 changes: 18 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"

Expand Down Expand Up @@ -39,6 +40,15 @@ type Context struct {
Logger log.Logger
}

// ErrorCode contains the exit code for server exit.
type ErrorCode struct {
Code int
}

func (e ErrorCode) Error() string {
return strconv.Itoa(e.Code)
}

func NewDefaultContext() *Context {
return NewContext(viper.New(), tmcfg.DefaultConfig(), log.NewTMLogger(log.NewSyncWriter(os.Stdout)))
}
Expand Down Expand Up @@ -245,6 +255,14 @@ func TrapSignal(cleanupFunc func()) {
}()
}

// WaitForQuitSignals waits for SIGINT and SIGTERM and returns.
func WaitForQuitSignals() ErrorCode {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
return ErrorCode{Code: int(sig.(syscall.Signal)) + 128}
}

func skipInterface(iface net.Interface) bool {
if iface.Flags&net.FlagUp == 0 {
return true // interface down
Expand Down
8 changes: 7 additions & 1 deletion simapp/simd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package main
import (
"os"

"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp/simd/cmd"
)

func main() {
rootCmd, _ := cmd.NewRootCmd()
if err := cmd.Execute(rootCmd); err != nil {
os.Exit(1)
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}

0 comments on commit f260ca4

Please sign in to comment.