Recheck for duplicate connection after handshaking to cover race condition with simultaneous bi-directional connections

This commit is contained in:
catbref 2020-05-25 07:24:19 +01:00
parent 0c32afa07f
commit 3afbd7aa51

View File

@ -780,6 +780,15 @@ public class Network {
private void onHandshakeCompleted(Peer peer) {
LOGGER.debug(String.format("Handshake completed with peer %s", peer));
// Are we already connected to this peer?
Peer existingPeer = getHandshakedPeerWithPublicKey(peer.getPeersPublicKey());
// NOTE: actual object reference compare, not Peer.equals()
if (existingPeer != peer) {
LOGGER.info(() -> String.format("We already have a connection with peer %s - discarding", peer));
peer.disconnect("existing connection");
return;
}
// Make a note that we've successfully completed handshake (and when)
peer.getPeerData().setLastConnected(NTP.getTime());