Skip to content

Commit

Permalink
txapp: close active txns on early shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed May 30, 2024
1 parent 1661f0f commit 8a5a2ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/kwild/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func buildServer(d *coreDependencies, closers *closeFuncs) *Server {
// to get the comet node, we need the abci app
// to get the abci app, we need the tx router
// but the tx router needs the cometbft client
txApp := buildTxApp(d, db, e, ev, snapshotter)
txApp := buildTxApp(d, db, e, ev, snapshotter, closers)

abciApp := buildAbci(d, txApp, snapshotter, statesyncer)

Expand Down Expand Up @@ -333,7 +333,8 @@ func (c *closeFuncs) closeAll() error {
return err
}

func buildTxApp(d *coreDependencies, db *pg.DB, engine *execution.GlobalContext, ev *voting.EventStore, snapshotter *statesync.SnapshotStore) *txapp.TxApp {
func buildTxApp(d *coreDependencies, db *pg.DB, engine *execution.GlobalContext, ev *voting.EventStore,
snapshotter *statesync.SnapshotStore, closers *closeFuncs) *txapp.TxApp {
var sh txapp.Snapshotter
if snapshotter != nil {
sh = snapshotter
Expand All @@ -344,6 +345,8 @@ func buildTxApp(d *coreDependencies, db *pg.DB, engine *execution.GlobalContext,
if err != nil {
failBuild(err, "failed to build new TxApp")
}

closers.addCloser(txApp.Close, "ending any active txApp transactions")
return txApp
}

Expand Down
15 changes: 15 additions & 0 deletions internal/txapp/txapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ func (r *TxApp) Log() *log.Logger {
return &r.log
}

// Close is used to end any active database transaction that may exist if the
// application tries to shut down before closing the transaction with a call to
// Commit. Neglecting to rollback such a transaction may prevent the DB
// connection from being closed and released to the connection pool.
func (r *TxApp) Close() error {
var err error
if r.genesisTx != nil {
err = errors.Join(err, r.genesisTx.Rollback(context.Background()))
}
if r.currentTx != nil {
err = errors.Join(err, r.currentTx.Rollback(context.Background()))
}
return err
}

// GenesisInit initializes the TxApp. It must be called outside of a session,
// and before any session is started.
// It can assign the initial validator set and initial account balances.
Expand Down

0 comments on commit 8a5a2ff

Please sign in to comment.