Skip to content

Commit 0032812

Browse files
authored
fix: allow mempool interrupt while it is still ongoing (#495)
* add context cancel * simplify channel cases * add changelog * revert config change * add minimal change set for sigint * rename * offload server shutdown go goroutine * remove goroutine in favor of timeout * use short timeout
1 parent 68436ec commit 0032812

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
### BUG FIXES
88

9-
- [\#471](https://github.com/cosmos/evm/pull/471) Notify new block for mempool in time.
9+
- [\#471](https://github.com/cosmos/evm/pull/471) Notify new block for mempool in time
1010
- [\#492](https://github.com/cosmos/evm/pull/492) Duplicate case switch to avoid empty execution block
11-
- [\#509](https://github.com/cosmos/evm/pull/509) Allow value with slashes when query token_pairs.
11+
- [\#509](https://github.com/cosmos/evm/pull/509) Allow value with slashes when query token_pairs
12+
- [\#495](https://github.com/cosmos/evm/pull/495) Allow immediate SIGINT interrupt when mempool is not empty
1213

1314
### IMPROVEMENTS
1415

evmd/app.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,19 +1146,22 @@ func (app *EVMD) SetClientCtx(clientCtx client.Context) {
11461146
app.clientCtx = clientCtx
11471147
}
11481148

1149-
// Close unsubscribes from the CometBFT event bus (if set) and closes the underlying BaseApp.
1149+
// Close unsubscribes from the CometBFT event bus (if set) and closes the mempool and underlying BaseApp.
11501150
func (app *EVMD) Close() error {
11511151
var err error
11521152
if m, ok := app.GetMempool().(*evmmempool.ExperimentalEVMMempool); ok {
1153+
app.Logger().Info("Shutting down mempool")
11531154
err = m.Close()
11541155
}
1155-
err = errors.Join(err, app.BaseApp.Close())
1156+
11561157
msg := "Application gracefully shutdown"
1158+
err = errors.Join(err, app.BaseApp.Close())
11571159
if err == nil {
11581160
app.Logger().Info(msg)
11591161
} else {
11601162
app.Logger().Error(msg, "error", err)
11611163
}
1164+
11621165
return err
11631166
}
11641167

mempool/mempool.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,20 @@ func (m *ExperimentalEVMMempool) SetEventBus(eventBus *cmttypes.EventBus) {
396396
}()
397397
}
398398

399-
// Close unsubscribes from the CometBFT event bus.
399+
// Close unsubscribes from the CometBFT event bus and shuts down the mempool.
400400
func (m *ExperimentalEVMMempool) Close() error {
401+
var errs []error
401402
if m.eventBus != nil {
402-
return m.eventBus.Unsubscribe(context.Background(), SubscriberName, stream.NewBlockHeaderEvents)
403+
if err := m.eventBus.Unsubscribe(context.Background(), SubscriberName, stream.NewBlockHeaderEvents); err != nil {
404+
errs = append(errs, fmt.Errorf("failed to unsubscribe from event bus: %w", err))
405+
}
403406
}
404-
return nil
407+
408+
if err := m.txPool.Close(); err != nil {
409+
errs = append(errs, fmt.Errorf("failed to close txpool: %w", err))
410+
}
411+
412+
return errors.Join(errs...)
405413
}
406414

407415
// getEVMMessage validates that the transaction contains exactly one message and returns it if it's an EVM message.

server/json_rpc.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/cosmos/cosmos-sdk/server"
2626
)
2727

28-
const shutdownTimeout = 5 * time.Second
28+
const shutdownTimeout = 200 * time.Millisecond
2929

3030
type AppWithPendingTxStream interface {
3131
RegisterPendingTxListener(listener func(common.Hash))
@@ -111,14 +111,13 @@ func StartJSONRPC(
111111
case <-ctx.Done():
112112
// The calling process canceled or closed the provided context, so we must
113113
// gracefully stop the JSON-RPC server.
114-
logger.Info("stopping JSON-RPC server...", "address", config.JSONRPC.Address)
114+
logger.Info("stopping JSON-RPC server...", "address", config.JSONRPC.Address, "timeout", shutdownTimeout)
115115
ctxShutdown, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
116116
defer cancel()
117117
if err := httpSrv.Shutdown(ctxShutdown); err != nil {
118118
logger.Error("failed to shutdown JSON-RPC server", "error", err.Error())
119119
}
120120
return nil
121-
122121
case err := <-errCh:
123122
if err == http.ErrServerClosed {
124123
close(httpSrvDone)

server/start.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ which accepts a path for the resulting pprof file.
132132
return err
133133
}
134134

135-
withTM, _ := cmd.Flags().GetBool(srvflags.WithCometBFT)
136-
if !withTM {
135+
withbft, _ := cmd.Flags().GetBool(srvflags.WithCometBFT)
136+
if !withbft {
137137
serverCtx.Logger.Info("starting ABCI without CometBFT")
138138
return wrapCPUProfile(serverCtx, func() error {
139139
return startStandAlone(serverCtx, clientCtx, opts)
@@ -400,7 +400,7 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
400400
genDocProvider := GenDocProvider(cfg)
401401

402402
var (
403-
tmNode *node.Node
403+
bftNode *node.Node
404404
gRPCOnly = svrCtx.Viper.GetBool(srvflags.GRPCOnly)
405405
)
406406

@@ -412,7 +412,7 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
412412
logger.Info("starting node with ABCI CometBFT in-process")
413413

414414
cmtApp := server.NewCometABCIWrapper(app)
415-
tmNode, err = node.NewNode(
415+
bftNode, err = node.NewNode(
416416
cfg,
417417
pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()),
418418
nodeKey,
@@ -427,26 +427,26 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
427427
return err
428428
}
429429

430-
if err := tmNode.Start(); err != nil {
430+
if err := bftNode.Start(); err != nil {
431431
logger.Error("failed start CometBFT server", "error", err.Error())
432432
return err
433433
}
434434

435435
if m, ok := evmApp.GetMempool().(*evmmempool.ExperimentalEVMMempool); ok {
436-
m.SetEventBus(tmNode.EventBus())
436+
m.SetEventBus(bftNode.EventBus())
437437
}
438438
defer func() {
439-
if tmNode.IsRunning() {
440-
_ = tmNode.Stop()
439+
if bftNode.IsRunning() {
440+
_ = bftNode.Stop()
441441
}
442442
}()
443443
}
444444

445445
// Add the tx service to the gRPC router. We only need to register this
446446
// service if API or gRPC or JSONRPC is enabled, and avoid doing so in the general
447447
// case, because it spawns a new local CometBFT RPC client.
448-
if (config.API.Enable || config.GRPC.Enable || config.JSONRPC.Enable || config.JSONRPC.EnableIndexer) && tmNode != nil {
449-
clientCtx = clientCtx.WithClient(local.New(tmNode))
448+
if (config.API.Enable || config.GRPC.Enable || config.JSONRPC.Enable || config.JSONRPC.EnableIndexer) && bftNode != nil {
449+
clientCtx = clientCtx.WithClient(local.New(bftNode))
450450

451451
app.RegisterTxService(clientCtx)
452452
app.RegisterTendermintService(clientCtx)

0 commit comments

Comments
 (0)