Skip to content

Commit

Permalink
Fix race conditions detected by race detector (#647)
Browse files Browse the repository at this point in the history
* bump iavl

* bump

* profiling

* race build

* bump

* bump
  • Loading branch information
codchen authored Mar 9, 2023
1 parent 21eee0e commit eeb6d29
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)'
all: lint install

install: go.sum
go install $(BUILD_FLAGS) ./cmd/seid
go install -race $(BUILD_FLAGS) ./cmd/seid

# In case when running seid fails with nitro issue or if you make changes to nitro, please use install-all
install-all: build-nitro install
Expand Down
24 changes: 12 additions & 12 deletions app/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@ import (
)

func (app *App) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
tracectx, topSpan := (*app.tracingInfo.Tracer).Start(context.Background(), "Block")
tracectx, topSpan := app.tracingInfo.Start("Block")
topSpan.SetAttributes(attribute.Int64("height", req.Header.Height))
app.tracingInfo.BlockSpan = &topSpan
app.tracingInfo.TracerContext = tracectx
_, beginBlockSpan := (*app.tracingInfo.Tracer).Start(app.tracingInfo.TracerContext, "BeginBlock")
app.tracingInfo.SetContext(tracectx)
_, beginBlockSpan := (*app.tracingInfo.Tracer).Start(app.tracingInfo.GetContext(), "BeginBlock")
defer beginBlockSpan.End()
return app.BaseApp.BeginBlock(ctx, req)
}

func (app *App) MidBlock(ctx sdk.Context, height int64) []abci.Event {
_, span := (*app.tracingInfo.Tracer).Start(app.tracingInfo.TracerContext, "MidBlock")
_, span := app.tracingInfo.Start("MidBlock")
defer span.End()
return app.BaseApp.MidBlock(ctx, height)
}

func (app *App) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
_, span := (*app.tracingInfo.Tracer).Start(app.tracingInfo.TracerContext, "EndBlock")
_, span := app.tracingInfo.Start("EndBlock")
defer span.End()
return app.BaseApp.EndBlock(ctx, req)
}

func (app *App) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abci.ResponseCheckTx, error) {
_, span := (*app.tracingInfo.Tracer).Start(app.tracingInfo.TracerContext, "CheckTx")
_, span := app.tracingInfo.Start("CheckTx")
defer span.End()
return app.BaseApp.CheckTx(ctx, req)
}

func (app *App) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx) abci.ResponseDeliverTx {
defer metrics.MeasureDeliverTxDuration(time.Now())
tracectx, span := (*app.tracingInfo.Tracer).Start(app.tracingInfo.TracerContext, "DeliverTx")
oldCtx := app.tracingInfo.TracerContext
app.tracingInfo.TracerContext = tracectx
tracectx, span := app.tracingInfo.Start("DeliverTx")
oldCtx := app.tracingInfo.GetContext()
app.tracingInfo.SetContext(tracectx)
defer span.End()
defer func() { app.tracingInfo.TracerContext = oldCtx }()
defer func() { app.tracingInfo.SetContext(oldCtx) }()
return app.BaseApp.DeliverTx(ctx, req)
}

func (app *App) Commit(ctx context.Context) (res *abci.ResponseCommit, err error) {
if app.tracingInfo.BlockSpan != nil {
defer (*app.tracingInfo.BlockSpan).End()
}
_, span := (*app.tracingInfo.Tracer).Start(app.tracingInfo.TracerContext, "Commit")
_, span := app.tracingInfo.Start("Commit")
defer span.End()
app.tracingInfo.TracerContext = context.Background()
app.tracingInfo.SetContext(context.Background())
app.tracingInfo.BlockSpan = nil
return app.BaseApp.Commit(ctx)
}
9 changes: 5 additions & 4 deletions app/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
otel.SetTracerProvider(defaultTracer)
tr := defaultTracer.Tracer("component-main")

