diff --git a/peer.go b/peer.go index 92d4973d8..1be50aff1 100644 --- a/peer.go +++ b/peer.go @@ -41,7 +41,12 @@ func newPeer(addr *NetAddress, reactorsByCh map[byte]Reactor, chDescs []*Channel } // outbound = true - return newPeerFromExistingConn(conn, true, reactorsByCh, chDescs, onPeerError, config, privKey) + peer, err := newPeerFromExistingConn(conn, true, reactorsByCh, chDescs, onPeerError, config, privKey) + if err != nil { + conn.Close() + return nil, err + } + return peer, nil } func newPeerFromExistingConn(conn net.Conn, outbound bool, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor, onPeerError func(*Peer, interface{}), config cfg.Config, privKey crypto.PrivKeyEd25519) (*Peer, error) { diff --git a/switch.go b/switch.go index fcde27449..f38d4feb1 100644 --- a/switch.go +++ b/switch.go @@ -318,14 +318,13 @@ func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) (*Peer, defer sw.dialing.Delete(addr.IP.String()) peer, err := newPeer(addr, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.config, sw.nodePrivKey) - if persistent { - peer.makePersistent() - } if err != nil { log.Info("Failed dialing peer", "address", addr, "error", err) - peer.CloseConn() return nil, err } + if persistent { + peer.makePersistent() + } err = sw.AddPeer(peer) if err != nil { log.Info("Failed adding peer", "address", addr, "error", err) @@ -549,7 +548,7 @@ func makeSwitch(i int, network, version string, initSwitch func(int, *Switch) *S func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) error { peer, err := newPeerFromExistingConn(conn, outbound, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.config, sw.nodePrivKey) if err != nil { - peer.CloseConn() + conn.Close() return err }