Skip to content

Commit

Permalink
Merge pull request #220 from ethpandaops/pk910/fix-nil-pointer-in-con…
Browse files Browse the repository at this point in the history
…tract-indexer

fix nil pointer panic & make system routines more resilient against panics
  • Loading branch information
pk910 authored Jan 21, 2025
2 parents d523bf7 + d7600b8 commit dbc8579
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion indexer/beacon/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *synchronizer) stopSync() {
}

func (sync *synchronizer) runSync() {
defer utils.HandleSubroutinePanic("runSync")
defer utils.HandleSubroutinePanic("runSync", nil)

sync.runMutex.Lock()
defer sync.runMutex.Unlock()
Expand Down
5 changes: 4 additions & 1 deletion indexer/execution/consolidation_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (ci *ConsolidationIndexer) GetMatcherHeight() uint64 {

// runConsolidationIndexerLoop is the main loop for the consolidation indexer
func (ci *ConsolidationIndexer) runConsolidationIndexerLoop() {
defer utils.HandleSubroutinePanic("ConsolidationIndexer.runConsolidationIndexerLoop")
defer utils.HandleSubroutinePanic("ConsolidationIndexer.runConsolidationIndexerLoop", ci.runConsolidationIndexerLoop)

for {
time.Sleep(30 * time.Second)
Expand Down Expand Up @@ -170,6 +170,9 @@ func (ci *ConsolidationIndexer) parseRequestLog(log *types.Log, forkId *beacon.F
// get the validator indices for the source and target pubkeys
var sourceIndex, targetIndex *uint64
for index, validator := range ci.indexerCtx.beaconIndexer.GetValidatorSet(forkId) {
if validator == nil {
continue
}
if sourceIndex == nil && bytes.Equal(validator.PublicKey[:], sourcePubkey) {
index := uint64(index)
sourceIndex = &index
Expand Down
2 changes: 1 addition & 1 deletion indexer/execution/deposit_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewDepositIndexer(indexer *IndexerCtx) *DepositIndexer {

// runDepositIndexerLoop is the main loop for the deposit indexer
func (ds *DepositIndexer) runDepositIndexerLoop() {
defer utils.HandleSubroutinePanic("DepositIndexer.runDepositIndexerLoop")
defer utils.HandleSubroutinePanic("DepositIndexer.runDepositIndexerLoop", ds.runDepositIndexerLoop)

for {
time.Sleep(60 * time.Second)
Expand Down
4 changes: 2 additions & 2 deletions indexer/execution/withdrawal_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (wi *WithdrawalIndexer) GetMatcherHeight() uint64 {

// runWithdrawalIndexerLoop is the main loop for the withdrawal indexer
func (wi *WithdrawalIndexer) runWithdrawalIndexerLoop() {
defer utils.HandleSubroutinePanic("WithdrawalIndexer.runWithdrawalIndexerLoop")
defer utils.HandleSubroutinePanic("WithdrawalIndexer.runWithdrawalIndexerLoop", wi.runWithdrawalIndexerLoop)

for {
time.Sleep(30 * time.Second)
Expand Down Expand Up @@ -172,7 +172,7 @@ func (wi *WithdrawalIndexer) parseRequestLog(log *types.Log, forkId *beacon.Fork

var validatorIndex *uint64
for index, validator := range validatorSet {
if bytes.Equal(validator.PublicKey[:], validatorPubkey) {
if validator != nil && bytes.Equal(validator.PublicKey[:], validatorPubkey) {
index := uint64(index)
validatorIndex = &index
break
Expand Down
2 changes: 1 addition & 1 deletion indexer/mevrelay/mevindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (mev *MevIndexer) StartUpdater() {
}

func (mev *MevIndexer) runUpdaterLoop() {
defer utils.HandleSubroutinePanic("MevIndexer.runUpdaterLoop")
defer utils.HandleSubroutinePanic("MevIndexer.runUpdaterLoop", mev.runUpdaterLoop)

for {
time.Sleep(15 * time.Second)
Expand Down
4 changes: 2 additions & 2 deletions services/fnsignatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (tss *TxSignaturesService) LookupSignatures(sigBytes []types.TxSignatureByt
}

func (tss *TxSignaturesService) runLookupLoop() {
defer utils.HandleSubroutinePanic("txsig.loop")
defer utils.HandleSubroutinePanic("TxSignaturesService.runLookupLoop", tss.runLookupLoop)

loopInterval := utils.Config.TxSignature.LookupInterval
if loopInterval == 0 {
Expand Down Expand Up @@ -204,7 +204,7 @@ func (tss *TxSignaturesService) processPendingSignatures() {

wg.Add(1)
go func(lookup *TxSignaturesLookup) {
defer utils.HandleSubroutinePanic("txsig.lookup")
defer utils.HandleSubroutinePanic("TxSignaturesService.processPendingSignatures.func1", nil)
err := tss.lookupSignature(lookup)
if err != nil {
logger_tss.Warnf("tx signatures lookup failed: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion services/validatornames.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (vn *ValidatorNames) StartUpdater() {
}

func (vn *ValidatorNames) runUpdaterLoop() {
defer utils.HandleSubroutinePanic("ValidatorNames.runUpdaterLoop")
defer utils.HandleSubroutinePanic("ValidatorNames.runUpdaterLoop", vn.runUpdaterLoop)

for {
time.Sleep(30 * time.Second)
Expand Down
11 changes: 10 additions & 1 deletion utils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"os/signal"
"runtime/debug"
"time"

"github.com/sirupsen/logrus"
)
Expand All @@ -15,8 +16,16 @@ func WaitForCtrlC() {
<-c
}

func HandleSubroutinePanic(identifier string) {
func HandleSubroutinePanic(identifier string, restartFn func()) {
if err := recover(); err != nil {
logrus.WithError(err.(error)).Errorf("uncaught panic in %v subroutine: %v, stack: %v", identifier, err, string(debug.Stack()))

if restartFn != nil {
go func() {
// Wait for 10 seconds before restarting the subroutine
time.Sleep(10 * time.Second)
restartFn()
}()
}
}
}

0 comments on commit dbc8579

Please sign in to comment.