Browse Source

avoid potential deadlock

pull/669/head
orignal 8 years ago
parent
commit
93ed032015
  1. 20
      Destination.cpp

20
Destination.cpp

@ -170,29 +170,36 @@ namespace client
} }
std::shared_ptr<const i2p::data::LeaseSet> LeaseSetDestination::FindLeaseSet (const i2p::data::IdentHash& ident) std::shared_ptr<const i2p::data::LeaseSet> LeaseSetDestination::FindLeaseSet (const i2p::data::IdentHash& ident)
{
std::shared_ptr<i2p::data::LeaseSet> remoteLS;
{ {
std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex); std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex);
auto it = m_RemoteLeaseSets.find (ident); auto it = m_RemoteLeaseSets.find (ident);
if (it != m_RemoteLeaseSets.end ()) if (it != m_RemoteLeaseSets.end ())
remoteLS = it->second;
}
if (remoteLS)
{ {
if (!it->second->IsExpired ()) if (!remoteLS->IsExpired ())
{ {
if (it->second->ExpiresSoon()) if (remoteLS->ExpiresSoon())
{ {
LogPrint(eLogDebug, "Destination: Lease Set expires soon, updating before expire"); LogPrint(eLogDebug, "Destination: Lease Set expires soon, updating before expire");
// update now before expiration for smooth handover // update now before expiration for smooth handover
RequestDestination(ident, [this, ident] (std::shared_ptr<i2p::data::LeaseSet> ls) { auto s = shared_from_this ();
RequestDestination(ident, [s, ident] (std::shared_ptr<i2p::data::LeaseSet> ls) {
if(ls && !ls->IsExpired()) if(ls && !ls->IsExpired())
{ {
ls->PopulateLeases(); ls->PopulateLeases();
{ {
std::lock_guard<std::mutex> _lock(m_RemoteLeaseSetsMutex); std::lock_guard<std::mutex> _lock(s->m_RemoteLeaseSetsMutex);
m_RemoteLeaseSets[ident] = ls; s->m_RemoteLeaseSets[ident] = ls;
} }
} }
}); });
} }
return it->second; return remoteLS;
} }
else else
LogPrint (eLogWarning, "Destination: remote LeaseSet expired"); LogPrint (eLogWarning, "Destination: remote LeaseSet expired");
@ -203,6 +210,7 @@ namespace client
if (ls && !ls->IsExpired ()) if (ls && !ls->IsExpired ())
{ {
ls->PopulateLeases (); // since we don't store them in netdb ls->PopulateLeases (); // since we don't store them in netdb
std::lock_guard<std::mutex> _lock(m_RemoteLeaseSetsMutex);
m_RemoteLeaseSets[ident] = ls; m_RemoteLeaseSets[ident] = ls;
return ls; return ls;
} }

Loading…
Cancel
Save