fix: OnNewPeer needs to use a wait group with OnNewPeerListen

This commit is contained in:
Derrick Hammer 2024-01-08 12:06:53 -05:00
parent a7f7963f1c
commit 2a0a817006
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 10 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import (
"net/url" "net/url"
"reflect" "reflect"
"sort" "sort"
"sync"
"time" "time"
) )
@ -207,11 +208,16 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool) error {
} }
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error { func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
challenge := protocol.GenerateChallenge() var wg sync.WaitGroup
challenge := protocol.GenerateChallenge()
peer.SetChallenge(challenge) peer.SetChallenge(challenge)
wg.Add(1)
go func() {
defer wg.Done()
p.OnNewPeerListen(peer, verifyId) p.OnNewPeerListen(peer, verifyId)
}()
handshakeOpenMsg, err := protocol.NewHandshakeOpen(challenge, p.networkID).ToMessage() handshakeOpenMsg, err := protocol.NewHandshakeOpen(challenge, p.networkID).ToMessage()
@ -223,6 +229,8 @@ func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
if err != nil { if err != nil {
return err return err
} }
wg.Wait() // Wait for OnNewPeerListen goroutine to finish
return nil return nil
} }
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) { func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {