From 67454b15b56a17841ec89576dbf0ac1d8795b6c7 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 17 May 2023 11:23:43 +0800 Subject: [PATCH 1/2] feat: add Close method for resource cleanup in graceful shutdown Closes: #16192 --- CHANGELOG.md | 1 + baseapp/baseapp.go | 5 +++++ server/start.go | 4 +++- server/types/app.go | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd6e4f612921..1f6497ebeed7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,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 * (store) [#16067](https://github.com/cosmos/cosmos-sdk/pull/16067) Add local snapshots management commands. * (server) [#16142](https://github.com/cosmos/cosmos-sdk/pull/16142) Remove JSON Indentation from the GRPC to REST gateway's responses. (Saving bandwidth) +* (baseapp) [#]() Add `Close` method to `BaseApp` for custom app to cleanup resource in graceful shutdown. ### State Machine Breaking diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index b89e88bd98dc..bffe143f6d54 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -1031,3 +1031,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 882ec02b105c..ec1f76756be6 100644 --- a/server/start.go +++ b/server/start.go @@ -2,6 +2,7 @@ package server import ( "context" + "errors" "fmt" "net" "os" @@ -252,7 +253,7 @@ func startStandAlone(svrCtx *Context, appCreator types.AppCreator) error { // so we can gracefully stop the ABCI server. <-ctx.Done() svrCtx.Logger.Info("stopping the ABCI server...") - return svr.Stop() + return errors.Join(svr.Stop(), app.Close()) }) return g.Wait() @@ -457,6 +458,7 @@ func startInProcess(svrCtx *Context, clientCtx client.Context, appCreator types. 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 1af0b0bf57ec..4b94665c7280 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -58,6 +58,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 From 1a78079bf376abace3741af1397149a0d632f034 Mon Sep 17 00:00:00 2001 From: yihuang Date: Wed, 17 May 2023 11:26:28 +0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6497ebeed7..32b840dc245c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,7 +115,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 * (store) [#16067](https://github.com/cosmos/cosmos-sdk/pull/16067) Add local snapshots management commands. * (server) [#16142](https://github.com/cosmos/cosmos-sdk/pull/16142) Remove JSON Indentation from the GRPC to REST gateway's responses. (Saving bandwidth) -* (baseapp) [#]() Add `Close` method to `BaseApp` for custom app to cleanup resource in graceful shutdown. +* (baseapp) [#16193](https://github.com/cosmos/cosmos-sdk/pull/16193) Add `Close` method to `BaseApp` for custom app to cleanup resource in graceful shutdown. ### State Machine Breaking