From 92eb048adb2f9a4e587036f4ff5b1506b9a29e6f Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 14 Sep 2014 07:50:01 -0400 Subject: [PATCH] fixed race condition --- Tunnel.cpp | 6 +++++- Tunnel.h | 1 + TunnelPool.cpp | 15 +++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Tunnel.cpp b/Tunnel.cpp index 1dbcd8fc..3cbd628d 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -301,6 +301,7 @@ namespace tunnel void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) { + std::unique_lock l(m_TransitTunnelsMutex); m_TransitTunnels[tunnel->GetTunnelID ()] = tunnel; } @@ -490,7 +491,10 @@ namespace tunnel { LogPrint ("Transit tunnel ", it->second->GetTunnelID (), " expired"); auto tmp = it->second; - it = m_TransitTunnels.erase (it); + { + std::unique_lock l(m_TransitTunnelsMutex); + it = m_TransitTunnels.erase (it); + } delete tmp; } else diff --git a/Tunnel.h b/Tunnel.h index e1c0c253..a848e6b7 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -152,6 +152,7 @@ namespace tunnel std::map m_InboundTunnels; std::mutex m_OutboundTunnelsMutex; std::list m_OutboundTunnels; + std::mutex m_TransitTunnelsMutex; std::map m_TransitTunnels; std::map m_Pools; TunnelPool * m_ExploratoryPool; diff --git a/TunnelPool.cpp b/TunnelPool.cpp index e28a9b07..fc725238 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -260,6 +260,8 @@ namespace tunnel new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ())); tunnel->SetTunnelPool (this); } + else + LogPrint ("Can't create outbound tunnel. No inbound tunnels found"); } void TunnelPool::RecreateOutboundTunnel (OutboundTunnel * tunnel) @@ -267,10 +269,15 @@ namespace tunnel InboundTunnel * inboundTunnel = GetNextInboundTunnel (); if (!inboundTunnel) inboundTunnel = tunnels.GetNextInboundTunnel (); - LogPrint ("Re-creating destination outbound tunnel..."); - auto * newTunnel = tunnels.CreateTunnel ( - tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ())); - newTunnel->SetTunnelPool (this); + if (inboundTunnel) + { + LogPrint ("Re-creating destination outbound tunnel..."); + auto * newTunnel = tunnels.CreateTunnel ( + tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ())); + newTunnel->SetTunnelPool (this); + } + else + LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found"); } } }