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

Go func recover #1798

Merged
merged 3 commits into from
Jun 3, 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
7 changes: 6 additions & 1 deletion p2p/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ func (p *P2PNode) Subscribe(location common.Location, datatype interface{}) erro
return err
}

go p.peerManager.Provide(p.ctx, location, datatype)
go func() {
err := p.peerManager.Provide(p.ctx, location, datatype)
if err != nil {
log.Global.Errorf("error providing topic %s in %s: %s", reflect.TypeOf(datatype), location.Name(), err.Error())
}
}()
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions p2p/node/peerManager/peerManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ func NewManager(ctx context.Context, low int, high int, datastore datastore.Data
logger := log.NewLogger("peers.log", viper.GetString(utils.PeersLogLevelFlag.Name))

go func() {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
q := query.Query{}
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
Expand Down
46 changes: 29 additions & 17 deletions p2p/node/pubsubManager/gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,36 @@ func (g *PubsubManager) Subscribe(location common.Location, datatype interface{}
// close the msgChan if we exit this function
defer close(msgChan)
full := 0
// Start worker goroutines
for i := 0; i < numWorkers; i++ {
go func(location common.Location) {
for msg := range msgChan { // This should exit when msgChan is closed
var data interface{}
// unmarshal the received data depending on the topic's type
err = pb.UnmarshalAndConvert(msg.Data, location, &data, datatype)
if err != nil {
log.Global.Errorf("error unmarshalling data: %s", err)
continue
}

// handle the received data
if g.onReceived != nil {
g.onReceived(msg.ReceivedFrom, *msg.Topic, data, location)
}
// maintain a number of worker threads to handle messages
var msgWorker func(location common.Location)
msgWorker = func(location common.Location) {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
"location": location.Name(),
}).Errorf("Go-Quai Panicked")
}
go msgWorker(location) // If this worker exits, start a new one
}()
for msg := range msgChan { // This should exit when msgChan is closed
var data interface{}
// unmarshal the received data depending on the topic's type
err = pb.UnmarshalAndConvert(msg.Data, location, &data, datatype)
if err != nil {
log.Global.Errorf("error unmarshalling data: %s", err)
continue
}

// handle the received data
if g.onReceived != nil {
g.onReceived(msg.ReceivedFrom, *msg.Topic, data, location)
}
}(location)
}
}
for i := 0; i < numWorkers; i++ {
go msgWorker(location)
}
log.Global.WithField("topic", topic.String()).Debugf("Subscribed to topic")
for {
Expand Down
Loading