From 2a0a817006f39e134b2c063ebfd9122ecb8c6a2c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 8 Jan 2024 12:06:53 -0500 Subject: [PATCH] fix: OnNewPeer needs to use a wait group with OnNewPeerListen --- service/p2p.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/service/p2p.go b/service/p2p.go index f0577ad..203501b 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -18,6 +18,7 @@ import ( "net/url" "reflect" "sort" + "sync" "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 { - challenge := protocol.GenerateChallenge() + var wg sync.WaitGroup + challenge := protocol.GenerateChallenge() peer.SetChallenge(challenge) - p.OnNewPeerListen(peer, verifyId) + wg.Add(1) + go func() { + defer wg.Done() + p.OnNewPeerListen(peer, verifyId) + }() 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 { return err } + + wg.Wait() // Wait for OnNewPeerListen goroutine to finish return nil } func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {