1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Merge pull request #3640 from IvanSavenko/fix_thread_race

Fix thread race
This commit is contained in:
Ivan Savenko 2024-02-25 21:03:41 +02:00 committed by GitHub
commit cda6899ee2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 3 deletions

View File

@ -88,7 +88,6 @@ void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
// create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer
std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())};
boost::mutex::scoped_lock lock(writeMutex);
boost::asio::write(*socket, boost::asio::buffer(messageSize), ec );
boost::asio::write(*socket, boost::asio::buffer(message), ec );

View File

@ -19,7 +19,6 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input
std::shared_ptr<NetworkSocket> socket;
boost::mutex writeMutex;
NetworkBuffer readBuffer;
INetworkConnectionListener & listener;

View File

@ -70,6 +70,8 @@ CConnection::~CConnection() = default;
void CConnection::sendPack(const CPack * pack)
{
boost::mutex::scoped_lock lock(writeMutex);
auto connectionPtr = networkConnection.lock();
if (!connectionPtr)
@ -92,7 +94,13 @@ CPack * CConnection::retrievePack(const std::vector<std::byte> & data)
*deserializer & result;
logNetwork->trace("Received CPack of type %s", (result ? typeid(*result).name() : "nullptr"));
if (result == nullptr)
throw std::runtime_error("Failed to retrieve pack!");
if (packReader->position != data.size())
throw std::runtime_error("Failed to retrieve pack! Not all data has been read!");
logNetwork->trace("Received CPack of type %s", typeid(*result).name());
return result;
}

View File

@ -32,6 +32,8 @@ class DLL_LINKAGE CConnection : boost::noncopyable
std::unique_ptr<BinaryDeserializer> deserializer;
std::unique_ptr<BinarySerializer> serializer;
boost::mutex writeMutex;
void disableStackSendingByID();
void enableStackSendingByID();
void disableSmartPointerSerialization();