Skip to content

Commit

Permalink
fixed race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 22, 2025
1 parent 2857a16 commit b3d0951
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 11 additions & 3 deletions libi2pd/TransitTunnel.cpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -101,13 +101,13 @@ namespace tunnel
TunnelMessageBlock block;
block.deliveryType = eDeliveryTypeLocal;
block.data = msg;
std::unique_lock<std::mutex> l(m_SendMutex);
std::lock_guard<std::mutex> l(m_SendMutex);
m_Gateway.PutTunnelDataMsg (block);
}

void TransitTunnelGateway::FlushTunnelDataMsgs ()
{
std::unique_lock<std::mutex> l(m_SendMutex);
std::lock_guard<std::mutex> l(m_SendMutex);
m_Gateway.SendBuffer ();
}

Expand All @@ -130,14 +130,22 @@ namespace tunnel
EncryptTunnelMsg (tunnelMsg, newMsg);

LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());
std::lock_guard<std::mutex> l(m_HandleMutex);
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
}

void TransitTunnelEndpoint::FlushTunnelDataMsgs ()
{
std::lock_guard<std::mutex> l(m_HandleMutex);
m_Endpoint.FlushI2NPMsgs ();
}

void TransitTunnelEndpoint::Cleanup ()
{
std::lock_guard<std::mutex> l(m_HandleMutex);
m_Endpoint.Cleanup ();
}

std::string TransitTunnelEndpoint::GetNextPeerName () const
{
auto hash = m_Endpoint.GetCurrentHash ();
Expand Down
5 changes: 3 additions & 2 deletions libi2pd/TransitTunnel.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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<i2p::I2NPMessage>&& tunnelMsg) override;
void FlushTunnelDataMsgs () override;
Expand All @@ -109,6 +109,7 @@ namespace tunnel

private:

std::mutex m_HandleMutex;
TunnelEndpoint m_Endpoint;
};

Expand Down

0 comments on commit b3d0951

Please sign in to comment.