From af758d7d6d709858f74fc6ea2477a18ecba98b0a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:08:32 +0200 Subject: [PATCH] refactor(network): call `app.Close()` on network cleanup (backport #18249) (#18251) Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + server/start.go | 36 ++++++++++++++++++------------------ testutil/network/network.go | 10 ++++++++-- testutil/network/util.go | 1 + 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b496daa03093..3d99941e53d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (server) [#18251](https://github.com/cosmos/cosmos-sdk/pull/18251) Call `baseapp.Close()` when app started as grpc only. * (baseapp) [#17769](https://github.com/cosmos/cosmos-sdk/pull/17769) Ensure we respect block size constraints in the `DefaultProposalHandler`'s `PrepareProposal` handler when a nil or no-op mempool is used. We provide a `TxSelector` type to assist in making transaction selection generalized. We also fix a comparison bug in tx selection when `req.maxTxBytes` is reached. * (config) [#17649](https://github.com/cosmos/cosmos-sdk/pull/17649) Fix `mempool.max-txs` configuration is invalid in `app.config`. * (mempool) [#17668](https://github.com/cosmos/cosmos-sdk/pull/17668) Fix `PriorityNonceIterator.Next()` nil pointer ref for min priority at the end of iteration. diff --git a/server/start.go b/server/start.go index 31deb30745dc..aa8f5c47734b 100644 --- a/server/start.go +++ b/server/start.go @@ -304,7 +304,6 @@ func startStandAlone(ctx *Context, clientCtx client.Context, appCreator types.Ap defer func() { _ = svr.Stop() - _ = app.Close() if apiSrv != nil { @@ -437,6 +436,24 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App } } + defer func() { + if tmNode != nil && tmNode.IsRunning() { + _ = tmNode.Stop() + } + + if traceWriterCleanup != nil { + traceWriterCleanup() + } + + _ = app.Close() + + if apiSrv != nil { + _ = apiSrv.Close() + } + + ctx.Logger.Info("exiting...") + }() + // At this point it is safe to block the process if we're in gRPC only mode as // we do not need to start Rosetta or handle any Tendermint related processes. if gRPCOnly { @@ -494,23 +511,6 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App } } - defer func() { - if tmNode != nil && tmNode.IsRunning() { - _ = tmNode.Stop() - _ = app.Close() - } - - if traceWriterCleanup != nil { - traceWriterCleanup() - } - - if apiSrv != nil { - _ = apiSrv.Close() - } - - ctx.Logger.Info("exiting...") - }() - // wait for signal capture and gracefully return return WaitForQuitSignals() } diff --git a/testutil/network/network.go b/testutil/network/network.go index 0724a97f3b3a..ef065d9e68b8 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -240,6 +240,7 @@ type ( ValAddress sdk.ValAddress RPCClient tmclient.Client + app servertypes.Application tmNode *node.Node api *api.Server grpc *grpc.Server @@ -581,8 +582,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { l.Log("starting test network...") for idx, v := range network.Validators { - err := startInProcess(cfg, v) - if err != nil { + if err := startInProcess(cfg, v); err != nil { return nil, err } l.Log("started validator", idx) @@ -734,6 +734,12 @@ func (n *Network) Cleanup() { _ = v.grpcWeb.Close() } } + + if v.app != nil { + if err := v.app.Close(); err != nil { + n.Logger.Log("failed to stop validator ABCI application", "err", err) + } + } } // Give a brief pause for things to finish closing in other processes. Hopefully this helps with the address-in-use errors. diff --git a/testutil/network/util.go b/testutil/network/util.go index 24ad548f9418..e67a96f4433f 100644 --- a/testutil/network/util.go +++ b/testutil/network/util.go @@ -39,6 +39,7 @@ func startInProcess(cfg Config, val *Validator) error { } app := cfg.AppConstructor(*val) + val.app = app genDocProvider := node.DefaultGenesisDocProviderFunc(tmCfg) tmNode, err := node.NewNode( //resleak:notresource