Skip to content

Commit d7c58ad

Browse files
committed
Split CNode::cs_vSend: message processing and message sending
cs_vSend is used for two purposes - to lock the datastructures used to queue messages to place on the wire and to only call SendMessages once at a time per-node. I believe SendMessages used to access some of the vSendMsg stuff, but it doesn't anymore, so these locks do not need to be on the same mutex, and also make deadlocking much more likely.
1 parent 8b66bf7 commit d7c58ad

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/net.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,12 +1147,10 @@ void CConnman::ThreadSocketHandler()
11471147
// * Hand off all complete messages to the processor, to be handled without
11481148
// blocking here.
11491149
{
1150-
TRY_LOCK(pnode->cs_vSend, lockSend);
1151-
if (lockSend) {
1152-
if (!pnode->vSendMsg.empty()) {
1153-
FD_SET(pnode->hSocket, &fdsetSend);
1154-
continue;
1155-
}
1150+
LOCK(pnode->cs_vSend);
1151+
if (!pnode->vSendMsg.empty()) {
1152+
FD_SET(pnode->hSocket, &fdsetSend);
1153+
continue;
11561154
}
11571155
}
11581156
{
@@ -1272,12 +1270,10 @@ void CConnman::ThreadSocketHandler()
12721270
continue;
12731271
if (FD_ISSET(pnode->hSocket, &fdsetSend))
12741272
{
1275-
TRY_LOCK(pnode->cs_vSend, lockSend);
1276-
if (lockSend) {
1277-
size_t nBytes = SocketSendData(pnode);
1278-
if (nBytes) {
1279-
RecordBytesSent(nBytes);
1280-
}
1273+
LOCK(pnode->cs_vSend);
1274+
size_t nBytes = SocketSendData(pnode);
1275+
if (nBytes) {
1276+
RecordBytesSent(nBytes);
12811277
}
12821278
}
12831279

@@ -1875,7 +1871,7 @@ void CConnman::ThreadMessageHandler()
18751871

18761872
// Send messages
18771873
{
1878-
TRY_LOCK(pnode->cs_vSend, lockSend);
1874+
TRY_LOCK(pnode->cs_sendProcessing, lockSend);
18791875
if (lockSend)
18801876
GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
18811877
}

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ class CNode
618618
std::list<CNetMessage> vProcessMsg;
619619
size_t nProcessQueueSize;
620620

621+
CCriticalSection cs_sendProcessing;
622+
621623
std::deque<CInv> vRecvGetData;
622624
uint64_t nRecvBytes;
623625
std::atomic<int> nRecvVersion;

0 commit comments

Comments
 (0)