Quellcode durchsuchen

Fix possible thread race on sending packet from two threads

Ivan Savenko vor 1 Jahr
Ursprung
Commit
6901945b6e

+ 0 - 1
lib/network/NetworkConnection.cpp

@@ -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 );
 

+ 0 - 1
lib/network/NetworkConnection.h

@@ -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;

+ 2 - 0
lib/serializer/Connection.cpp

@@ -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)

+ 2 - 0
lib/serializer/Connection.h

@@ -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();