diff --git a/src/net.cpp b/src/net.cpp index b48efb533d390..ed0154e8c356e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1399,11 +1399,16 @@ void CConnman::CalculateNumConnectionsChangedStats() mapSentBytesMsgStats[NET_MESSAGE_COMMAND_OTHER] = 0; auto vNodesCopy = CopyNodeVector(CConnman::FullyConnectedOnly); for (auto pnode : vNodesCopy) { - LOCK(pnode->cs_vRecv); - for (const mapMsgCmdSize::value_type &i : pnode->mapRecvBytesPerMsgCmd) - mapRecvBytesMsgStats[i.first] += i.second; - for (const mapMsgCmdSize::value_type &i : pnode->mapSendBytesPerMsgCmd) - mapSentBytesMsgStats[i.first] += i.second; + { + LOCK(pnode->cs_vRecv); + for (const mapMsgCmdSize::value_type &i : pnode->mapRecvBytesPerMsgCmd) + mapRecvBytesMsgStats[i.first] += i.second; + } + { + LOCK(pnode->cs_vSend); + for (const mapMsgCmdSize::value_type &i : pnode->mapSendBytesPerMsgCmd) + mapSentBytesMsgStats[i.first] += i.second; + } if(pnode->fClient) spvNodes++; else @@ -1877,11 +1882,9 @@ void CConnman::SocketHandler() break; } - LOCK(pnode->cs_vSend); - size_t nBytes = SocketSendData(pnode); - if (nBytes) { - RecordBytesSent(nBytes); - } + // Send data + size_t bytes_sent = WITH_LOCK(pnode->cs_vSend, return SocketSendData(pnode)); + if (bytes_sent) RecordBytesSent(bytes_sent); } ReleaseNodeVector(vErrorNodes); diff --git a/src/net.h b/src/net.h index 65058c0b65b2d..b7be35ae20d55 100644 --- a/src/net.h +++ b/src/net.h @@ -1026,8 +1026,10 @@ class CNode NetPermissionFlags m_permissionFlags{ PF_NONE }; std::atomic nServices{NODE_NONE}; SOCKET hSocket GUARDED_BY(cs_hSocket); - size_t nSendSize{0}; // total size of all vSendMsg entries - size_t nSendOffset{0}; // offset inside the first vSendMsg already sent + /** Total size of all vSendMsg entries */ + size_t nSendSize GUARDED_BY(cs_vSend){0}; + /** Offset inside the first vSendMsg already sent */ + size_t nSendOffset GUARDED_BY(cs_vSend){0}; uint64_t nSendBytes GUARDED_BY(cs_vSend){0}; std::list> vSendMsg GUARDED_BY(cs_vSend); std::atomic nSendMsgSize{0}; @@ -1119,7 +1121,7 @@ class CNode Network ConnectedThroughNetwork() const; protected: - mapMsgCmdSize mapSendBytesPerMsgCmd; + mapMsgCmdSize mapSendBytesPerMsgCmd GUARDED_BY(cs_vSend); mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv); public: