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

autonatv2: recover from panics #2992

Merged
merged 2 commits into from
Oct 4, 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
4 changes: 2 additions & 2 deletions p2p/protocol/autonatv2/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const (
DialProtocol = "/libp2p/autonat/2/dial-request"

maxMsgSize = 8192
streamTimeout = time.Minute
streamTimeout = 15 * time.Second
dialBackStreamTimeout = 5 * time.Second
dialBackDialTimeout = 30 * time.Second
dialBackDialTimeout = 10 * time.Second
dialBackMaxMsgSize = 1024
minHandshakeSizeBytes = 30_000 // for amplification attack prevention
maxHandshakeSizeBytes = 100_000
Expand Down
1 change: 0 additions & 1 deletion p2p/protocol/autonatv2/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,5 +657,4 @@ func TestAreAddrsConsistency(t *testing.T) {
}
})
}

}
9 changes: 9 additions & 0 deletions p2p/protocol/autonatv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package autonatv2
import (
"context"
"fmt"
"os"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -248,6 +250,13 @@ func newDialRequest(reqs []Request, nonce uint64) pb.Message {

// handleDialBack receives the nonce on the dial-back stream
func (ac *client) handleDialBack(s network.Stream) {
defer func() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to ensure that the stream has been closed?

Copy link
Member Author

@sukunrt sukunrt Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We should reset the stream on panic. aa2caf0

if rerr := recover(); rerr != nil {
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
}
s.Reset()
}()

if err := s.Scope().SetService(ServiceName); err != nil {
log.Debugf("failed to attach stream to service %s: %w", ServiceName, err)
s.Reset()
Expand Down
10 changes: 10 additions & 0 deletions p2p/protocol/autonatv2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"io"
"os"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -88,6 +90,14 @@ func (as *server) Close() {

// handleDialRequest is the dial-request protocol stream handler
func (as *server) handleDialRequest(s network.Stream) {
defer func() {
if rerr := recover(); rerr != nil {
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
s.Reset()
}
}()

log.Debugf("received dial-request from: %s, addr: %s", s.Conn().RemotePeer(), s.Conn().RemoteMultiaddr())
evt := as.serveDialRequest(s)
log.Debugf("completed dial-request from %s, response status: %s, dial status: %s, err: %s",
s.Conn().RemotePeer(), evt.ResponseStatus, evt.DialStatus, evt.Error)
Expand Down
Loading