From b0f75847eaf5a85cd614c4f990f20172ad8185fc Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 13 May 2016 15:48:10 -0700 Subject: [PATCH] Fix issue #717 where if addrman is starved of addresses (e.g. on testnet) the Select_() function will loop endlessly trying to find an address, and therefore eat up 100% cpu time on the 'opencon' thread. Solution is to (1) add a delay to the loop and (2) restrict the number of attempts to find an address. On exiting the loop, we return to an outer loop in net.cpp which will sleep, add seed nodes and calcualte new addresses. --- src/addrman.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index c41ee3f9f..82c4ea277 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -336,11 +336,14 @@ CAddrInfo CAddrMan::Select_() if (size() == 0) return CAddrInfo(); + // Track number of attempts to find a table entry, before giving up + int nRetries = 0; + // Use a 50% chance for choosing between tried and new table entries. if (nTried > 0 && (nNew == 0 || GetRandInt(2) == 0)) { // use a tried node double fChanceFactor = 1.0; - while (1) { + while (nRetries++ < ADDRMAN_TRIED_BUCKET_COUNT) { int nKBucket = GetRandInt(ADDRMAN_TRIED_BUCKET_COUNT); int nKBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE); if (vvTried[nKBucket][nKBucketPos] == -1) @@ -351,11 +354,12 @@ CAddrInfo CAddrMan::Select_() if (GetRandInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30)) return info; fChanceFactor *= 1.2; + MilliSleep(100); } } else { // use a new node double fChanceFactor = 1.0; - while (1) { + while (nRetries++ < ADDRMAN_NEW_BUCKET_COUNT) { int nUBucket = GetRandInt(ADDRMAN_NEW_BUCKET_COUNT); int nUBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE); if (vvNew[nUBucket][nUBucketPos] == -1) @@ -366,8 +370,11 @@ CAddrInfo CAddrMan::Select_() if (GetRandInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30)) return info; fChanceFactor *= 1.2; + MilliSleep(100); } } + + return CAddrInfo(); } #ifdef DEBUG_ADDRMAN