Skip to content

Commit faae6ca

Browse files
author
MarcoFalke
committed
net: RecordBytesSent under cs_vSend lock
The CNode member nSendBytes is incremented under the node's lock cs_vSend. However, RecordBytesSent is not. An RPC thread that acquires the cs_vSend lock puts the message handler thread on hold. While the thread is on hold, getnettotals returns "stale" values or values that don't add up. This can be fixed by making cs_vSend a "write lock" for the total bytes sent in connman. After this commit, both calls to RecordBytesSent are done under the LOCK(pnode->cs_vSend);
1 parent 8da1e43 commit faae6ca

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/net.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,7 @@ void CConnman::SocketHandler()
14241424
//
14251425
// Send
14261426
//
1427-
if (sendSet)
1428-
{
1427+
if (sendSet) {
14291428
LOCK(pnode->cs_vSend);
14301429
size_t nBytes = SocketSendData(pnode);
14311430
if (nBytes) {
@@ -2744,7 +2743,6 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
27442743
pnode->m_serializer->prepareForTransport(msg, serializedHeader);
27452744
size_t nTotalSize = nMessageSize + serializedHeader.size();
27462745

2747-
size_t nBytesSent = 0;
27482746
{
27492747
LOCK(pnode->cs_vSend);
27502748
bool optimisticSend(pnode->vSendMsg.empty());
@@ -2760,11 +2758,11 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
27602758
pnode->vSendMsg.push_back(std::move(msg.data));
27612759

27622760
// If write queue empty, attempt "optimistic write"
2763-
if (optimisticSend == true)
2764-
nBytesSent = SocketSendData(pnode);
2761+
if (optimisticSend == true) {
2762+
const size_t bytes_sent{SocketSendData(pnode)};
2763+
if (bytes_sent) RecordBytesSent(bytes_sent);
2764+
}
27652765
}
2766-
if (nBytesSent)
2767-
RecordBytesSent(nBytesSent);
27682766
}
27692767

27702768
bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)

0 commit comments

Comments
 (0)