From b3d09513b85a36cd2b170f22ab4fb1f27cfa9c9d Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jan 2025 19:38:07 -0500 Subject: [PATCH] fixed race condition --- libi2pd/TransitTunnel.cpp | 14 +++++++++++--- libi2pd/TransitTunnel.h | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libi2pd/TransitTunnel.cpp b/libi2pd/TransitTunnel.cpp index ba505ae3..2b9b2a85 100644 --- a/libi2pd/TransitTunnel.cpp +++ b/libi2pd/TransitTunnel.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2024, The PurpleI2P Project +* Copyright (c) 2013-2025, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -101,13 +101,13 @@ namespace tunnel TunnelMessageBlock block; block.deliveryType = eDeliveryTypeLocal; block.data = msg; - std::unique_lock l(m_SendMutex); + std::lock_guard l(m_SendMutex); m_Gateway.PutTunnelDataMsg (block); } void TransitTunnelGateway::FlushTunnelDataMsgs () { - std::unique_lock l(m_SendMutex); + std::lock_guard l(m_SendMutex); m_Gateway.SendBuffer (); } @@ -130,14 +130,22 @@ namespace tunnel EncryptTunnelMsg (tunnelMsg, newMsg); LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ()); + std::lock_guard l(m_HandleMutex); m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg); } void TransitTunnelEndpoint::FlushTunnelDataMsgs () { + std::lock_guard l(m_HandleMutex); m_Endpoint.FlushI2NPMsgs (); } + void TransitTunnelEndpoint::Cleanup () + { + std::lock_guard l(m_HandleMutex); + m_Endpoint.Cleanup (); + } + std::string TransitTunnelEndpoint::GetNextPeerName () const { auto hash = m_Endpoint.GetCurrentHash (); diff --git a/libi2pd/TransitTunnel.h b/libi2pd/TransitTunnel.h index c0f9c276..efdd2bc6 100644 --- a/libi2pd/TransitTunnel.h +++ b/libi2pd/TransitTunnel.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2024, The PurpleI2P Project +* Copyright (c) 2013-2025, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -100,7 +100,7 @@ namespace tunnel TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey), m_Endpoint (false) {}; // transit endpoint is always outbound - void Cleanup () override { m_Endpoint.Cleanup (); } + void Cleanup () override; void HandleTunnelDataMsg (std::shared_ptr&& tunnelMsg) override; void FlushTunnelDataMsgs () override; @@ -109,6 +109,7 @@ namespace tunnel private: + std::mutex m_HandleMutex; TunnelEndpoint m_Endpoint; };