From 1b1ecf72384f57ec03e6be5915b3f988e8f17ad1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 19 May 2023 09:56:58 +0000 Subject: [PATCH] feat: add Close method for resource cleanup in graceful shutdown (backport #16193) (#16204) Co-authored-by: yihuang Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + baseapp/baseapp.go | 5 +++++ server/start.go | 6 ++++++ server/types/app.go | 3 +++ 4 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87ff6dea9989..ed426c69a3ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/staking) [#16068](https://github.com/cosmos/cosmos-sdk/pull/16068) Update simulation to allow non-EOA accounts to stake. * (server) [#16142](https://github.com/cosmos/cosmos-sdk/pull/16142) Remove JSON Indentation from the GRPC to REST gateway's responses. (Saving bandwidth) * (types) [#16145](https://github.com/cosmos/cosmos-sdk/pull/16145) Rename interface `ExtensionOptionI` back to `TxExtensionOptionI` to avoid breaking change. +* (baseapp) [#16193](https://github.com/cosmos/cosmos-sdk/pull/16193) Add `Close` method to `BaseApp` for custom app to cleanup resource in graceful shutdown. ### Bug Fixes diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index fbc8496a87d6..c8c5ed14a63e 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -1170,3 +1170,8 @@ func NoOpProcessProposal() sdk.ProcessProposalHandler { return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT} } } + +// Close is called in start cmd to gracefully cleanup resources. +func (app *BaseApp) Close() error { + return nil +} diff --git a/server/start.go b/server/start.go index 1576c0e9ae98..f3f4f657d674 100644 --- a/server/start.go +++ b/server/start.go @@ -252,6 +252,11 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error { fmt.Println(err.Error()) os.Exit(1) } + + if err = app.Close(); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } }() // Wait for SIGINT or SIGTERM signal @@ -500,6 +505,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App defer func() { if tmNode != nil && tmNode.IsRunning() { _ = tmNode.Stop() + _ = app.Close() } if traceWriterCleanup != nil { diff --git a/server/types/app.go b/server/types/app.go index bbcc6aae7233..727f767fc35e 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -63,6 +63,9 @@ type ( // Return the snapshot manager SnapshotManager() *snapshots.Manager + + // Close is called in start cmd to gracefully cleanup resources. + Close() error } // AppCreator is a function that allows us to lazily initialize an