always set the peer if the peer scope is null
Removes the very ugly contract of "if you know the peer you have set it" and makes the
code robust against upstream bugz.
@@ -157,9 +157,9 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
return nil, fmt.Errorf("gater rejected connection with peer %s and addr %s with direction %d",
sconn.RemotePeer().Pretty(), maconn.RemoteMultiaddr(), dir)
}
- // Only call SetPeer if we didn't know the peer ID in advance.
- // Otherwise, the caller will already have called it before calling Upgrade.
- if p == "" {
+ // Only call SetPeer if it hasn't already been set -- this can happen when we don't know
+ // the peer in advance and in some bug scenarios.
+ if connScope.PeerScope() == nil {
if err := connScope.SetPeer(sconn.RemotePeer()); err != nil {
log.Debugw("resource manager blocked connection for peer", "peer", sconn.RemotePeer(), "addr", conn.RemoteAddr(), "error", err)
if err := maconn.Close(); err != nil {
