Skip to content

Commit

Permalink
Merge bitcoin#9225: Fix some benign races
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
laanwj authored and CryptoCentric committed Feb 25, 2019
1 parent 67cfbdf commit 935a8cc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
53 changes: 22 additions & 31 deletions src/addrman.h
Expand Up @@ -478,6 +478,7 @@ class CAddrMan
//! Return the number of (unique) addresses in all tables.
size_t size() const
{
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
return vRandom.size();
}

Expand All @@ -497,13 +498,11 @@ class CAddrMan
//! Add a single address.
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
LOCK(cs);
bool fRet = false;
{
LOCK(cs);
Check();
fRet |= Add_(addr, source, nTimePenalty);
Check();
}
Check();
fRet |= Add_(addr, source, nTimePenalty);
Check();
if (fRet)
LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
return fRet;
Expand All @@ -512,14 +511,12 @@ class CAddrMan
//! Add multiple addresses.
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
LOCK(cs);
int nAdd = 0;
{
LOCK(cs);
Check();
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
Check();
}
Check();
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
Check();
if (nAdd)
LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
return nAdd > 0;
Expand All @@ -528,23 +525,19 @@ class CAddrMan
//! Mark an entry as accessible.
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
{
{
LOCK(cs);
Check();
Good_(addr, nTime);
Check();
}
LOCK(cs);
Check();
Good_(addr, nTime);
Check();
}

//! Mark an entry as connection attempted to.
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
{
{
LOCK(cs);
Check();
Attempt_(addr, fCountFailure, nTime);
Check();
}
LOCK(cs);
Check();
Attempt_(addr, fCountFailure, nTime);
Check();
}

/**
Expand Down Expand Up @@ -578,12 +571,10 @@ class CAddrMan
//! Mark an entry as currently-connected-to.
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
{
{
LOCK(cs);
Check();
Connected_(addr, nTime);
Check();
}
LOCK(cs);
Check();
Connected_(addr, nTime);
Check();
}

void SetServices(const CService &addr, ServiceFlags nServices)
Expand Down
2 changes: 1 addition & 1 deletion src/net.h
Expand Up @@ -711,7 +711,7 @@ class CNode
const bool fInbound;
bool fNetworkNode;
std::atomic_bool fSuccessfullyConnected;
bool fDisconnect;
std::atomic_bool fDisconnect;
// We use fRelayTxes for two purposes -
// a) it allows us to not relay tx invs before receiving the peer's version message
// b) the peer may tell us in its version message that we should not relay tx invs
Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Expand Up @@ -74,7 +74,7 @@ CBlockIndex *pindexBestHeader = NULL;
CWaitableCriticalSection csBestBlock;
CConditionVariable cvBlockChange;
int nScriptCheckThreads = 0;
bool fImporting = false;
std::atomic_bool fImporting(false);
bool fReindex = false;
bool fTxIndex = true;
bool fAddressIndex = false;
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Expand Up @@ -162,7 +162,7 @@ extern uint64_t nLastBlockSize;
extern const std::string strMessageMagic;
extern CWaitableCriticalSection csBestBlock;
extern CConditionVariable cvBlockChange;
extern bool fImporting;
extern std::atomic_bool fImporting;
extern bool fReindex;
extern int nScriptCheckThreads;
extern bool fTxIndex;
Expand Down

0 comments on commit 935a8cc

Please sign in to comment.