Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(p2p): preemptive panic recovery #174

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion p2p/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,14 @@ func (s *session[H]) doRequest(
}

// processResponses converts HeaderResponse to Header.
func (s *session[H]) processResponses(responses []*p2p_pb.HeaderResponse) ([]H, error) {
func (s *session[H]) processResponses(responses []*p2p_pb.HeaderResponse) (h []H, err error) {
defer func() {
r := recover()
if r != nil {
err = fmt.Errorf("PANIC processing responses: %s", r)
walldiss marked this conversation as resolved.
Show resolved Hide resolved
}
}()

hdrs, err := processResponses[H](responses)
if err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion p2p/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ func (s *Subscriber[H]) Stop(context.Context) error {
// SetVerifier set given verification func as Header PubSub topic validator
// Does not punish peers if *header.VerifyError is given with Uncertain set to true.
func (s *Subscriber[H]) SetVerifier(val func(context.Context, H) error) error {
pval := func(ctx context.Context, p peer.ID, msg *pubsub.Message) pubsub.ValidationResult {
pval := func(ctx context.Context, p peer.ID, msg *pubsub.Message) (res pubsub.ValidationResult) {
defer func() {
err := recover()
if err != nil {
log.Errorf("PANIC while unmarshalling or verifying header: %s", err)
res = pubsub.ValidationReject
}
}()

hdr := header.New[H]()
err := hdr.UnmarshalBinary(msg.Data)
if err != nil {
Expand Down
Loading