server: run cleanupFunc before Exit (#4324)

Ensure gaiad shutdown Tendermint gracefully upon
receiving SIGINT and SIGTERM.

Closes: #4323
This commit is contained in:
Ethan Buchman 2019-05-10 22:27:11 -04:00 committed by Alessio Treglia
parent 829ce17d06
commit 77c04202a2
1 changed files with 10 additions and 7 deletions

View File

@ -216,14 +216,17 @@ func TrapSignal(cleanupFunc func()) {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
switch sig {
case syscall.SIGTERM:
defer cleanupFunc()
os.Exit(128 + int(syscall.SIGTERM))
case syscall.SIGINT:
defer cleanupFunc()
os.Exit(128 + int(syscall.SIGINT))
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)
}()
}