Skip to content

Commit

Permalink
fix(logic): fix out of gas on goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 17, 2023
1 parent 3df9cb2 commit b38ab90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo

panic(r)
}
if sdkCtx.GasMeter().IsOutOfGas() {
response, err = nil, sdkerrors.Wrapf(
types.LimitExceeded, "out of gas: %s (%d/%d)",
types.ModuleName, sdkCtx.GasMeter().GasConsumed(), sdkCtx.GasMeter().Limit())

return
}
}()
sdkCtx.GasMeter().ConsumeGas(sdkCtx.GasMeter().GasConsumed(), types.ModuleName)

Expand Down
9 changes: 9 additions & 0 deletions x/logic/types/gas.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"runtime"
"sync"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,6 +19,14 @@ type safeGasMeter struct {

func (m *safeGasMeter) ConsumeGas(amount uint64, descriptor string) {
m.mutex.Lock()
defer func() {
if r := recover(); r != nil {
if _, ok := r.(sdk.ErrorOutOfGas); ok {
runtime.Goexit()
}
panic(r)
}
}()
defer m.mutex.Unlock()

m.gasMeter.ConsumeGas(amount, descriptor)
Expand Down

0 comments on commit b38ab90

Please sign in to comment.