Skip to content

Commit 5488514

Browse files
committed
Merge #9225: Fix some benign races
dfed983 Fix unlocked access to vNodes.size() (Matt Corallo) 3033522 Remove double brackets in addrman (Matt Corallo) dbfaade Fix AddrMan locking (Matt Corallo) 047ea10 Make fImporting an std::atomic (Matt Corallo) 42071ca Make fDisconnect an std::atomic (Matt Corallo)
2 parents 0a04413 + dfed983 commit 5488514

File tree

5 files changed

+32
-36
lines changed

5 files changed

+32
-36
lines changed

src/addrman.h

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ class CAddrMan
482482
//! Return the number of (unique) addresses in all tables.
483483
size_t size() const
484484
{
485+
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
485486
return vRandom.size();
486487
}
487488

@@ -501,13 +502,11 @@ class CAddrMan
501502
//! Add a single address.
502503
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
503504
{
505+
LOCK(cs);
504506
bool fRet = false;
505-
{
506-
LOCK(cs);
507-
Check();
508-
fRet |= Add_(addr, source, nTimePenalty);
509-
Check();
510-
}
507+
Check();
508+
fRet |= Add_(addr, source, nTimePenalty);
509+
Check();
511510
if (fRet)
512511
LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
513512
return fRet;
@@ -516,14 +515,12 @@ class CAddrMan
516515
//! Add multiple addresses.
517516
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
518517
{
518+
LOCK(cs);
519519
int nAdd = 0;
520-
{
521-
LOCK(cs);
522-
Check();
523-
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
524-
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
525-
Check();
526-
}
520+
Check();
521+
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
522+
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
523+
Check();
527524
if (nAdd)
528525
LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
529526
return nAdd > 0;
@@ -532,23 +529,19 @@ class CAddrMan
532529
//! Mark an entry as accessible.
533530
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
534531
{
535-
{
536-
LOCK(cs);
537-
Check();
538-
Good_(addr, nTime);
539-
Check();
540-
}
532+
LOCK(cs);
533+
Check();
534+
Good_(addr, nTime);
535+
Check();
541536
}
542537

543538
//! Mark an entry as connection attempted to.
544539
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
545540
{
546-
{
547-
LOCK(cs);
548-
Check();
549-
Attempt_(addr, fCountFailure, nTime);
550-
Check();
551-
}
541+
LOCK(cs);
542+
Check();
543+
Attempt_(addr, fCountFailure, nTime);
544+
Check();
552545
}
553546

554547
/**
@@ -582,12 +575,10 @@ class CAddrMan
582575
//! Mark an entry as currently-connected-to.
583576
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
584577
{
585-
{
586-
LOCK(cs);
587-
Check();
588-
Connected_(addr, nTime);
589-
Check();
590-
}
578+
LOCK(cs);
579+
Check();
580+
Connected_(addr, nTime);
581+
Check();
591582
}
592583

593584
void SetServices(const CService &addr, ServiceFlags nServices)

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int64_t nTimeBestReceived = 0; // Used only to inform the wallet of when we last
6969
CWaitableCriticalSection csBestBlock;
7070
CConditionVariable cvBlockChange;
7171
int nScriptCheckThreads = 0;
72-
bool fImporting = false;
72+
std::atomic_bool fImporting(false);
7373
bool fReindex = false;
7474
bool fTxIndex = false;
7575
bool fHavePruned = false;

src/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ extern uint64_t nLastBlockWeight;
165165
extern const std::string strMessageMagic;
166166
extern CWaitableCriticalSection csBestBlock;
167167
extern CConditionVariable cvBlockChange;
168-
extern bool fImporting;
168+
extern std::atomic_bool fImporting;
169169
extern bool fReindex;
170170
extern int nScriptCheckThreads;
171171
extern bool fTxIndex;

src/net.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,13 @@ void CConnman::ThreadSocketHandler()
11031103
}
11041104
}
11051105
}
1106-
if(vNodes.size() != nPrevNodeCount) {
1107-
nPrevNodeCount = vNodes.size();
1106+
size_t vNodesSize;
1107+
{
1108+
LOCK(cs_vNodes);
1109+
vNodesSize = vNodes.size();
1110+
}
1111+
if(vNodesSize != nPrevNodeCount) {
1112+
nPrevNodeCount = vNodesSize;
11081113
if(clientInterface)
11091114
clientInterface->NotifyNumConnectionsChanged(nPrevNodeCount);
11101115
}

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ class CNode
615615
const bool fInbound;
616616
bool fNetworkNode;
617617
bool fSuccessfullyConnected;
618-
bool fDisconnect;
618+
std::atomic_bool fDisconnect;
619619
// We use fRelayTxes for two purposes -
620620
// a) it allows us to not relay tx invs before receiving the peer's version message
621621
// b) the peer may tell us in its version message that we should not relay tx invs

0 commit comments

Comments
 (0)