Skip to content

Commit 3ff6ff5

Browse files
committed
Merge bitcoin#11744: net: Add missing locks in net.{cpp,h}
bfb0c0a Add Clang thread safety analysis annotations (practicalswift) 63f21d2 net: Add missing locks in net.{cpp,h} (practicalswift) Pull request description: Add missing locks in `net.{cpp,h}`: * writing variable `nTotalBytesRecv` requires holding mutex `cs_totalBytesRecv` exclusively * writing variables `nTotalBytesSent`, `nMaxOutboundTotalBytesSentInCycle` and `nMaxOutboundCycleStartTime` require holding mutex `cs_totalBytesSent` exclusively * writing variables `nMaxOutboundTimeframe` and `nMaxOutboundLimit` require holding mutex `cs_totalBytesSent` exclusively * writing variable `vAddedNodes` requires holding mutex `cs_vAddedNodes` exclusively Tree-SHA512: 54a5b4bc6dc6f404dacf403af2ddd7b2214cc0a17d1d32a282def1c6b536105dada56bfabbc8606f56755f2d24874abba09913b51c8d13b0f2b000149551f0b0
2 parents 16fff80 + bfb0c0a commit 3ff6ff5

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

Diff for: src/net.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -2269,10 +2269,16 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
22692269
{
22702270
Init(connOptions);
22712271

2272-
nTotalBytesRecv = 0;
2273-
nTotalBytesSent = 0;
2274-
nMaxOutboundTotalBytesSentInCycle = 0;
2275-
nMaxOutboundCycleStartTime = 0;
2272+
{
2273+
LOCK(cs_totalBytesRecv);
2274+
nTotalBytesRecv = 0;
2275+
}
2276+
{
2277+
LOCK(cs_totalBytesSent);
2278+
nTotalBytesSent = 0;
2279+
nMaxOutboundTotalBytesSentInCycle = 0;
2280+
nMaxOutboundCycleStartTime = 0;
2281+
}
22762282

22772283
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
22782284
if (clientInterface) {

Diff for: src/net.h

+16-10
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ class CConnman
158158
m_msgproc = connOptions.m_msgproc;
159159
nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
160160
nReceiveFloodSize = connOptions.nReceiveFloodSize;
161-
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
162-
nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
161+
{
162+
LOCK(cs_totalBytesSent);
163+
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
164+
nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
165+
}
163166
vWhitelistedRange = connOptions.vWhitelistedRange;
164-
vAddedNodes = connOptions.m_added_nodes;
167+
{
168+
LOCK(cs_vAddedNodes);
169+
vAddedNodes = connOptions.m_added_nodes;
170+
}
165171
}
166172

167173
CConnman(uint64_t seed0, uint64_t seed1);
@@ -364,14 +370,14 @@ class CConnman
364370
// Network usage totals
365371
CCriticalSection cs_totalBytesRecv;
366372
CCriticalSection cs_totalBytesSent;
367-
uint64_t nTotalBytesRecv;
368-
uint64_t nTotalBytesSent;
373+
uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv);
374+
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent);
369375

370376
// outbound limit & stats
371-
uint64_t nMaxOutboundTotalBytesSentInCycle;
372-
uint64_t nMaxOutboundCycleStartTime;
373-
uint64_t nMaxOutboundLimit;
374-
uint64_t nMaxOutboundTimeframe;
377+
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent);
378+
uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent);
379+
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
380+
uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent);
375381

376382
// Whitelisted ranges. Any node connecting from these is automatically
377383
// whitelisted (as well as those connecting to whitelisted binds).
@@ -389,7 +395,7 @@ class CConnman
389395
CAddrMan addrman;
390396
std::deque<std::string> vOneShots;
391397
CCriticalSection cs_vOneShots;
392-
std::vector<std::string> vAddedNodes;
398+
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
393399
CCriticalSection cs_vAddedNodes;
394400
std::vector<CNode*> vNodes;
395401
std::list<CNode*> vNodesDisconnected;

0 commit comments

Comments
 (0)