Skip to content

Commit 190cbfa

Browse files
authored
polygon/sync: message listener to preserve peer events ordering (#10032)
Observed the following issue in a long running Astrid sync on bor-mainnet: ``` [DBUG] [04-17|14:25:43.504] [p2p.peerEventObserver] received new peer event id=Disconnect peerId=51935aa1eeabdb73b70d36c7d5953a3bfdf5c84e88241c44a7d16d508b281d397bdd8504c934bfb45af146b86eb5899ccea85e590774f9823d056a424080b763 [DBUG] [04-17|14:25:43.504] [p2p.peerEventObserver] received new peer event id=Connect peerId=51935aa1eeabdb73b70d36c7d5953a3bfdf5c84e88241c44a7d16d508b281d397bdd8504c934bfb45af146b86eb5899ccea85e590774f9823d056a424080b763 ``` Note the timestamps are the same on the millisecond level, however the disconnect was processed before the connect which is wrong (connect should always be first). This then got the `PeerTracker` in a bad state - it kept on returning peer `51935aa1eeabdb73b70d36c7d5953a3bfdf5c84e88241c44a7d16d508b281d397bdd8504c934bfb45af146b86eb5899ccea85e590774f9823d056a424080b763` as a valid peer to download from, which caused repeated `peer not found` errors when sending messages to it. Fix is to have the message listener wait for all observers to finish processing peer event 1 before proceeding to notifying them about peer event 2.
1 parent 6d9a5fd commit 190cbfa

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

polygon/p2p/message_listener.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,18 @@ func (ml *messageListener) notifyPeerEventObservers(peerEvent *sentry.PeerEvent)
170170
ml.observersMu.Lock()
171171
defer ml.observersMu.Unlock()
172172

173-
notifyObservers(ml.peerEventObservers, peerEvent)
173+
// wait on all observers to finish processing the peer event before notifying them
174+
// with subsequent events in order to preserve the ordering of the sentry messages
175+
var wg sync.WaitGroup
176+
for _, observer := range ml.peerEventObservers {
177+
wg.Add(1)
178+
go func(observer MessageObserver[*sentry.PeerEvent]) {
179+
defer wg.Done()
180+
observer(peerEvent)
181+
}(observer)
182+
}
183+
184+
wg.Wait()
174185
return nil
175186
}
176187

0 commit comments

Comments
 (0)