Skip to content

Commit

Permalink
Add catch all panic handler in dex endblock
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Dec 29, 2023
1 parent 3a53526 commit ff3b08d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
9 changes: 9 additions & 0 deletions x/dex/contract/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ func handleUnfulfilledMarketOrders(ctx context.Context, sdkCtx sdk.Context, env
}

func orderMatchingRunnable(ctx context.Context, sdkContext sdk.Context, env *environment, keeper *keeper.Keeper, contractInfo types.ContractInfoV2, tracer *otrace.Tracer) {
defer func() {
if err := recover(); err != nil {
msg := fmt.Sprintf("PANIC RECOVERED during order matching: %s", err)
sdkContext.Logger().Error(msg)
if env != nil {
env.addError(contractInfo.ContractAddr, errors.New(msg))
}

Check warning on line 237 in x/dex/contract/abci.go

View check run for this annotation

Codecov / codecov/patch

x/dex/contract/abci.go#L231-L237

Added lines #L231 - L237 were not covered by tests
}
}()
_, span := (*tracer).Start(ctx, "orderMatchingRunnable")
defer span.End()
defer telemetry.MeasureSince(time.Now(), "dex", "order_matching_runnable")
Expand Down
18 changes: 9 additions & 9 deletions x/dex/contract/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package contract
import (
"context"
"fmt"
"sync"
"time"

otrace "go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -130,16 +129,17 @@ func ExecutePairsInParallel(ctx sdk.Context, contractAddr string, dexkeeper *kee
cancelResults := []*types.Cancellation{}
settlements := []*types.SettlementEntry{}

mu := sync.Mutex{}
wg := sync.WaitGroup{}
// mu := sync.Mutex{}
// wg := sync.WaitGroup{}

for _, pair := range registeredPairs {
wg.Add(1)
// wg.Add(1)

pair := pair
pairCtx := ctx.WithMultiStore(multi.NewStore(ctx.MultiStore(), GetPerPairWhitelistMap(contractAddr, pair))).WithEventManager(sdk.NewEventManager())
go func() {
defer wg.Done()
// go func() {
func() {
// defer wg.Done()
pairCopy := pair
pairStr := types.GetPairString(&pairCopy)
orderbook, found := orderBooks.Load(pairStr)
Expand All @@ -150,8 +150,8 @@ func ExecutePairsInParallel(ctx sdk.Context, contractAddr string, dexkeeper *kee
orderIDToSettledQuantities := GetOrderIDToSettledQuantities(pairSettlements)
PrepareCancelUnfulfilledMarketOrders(pairCtx, typedContractAddr, pairCopy, orderIDToSettledQuantities)

mu.Lock()
defer mu.Unlock()
// mu.Lock()
// defer mu.Unlock()
orders, cancels := GetMatchResults(ctx, typedContractAddr, pairCopy)
orderResults = append(orderResults, orders...)
cancelResults = append(cancelResults, cancels...)
Expand All @@ -160,7 +160,7 @@ func ExecutePairsInParallel(ctx sdk.Context, contractAddr string, dexkeeper *kee
ctx.EventManager().EmitEvents(pairCtx.EventManager().Events())
}()
}
wg.Wait()
// wg.Wait()
dexkeeper.SetMatchResult(ctx, contractAddr, types.NewMatchResult(orderResults, cancelResults, settlements))

return settlements
Expand Down
6 changes: 6 additions & 0 deletions x/dex/contract/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func (r *ParallelRunner) Run() {
}

func (r *ParallelRunner) wrapRunnable(contractAddr types.ContractAddress) {
defer func() {
if err := recover(); err != nil {
r.sdkCtx.Logger().Error(fmt.Sprintf("panic in parallel runner recovered: %s", err))
}

Check warning on line 140 in x/dex/contract/runner.go

View check run for this annotation

Codecov / codecov/patch

x/dex/contract/runner.go#L139-L140

Added lines #L139 - L140 were not covered by tests
}()

contractInfo, _ := r.contractAddrToInfo.Load(contractAddr)
r.runnable(*contractInfo)

Expand Down
5 changes: 5 additions & 0 deletions x/dex/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ func (am AppModule) getPriceToDelete(
// 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) {
defer func() {
if err := recover(); err != nil {
ctx.Logger().Error(fmt.Sprintf("panic in endblock recovered: %s", err))
}

Check warning on line 310 in x/dex/module.go

View check run for this annotation

Codecov / codecov/patch

x/dex/module.go#L309-L310

Added lines #L309 - L310 were not covered by tests
}()
_, span := am.tracingInfo.Start("DexEndBlock")
defer span.End()
defer dexutils.GetMemState(ctx.Context()).Clear(ctx)
Expand Down

0 comments on commit ff3b08d

Please sign in to comment.