tracingInfo := &tracing.Info{
Tracer: &tr,
}
tracingInfo.SetContext(context.Background())
antehandler, anteDepGenerator, err := app.NewAnteHandlerAndDepGenerator(
app.HandlerOptions{
HandlerOptions: ante.HandlerOptions{
Expand All @@ -87,10 +91,7 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
DexKeeper: &suite.App.DexKeeper,
NitroKeeper: &suite.App.NitroKeeper,
AccessControlKeeper: &suite.App.AccessControlKeeper,
TracingInfo: &tracing.Info{
Tracer: &tr,
TracerContext: context.Background(),
},
TracingInfo: tracingInfo,
},
)

Expand Down
2 changes: 1 addition & 1 deletion app/antedecorators/traced.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewTracedAnteDecorator(wrapped sdk.AnteDecorator, tracingInfo *tracing.Info

func (d TracedAnteDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if d.tracingInfo != nil {
_, span := (*d.tracingInfo.Tracer).Start(d.tracingInfo.TracerContext, d.traceName)
_, span := d.tracingInfo.Start(d.traceName)
defer span.End()
}
return d.wrapped.AnteHandle(ctx, tx, simulate, next)
Expand Down
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,12 @@ func New(
tkeys: tkeys,
memKeys: memKeys,
tracingInfo: &tracing.Info{
Tracer: &tr,
TracerContext: context.Background(),
Tracer: &tr,
},
txDecoder: encodingConfig.TxConfig.TxDecoder(),
versionInfo: version.NewInfo(),
}
app.tracingInfo.SetContext(context.Background())

app.ParamsKeeper = initParamsKeeper(appCodec, cdc, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])

Expand Down
5 changes: 5 additions & 0 deletions cmd/seid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"math"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -61,6 +63,9 @@ func (s *rootOptions) apply(options ...Option) { //nolint:unused // I figure thi

// NewRootCmd creates a new root command for a Cosmos SDK application
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
go func() {
http.ListenAndServe(":6060", nil)
}()
encodingConfig := app.MakeEncodingConfig()
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
go.opentelemetry.io/otel/trace v1.9.0
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/text v0.7.0
google.golang.org/genproto v0.0.0-20230223222841-637eb2293923
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
Expand Down Expand Up @@ -255,7 +256,6 @@ require (
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.2.0 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
honnef.co/go/tools v0.3.1 // indirect
Expand All @@ -269,8 +269,8 @@ require (
replace (
github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.0.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.1
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.2
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.1-tony-2
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.2-tony-3
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.1.177
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1066,10 +1066,10 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/sei-cosmos v0.2.1 h1:u1yUGasR7iDMVb2VMDmsCWx6duQQwvuq6iTwHrRc7dc=
github.com/sei-protocol/sei-cosmos v0.2.1/go.mod h1:LUhhplOtRXliV1x17gbOptKPy90xSK0Sxv8zmgsMvTY=
github.com/sei-protocol/sei-iavl v0.1.2 h1:jEYkZv83DbTRapJtkT5gBFa2uEBwD4udw8AiYx0gcsI=
github.com/sei-protocol/sei-iavl v0.1.2/go.mod h1:7PfkEVT5dcoQE+s/9KWdoXJ8VVVP1QpYYPLdxlkSXFk=
github.com/sei-protocol/sei-cosmos v0.2.1-tony-2 h1:RFs3RZ0VVV9IFTsK/0WX9E/p8axapU51+rtYinU08Lc=
github.com/sei-protocol/sei-cosmos v0.2.1-tony-2/go.mod h1:LUhhplOtRXliV1x17gbOptKPy90xSK0Sxv8zmgsMvTY=
github.com/sei-protocol/sei-iavl v0.1.2-tony-3 h1:udXCIcgpbJjX09c3udk8IlAyTRQyFWhZjw5SAgcuqaI=
github.com/sei-protocol/sei-iavl v0.1.2-tony-3/go.mod h1:7PfkEVT5dcoQE+s/9KWdoXJ8VVVP1QpYYPLdxlkSXFk=
github.com/sei-protocol/sei-tendermint v0.1.177 h1:gn6/z82eGBBdyRgEyd8wpbB0C3/l1BhjzcsiCFoSrvo=
github.com/sei-protocol/sei-tendermint v0.1.177/go.mod h1:+BtDvAwTkE64BlxzpH9ZP7S6vUYT9wRXiZa/WW8/o4g=
github.com/sei-protocol/sei-tm-db v0.0.5 h1:3WONKdSXEqdZZeLuWYfK5hP37TJpfaUa13vAyAlvaQY=
Expand Down
26 changes: 25 additions & 1 deletion utils/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tracing

import (
"context"
"sync"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/jaeger"
Expand Down Expand Up @@ -46,6 +47,29 @@ func GetTracerProviderOptions(url string) ([]trace.TracerProviderOption, error)

type Info struct {
Tracer *otrace.Tracer
TracerContext context.Context
tracerContext context.Context
BlockSpan *otrace.Span

mtx sync.RWMutex
}

func (i *Info) Start(name string) (context.Context, otrace.Span) {
i.mtx.Lock()
defer i.mtx.Unlock()
if i.tracerContext == nil {
i.tracerContext = context.Background()
}
return (*i.Tracer).Start(i.tracerContext, "DeliverTx")
}

func (i *Info) GetContext() context.Context {
i.mtx.RLock()
defer i.mtx.RUnlock()
return i.tracerContext
}

func (i *Info) SetContext(c context.Context) {
i.mtx.Lock()
defer i.mtx.Unlock()
i.tracerContext = c
}
2 changes: 1 addition & 1 deletion x/dex/contract/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type environment struct {

func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo []types.ContractInfoV2, tracingInfo *tracing.Info) ([]types.ContractInfoV2, sdk.Context, bool) {
tracer := tracingInfo.Tracer
spanCtx, span := (*tracer).Start(tracingInfo.TracerContext, "DexEndBlockerAtomic")
spanCtx, span := tracingInfo.Start("DexEndBlockerAtomic")
defer span.End()
env := newEnv(ctx, validContractsInfo, keeper)
cachedCtx, msCached := cacheContext(ctx, env)
Expand Down
6 changes: 3 additions & 3 deletions x/dex/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (am AppModule) getAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 {
// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
defer func() {
_, span := (*am.tracingInfo.Tracer).Start(am.tracingInfo.TracerContext, "DexBeginBlockRollback")
_, span := am.tracingInfo.Start("DexBeginBlockRollback")
defer span.End()
}()

Expand All @@ -245,7 +245,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
}

func (am AppModule) beginBlockForContract(ctx sdk.Context, contract types.ContractInfoV2, epoch int64, gasLimit uint64) {
_, span := (*am.tracingInfo.Tracer).Start(am.tracingInfo.TracerContext, "DexBeginBlock")
_, span := am.tracingInfo.Start("DexBeginBlock")
contractAddr := contract.ContractAddr
span.SetAttributes(attribute.String("contract", contractAddr))
defer span.End()
Expand All @@ -271,7 +271,7 @@ func (am AppModule) beginBlockForContract(ctx sdk.Context, contract types.Contra
// EndBlock executes all ABCI EndBlock logic respective to the capability module. It
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) (ret []abci.ValidatorUpdate) {
_, span := (*am.tracingInfo.Tracer).Start(am.tracingInfo.TracerContext, "DexEndBlock")
_, span := am.tracingInfo.Start("DexEndBlock")
defer span.End()
defer dexutils.GetMemState(ctx.Context()).Clear(ctx)

Expand Down

0 comments on commit eeb6d29

Please sign in to comment.