Skip to content
Snippets Groups Projects
Unverified Commit da5e87ba authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Merge pull request #179 from iotaledger/fix/autopeering-race

Fix: Race condition in autopeering
parents 83fc6010 80a2612c
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"github.com/iotaledger/goshimmer/packages/autopeering/server" "github.com/iotaledger/goshimmer/packages/autopeering/server"
"github.com/iotaledger/hive.go/backoff" "github.com/iotaledger/hive.go/backoff"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/typeutils"
) )
const ( const (
...@@ -35,6 +36,7 @@ type Protocol struct { ...@@ -35,6 +36,7 @@ type Protocol struct {
log *logger.Logger // logging log *logger.Logger // logging
mgr *manager // the manager handles the actual peer discovery and re-verification mgr *manager // the manager handles the actual peer discovery and re-verification
running *typeutils.AtomicBool
closeOnce sync.Once closeOnce sync.Once
} }
...@@ -44,6 +46,7 @@ func New(local *peer.Local, cfg Config) *Protocol { ...@@ -44,6 +46,7 @@ func New(local *peer.Local, cfg Config) *Protocol {
Protocol: server.Protocol{}, Protocol: server.Protocol{},
loc: local, loc: local,
log: cfg.Log, log: cfg.Log,
running: typeutils.NewAtomicBool(),
} }
p.mgr = newManager(p, cfg.MasterPeers, cfg.Log.Named("mgr")) p.mgr = newManager(p, cfg.MasterPeers, cfg.Log.Named("mgr"))
...@@ -54,13 +57,14 @@ func New(local *peer.Local, cfg Config) *Protocol { ...@@ -54,13 +57,14 @@ func New(local *peer.Local, cfg Config) *Protocol {
func (p *Protocol) Start(s server.Sender) { func (p *Protocol) Start(s server.Sender) {
p.Protocol.Sender = s p.Protocol.Sender = s
p.mgr.start() p.mgr.start()
p.log.Debug("discover started") p.log.Debug("discover started")
p.running.Set()
} }
// Close finalizes the protocol. // Close finalizes the protocol.
func (p *Protocol) Close() { func (p *Protocol) Close() {
p.closeOnce.Do(func() { p.closeOnce.Do(func() {
p.running.UnSet()
p.mgr.close() p.mgr.close()
}) })
} }
...@@ -105,8 +109,11 @@ func (p *Protocol) GetVerifiedPeers() []*peer.Peer { ...@@ -105,8 +109,11 @@ func (p *Protocol) GetVerifiedPeers() []*peer.Peer {
// HandleMessage responds to incoming peer discovery messages. // HandleMessage responds to incoming peer discovery messages.
func (p *Protocol) HandleMessage(s *server.Server, fromAddr string, fromID peer.ID, fromKey peer.PublicKey, data []byte) (bool, error) { func (p *Protocol) HandleMessage(s *server.Server, fromAddr string, fromID peer.ID, fromKey peer.PublicKey, data []byte) (bool, error) {
switch pb.MType(data[0]) { if !p.running.IsSet() {
return false, nil
}
switch pb.MType(data[0]) {
// Ping // Ping
case pb.MPing: case pb.MPing:
m := new(pb.Ping) m := new(pb.Ping)
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"github.com/iotaledger/goshimmer/packages/autopeering/server" "github.com/iotaledger/goshimmer/packages/autopeering/server"
"github.com/iotaledger/hive.go/backoff" "github.com/iotaledger/hive.go/backoff"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/typeutils"
) )
const ( const (
...@@ -45,6 +46,7 @@ type Protocol struct { ...@@ -45,6 +46,7 @@ type Protocol struct {
log *logger.Logger // logging log *logger.Logger // logging
mgr *manager // the manager handles the actual neighbor selection mgr *manager // the manager handles the actual neighbor selection
running *typeutils.AtomicBool
closeOnce sync.Once closeOnce sync.Once
} }
...@@ -55,6 +57,7 @@ func New(local *peer.Local, disc DiscoverProtocol, cfg Config) *Protocol { ...@@ -55,6 +57,7 @@ func New(local *peer.Local, disc DiscoverProtocol, cfg Config) *Protocol {
loc: local, loc: local,
disc: disc, disc: disc,
log: cfg.Log, log: cfg.Log,
running: typeutils.NewAtomicBool(),
} }
p.mgr = newManager(p, disc.GetVerifiedPeers, cfg.Log.Named("mgr"), cfg) p.mgr = newManager(p, disc.GetVerifiedPeers, cfg.Log.Named("mgr"), cfg)
...@@ -65,13 +68,14 @@ func New(local *peer.Local, disc DiscoverProtocol, cfg Config) *Protocol { ...@@ -65,13 +68,14 @@ func New(local *peer.Local, disc DiscoverProtocol, cfg Config) *Protocol {
func (p *Protocol) Start(s server.Sender) { func (p *Protocol) Start(s server.Sender) {
p.Protocol.Sender = s p.Protocol.Sender = s
p.mgr.start() p.mgr.start()
p.log.Debug("neighborhood started") p.log.Debug("neighborhood started")
p.running.Set()
} }
// Close finalizes the protocol. // Close finalizes the protocol.
func (p *Protocol) Close() { func (p *Protocol) Close() {
p.closeOnce.Do(func() { p.closeOnce.Do(func() {
p.running.UnSet()
p.mgr.close() p.mgr.close()
}) })
} }
...@@ -100,8 +104,11 @@ func (p *Protocol) RemoveNeighbor(id peer.ID) { ...@@ -100,8 +104,11 @@ func (p *Protocol) RemoveNeighbor(id peer.ID) {
// HandleMessage responds to incoming neighbor selection messages. // HandleMessage responds to incoming neighbor selection messages.
func (p *Protocol) HandleMessage(s *server.Server, fromAddr string, fromID peer.ID, _ peer.PublicKey, data []byte) (bool, error) { func (p *Protocol) HandleMessage(s *server.Server, fromAddr string, fromID peer.ID, _ peer.PublicKey, data []byte) (bool, error) {
switch pb.MType(data[0]) { if !p.running.IsSet() {
return false, nil
}
switch pb.MType(data[0]) {
// PeeringRequest // PeeringRequest
case pb.MPeeringRequest: case pb.MPeeringRequest:
m := new(pb.PeeringRequest) m := new(pb.PeeringRequest)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment