From 629c7185277a9c9082e8ec224030a00fa3c00130 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 3 Dec 2022 14:18:40 -0500 Subject: [PATCH] fixed races condition with pending outgoing sessions --- libi2pd/SSU2.cpp | 7 +++++++ libi2pd/SSU2.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index 4636762d..e1f75746 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -368,6 +368,7 @@ namespace transport bool SSU2Server::AddPendingOutgoingSession (std::shared_ptr session) { if (!session) return false; + std::unique_lock l(m_PendingOutgoingSessionsMutex); return m_PendingOutgoingSessions.emplace (session->GetRemoteEndpoint (), session).second; } @@ -381,6 +382,7 @@ namespace transport std::shared_ptr SSU2Server::FindPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep) const { + std::unique_lock l(m_PendingOutgoingSessionsMutex); auto it = m_PendingOutgoingSessions.find (ep); if (it != m_PendingOutgoingSessions.end ()) return it->second; @@ -389,6 +391,7 @@ namespace transport void SSU2Server::RemovePendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep) { + std::unique_lock l(m_PendingOutgoingSessionsMutex); m_PendingOutgoingSessions.erase (ep); } @@ -510,7 +513,10 @@ namespace transport { if (it1->second->GetState () == eSSU2SessionStateSessionRequestSent && it1->second->ProcessSessionCreated (buf, len)) + { + std::unique_lock l(m_PendingOutgoingSessionsMutex); m_PendingOutgoingSessions.erase (it1); // we are done with that endpoint + } else it1->second->ProcessRetry (buf, len); } @@ -754,6 +760,7 @@ namespace transport if (it->second->IsTerminationTimeoutExpired (ts)) { //it->second->Terminate (); + std::unique_lock l(m_PendingOutgoingSessionsMutex); it = m_PendingOutgoingSessions.erase (it); } else diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index c5c2c140..ac4495db 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -10,6 +10,7 @@ #define SSU2_H__ #include +#include #include "util.h" #include "SSU2Session.h" @@ -140,6 +141,7 @@ namespace transport std::unordered_map > m_Sessions; std::unordered_map > m_SessionsByRouterHash; std::map > m_PendingOutgoingSessions; + mutable std::mutex m_PendingOutgoingSessionsMutex; std::map > m_IncomingTokens, m_OutgoingTokens; // remote endpoint -> (token, expires in seconds) std::map > m_Relays; // we are introducer, relay tag -> session std::list m_Introducers, m_IntroducersV6; // introducers we are connected to