Skip to content

Commit

Permalink
fix(baseapp): fix BaseApp.getContext data races
Browse files Browse the repository at this point in the history
This change adds a mutex to BaseApp and firstly locks it
when invoking (*BaseApp).getContextForTx. Ideally the (types/sdk).Context
should be concurrency safe mirroring the Go standard library's
context.Context.

Fixes #18382
  • Loading branch information
odeke-em committed Nov 7, 2023
1 parent fb9dadc commit 4bc26ca
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"sort"
"strconv"
"sync"

"github.com/cockroachdb/errors"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -59,6 +60,7 @@ var _ servertypes.ABCI = (*BaseApp)(nil)
// BaseApp reflects the ABCI application implementation.
type BaseApp struct {
// initialized on creation
mu sync.Mutex // mu protects the fields below.
logger log.Logger
name string // application name from abci.BlockInfo
db dbm.DB // common DB backend
Expand Down Expand Up @@ -641,6 +643,9 @@ func (app *BaseApp) getBlockGasMeter(ctx sdk.Context) storetypes.GasMeter {

// retrieve the context for the tx w/ txBytes and other memoized values.
func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
app.mu.Lock()
defer app.mu.Unlock()

modeState := app.getState(mode)
if modeState == nil {
panic(fmt.Sprintf("state is nil for mode %v", mode))
Expand Down

0 comments on commit 4bc26ca

Please sign in to comment.