Add panic handler (#3296)

This commit is contained in:
Preston Van Loon 2019-08-23 21:15:02 -04:00 committed by terence tsao
parent 3b422cb9c6
commit a852d610e2
2 changed files with 31 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"runtime/debug"
"time"
"github.com/gogo/protobuf/proto"
@ -85,6 +86,13 @@ func (r *RegularSync) subscribe(topic string, validate validator, handle subHand
// Pipeline decodes the incoming subscription data, runs the validation, and handles the
// message.
pipeline := func(data []byte) {
defer func() {
if r := recover(); r != nil {
log.WithField("error", r).Error("Panic occurred")
debug.PrintStack()
}
}()
if data == nil {
log.Warn("Received nil message on pubsub")
return

View File

@ -38,3 +38,26 @@ func TestSubscribe_ReceivesValidMessage(t *testing.T) {
t.Fatal("Did not receive PubSub in 1 second")
}
}
func TestSubscribe_HandlesPanic(t *testing.T) {
p2p := p2ptest.NewTestP2P(t)
r := RegularSync{
ctx: context.Background(),
p2p: p2p,
}
topic := "/eth2/voluntary_exit"
var wg sync.WaitGroup
wg.Add(1)
r.subscribe(topic, noopValidator, func(_ context.Context, msg proto.Message) error {
defer wg.Done()
panic("bad")
})
p2p.ReceivePubSub(topic, &pb.VoluntaryExit{Epoch: 55})
if testutil.WaitTimeout(&wg, time.Second) {
t.Fatal("Did not receive PubSub in 1 second")
}
}