diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ae9be2d4004..87b9583003c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client) [#17513](https://github.com/cosmos/cosmos-sdk/pull/17513) Allow overwritting `client.toml`. Use `client.CreateClientConfig` in place of `client.ReadFromClientConfig` and provide a custom template and a custom config. * (x/bank) [#14224](https://github.com/cosmos/cosmos-sdk/pull/14224) Allow injection of restrictions on transfers using `AppendSendRestriction` or `PrependSendRestriction`. * (genutil) [#17571](https://github.com/cosmos/cosmos-sdk/pull/17571) Allow creation of `AppGenesis` without a file lookup. +* (server) [#17094](https://github.com/cosmos/cosmos-sdk/pull/17094) Add duration `shutdown-grace` for resource clean up (closing database handles) before exit. ### Improvements diff --git a/server/start.go b/server/start.go index 28d43147e260..9bc7ef314e5f 100644 --- a/server/start.go +++ b/server/start.go @@ -8,6 +8,7 @@ import ( "net" "os" "runtime/pprof" + "time" "github.com/cometbft/cometbft/abci/server" cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" @@ -64,6 +65,7 @@ const ( FlagMinRetainBlocks = "min-retain-blocks" FlagIAVLCacheSize = "iavl-cache-size" FlagDisableIAVLFastNode = "iavl-disable-fastnode" + FlagShutdownGrace = "shutdown-grace" // state sync-related flags FlagStateSyncSnapshotInterval = "state-sync.snapshot-interval" @@ -168,9 +170,19 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. serverCtx.Logger.Info("starting ABCI without CometBFT") } - return wrapCPUProfile(serverCtx, func() error { + err = wrapCPUProfile(serverCtx, func() error { return start(serverCtx, clientCtx, appCreator, withCMT, opts) }) + + serverCtx.Logger.Debug("received quit signal") + graceDuration, _ := cmd.Flags().GetDuration(FlagShutdownGrace) + if graceDuration > 0 { + serverCtx.Logger.Info("graceful shutdown start", FlagShutdownGrace, graceDuration) + <-time.After(graceDuration) + serverCtx.Logger.Info("graceful shutdown complete") + } + + return err }, } @@ -207,6 +219,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep") cmd.Flags().Bool(FlagDisableIAVLFastNode, false, "Disable fast node for IAVL tree") cmd.Flags().Int(FlagMempoolMaxTxs, mempool.DefaultMaxTx, "Sets MaxTx value for the app-side mempool") + cmd.Flags().Duration(FlagShutdownGrace, 0*time.Second, "On Shutdown, duration to wait for resource clean up") // support old flags name for backwards compatibility cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {