Skip to content

Commit dbfaade

Browse files
committed
Fix AddrMan locking
1 parent 047ea10 commit dbfaade

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/addrman.h

Lines changed: 10 additions & 13 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;

0 commit comments

Comments
 (0)