diff --git a/node/Topology.cpp b/node/Topology.cpp index 9027eff16..031c0b1b6 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -146,26 +146,30 @@ SharedPtr Topology::getPeer(const Address &zta) return SharedPtr(); } - Mutex::Lock _l(_lock); - - SharedPtr &ap = _peers[zta]; - - if (ap) { - ap->use(RR->node->now()); - return ap; + { + Mutex::Lock _l(_lock); + const SharedPtr *const ap = _peers.get(zta); + if (ap) { + (*ap)->use(RR->node->now()); + return *ap; + } } - Identity id(_getIdentity(zta)); - if (id) { - try { - ap = SharedPtr(new Peer(RR->identity,id)); - ap->use(RR->node->now()); - return ap; - } catch ( ... ) {} // invalid identity? - } + try { + Identity id(_getIdentity(zta)); + if (id) { + SharedPtr np(new Peer(RR->identity,id)); + { + Mutex::Lock _l(_lock); + SharedPtr &ap = _peers[zta]; + if (!ap) + ap.swap(np); + ap->use(RR->node->now()); + return ap; + } + } + } catch ( ... ) {} // invalid identity on disk? - // If we get here it means we read an invalid cache identity or had some other error - _peers.erase(zta); return SharedPtr(); } diff --git a/node/Topology.hpp b/node/Topology.hpp index b9f063c85..d6f453aca 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -87,7 +87,7 @@ public: inline SharedPtr getPeerNoCache(const Address &zta,const uint64_t now) { Mutex::Lock _l(_lock); - const SharedPtr *ap = _peers.get(zta); + const SharedPtr *const ap = _peers.get(zta); if (ap) { (*ap)->use(now); return *ap;