Skip to content

Commit

Permalink
refactor(network): call app.Close() on network cleanup (#18249)
Browse files Browse the repository at this point in the history
(cherry picked from commit 139a29e)

# Conflicts:
#	testutil/network/network.go
#	testutil/network/util.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Oct 25, 2023
1 parent 98a8bfe commit 00551e9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
19 changes: 17 additions & 2 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,20 @@ type (
ValAddress sdk.ValAddress
RPCClient tmclient.Client

<<<<<<< HEAD

Check failure on line 243 in testutil/network/network.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected '}', found '<<'

Check failure on line 243 in testutil/network/network.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected ';', found '<<'

Check failure on line 243 in testutil/network/network.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected '}', found '<<' (typecheck)
tmNode *node.Node
api *api.Server
grpc *grpc.Server
grpcWeb *http.Server
=======
app servertypes.Application
tmNode *node.Node
api *api.Server
grpc *grpc.Server
grpcWeb *http.Server
errGroup *errgroup.Group
cancelFn context.CancelFunc
>>>>>>> 139a29e7e (refactor(network): call `app.Close()` on network cleanup (#18249))

Check failure on line 256 in testutil/network/network.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'

Check failure on line 256 in testutil/network/network.go

View workflow job for this annotation

GitHub Actions / golangci-lint

illegal character U+0023 '#' (typecheck)
}

// ValidatorI expose a validator's context and configuration
Expand Down Expand Up @@ -581,8 +591,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)
Expand Down Expand Up @@ -734,6 +743,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.
Expand Down
45 changes: 45 additions & 0 deletions testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/cometbft/cometbft/types"
tmtime "github.com/cometbft/cometbft/types/time"

<<<<<<< HEAD

Check failure on line 18 in testutil/network/util.go

View workflow job for this annotation

GitHub Actions / split-test-files

expected 'STRING', found '<<'

Check failure on line 18 in testutil/network/util.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 18 in testutil/network/util.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 18 in testutil/network/util.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found '<<' (typecheck)
=======
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/server"
>>>>>>> 139a29e7e (refactor(network): call `app.Close()` on network cleanup (#18249))

Check failure on line 23 in testutil/network/util.go

View workflow job for this annotation

GitHub Actions / golangci-lint

illegal character U+0023 '#' (typecheck)
"github.com/cosmos/cosmos-sdk/server/api"
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
Expand All @@ -39,7 +45,17 @@ func startInProcess(cfg Config, val *Validator) error {
}

app := cfg.AppConstructor(*val)
<<<<<<< HEAD
genDocProvider := node.DefaultGenesisDocProviderFunc(tmCfg)
=======
val.app = app

appGenesisProvider := func() (*cmttypes.GenesisDoc, error) {
appGenesis, err := genutiltypes.AppGenesisFromFile(cmtCfg.GenesisFile())
if err != nil {
return nil, err
}
>>>>>>> 139a29e7e (refactor(network): call `app.Close()` on network cleanup (#18249))

tmNode, err := node.NewNode( //resleak:notresource
tmCfg,
Expand Down Expand Up @@ -71,11 +87,40 @@ func startInProcess(cfg Config, val *Validator) error {

app.RegisterTxService(val.ClientCtx)
app.RegisterTendermintService(val.ClientCtx)
<<<<<<< HEAD
app.RegisterNodeService(val.ClientCtx)
}

if val.APIAddress != "" {
apiSrv := api.New(val.ClientCtx, logger.With("module", "api-server"))
=======
app.RegisterNodeService(val.ClientCtx, *val.AppConfig)
}

ctx := context.Background()
ctx, val.cancelFn = context.WithCancel(ctx)
val.errGroup, ctx = errgroup.WithContext(ctx)

grpcCfg := val.AppConfig.GRPC

if grpcCfg.Enable {
grpcSrv, err := servergrpc.NewGRPCServer(val.ClientCtx, app, grpcCfg)
if err != nil {
return err
}

// Start the gRPC server in a goroutine. Note, the provided ctx will ensure
// that the server is gracefully shut down.
val.errGroup.Go(func() error {
return servergrpc.StartGRPCServer(ctx, logger.With(log.ModuleKey, "grpc-server"), grpcCfg, grpcSrv)
})

val.grpc = grpcSrv
}

if val.APIAddress != "" {
apiSrv := api.New(val.ClientCtx, logger.With(log.ModuleKey, "api-server"), val.grpc)
>>>>>>> 139a29e7e (refactor(network): call `app.Close()` on network cleanup (#18249))
app.RegisterAPIRoutes(apiSrv, val.AppConfig.API)

errCh := make(chan error)
Expand Down

0 comments on commit 00551e9

Please sign in to comment.