bitcoin-qt -noconnect uses 100% CPU #1664

issue dooglus openend this issue on August 9, 2012
  1. dooglus commented at 1:00 am on August 9, 2012: contributor

    Maybe I’m doing it wrong, but if I specify “-noconnect” in an attempt to have bitcoin-qt not connect to any peers, the ThreadOpenConnections2() function goes into a tight loop that runs forever.

    The following 1-line change fixes the problem:

     0diff --git a/src/net.cpp b/src/net.cpp
     1index e10829a..fe1712a 100644
     2--- a/src/net.cpp
     3+++ b/src/net.cpp
     4@@ -1367,7 +1367,7 @@ void ThreadOpenConnections2(void* parg)
     5     printf("ThreadOpenConnections started\n");
     6
     7     // Connect to specific addresses
     8-    if (mapArgs.count("-connect"))
     9+    if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size())
    10     {
    11         for (int64 nLoop = 0;; nLoop++)
    12         {
    
  2. luke-jr commented at 1:40 am on August 9, 2012: member
    I don’t think -noconnect was ever supported as a valid option.
  3. gmaxwell commented at 1:48 am on August 9, 2012: contributor
    As Luke says, that wasn’t an intended option as far as I know. But it sounds like a reasonable thing to support, though we should probably also audit the other cases were a no wasn’t really anticipated.
  4. laanwj commented at 11:18 am on August 18, 2012: member

    This also appears to happen when it simply has no nodes to connect to, even without -noconnect. In my case I have a testnet node that has no addresses to connect to, so it keeps cycling through them and the “bitcoin-opencon” thread takes a lot of CPU time.

    In my case, it keeps selecting the same peer every half a second (with a nonstandard port #, so after 50 tries), trying to connect to it:

    0trying connection XX.XX.XX.XX:18334 lastseen=1542.8hrs
    1connect() failed after select(): Connection refused
    

    Not yet sure why that takes so much CPU usage %, though, as the thread sleeps mostly.

    Edit: According to callgrind, most time is spent inside CAddrMan::Select(int), calling CAddrMan::Select_(int), calling GetRandInt(int).

    Is that random function so expensive? I think so, after changing to simply use rand(), putting at the top of addrman.cpp a hack

    0#define GetRandInt(x) (rand()%(x))
    

    The CPU usage dropped back to 4% only. Still significant, but not close to holding up a core. Using rand might be good enough here, as there is no need for cryptographically secure number here. See also issue #1057.

    Anyway, using something like exponential backoff in the connection thread is probably the real fix. Just wanted to know why peer selection was so expensive.

  5. laanwj commented at 6:50 am on September 19, 2012: member
    Is this fixed in 0.7.0?
  6. dooglus commented at 6:15 pm on September 19, 2012: contributor

    It doesn’t use 100% CPU, but it doesn’t work either - it still tries to connect:

    0(gdb) run -server -nolisten -noconnect
    1[...]
    2Bitcoin version v0.7.0rc3-dirty-beta (2012-09-12 14:07:22 -0400)
    3[...]
    4trying connection 92.240.248.99:8333 lastseen=2.7hrs
    5connected 92.240.248.99:8333
    
  7. dooglus commented at 6:19 pm on September 19, 2012: contributor

    I see Pieter’s commit fixing the 100% CPU bug in the 0.7.0 code:

    0commit f161a2c2114cd7d950248ce75a91ba1923e9abb1
    1Author: Pieter Wuille <pieter.wuille@gmail.com>
    2Date:   Tue Aug 21 17:32:04 2012 +0200
    3
    4    Fix infinite loops in connection logic
    

    but nothing implementing “-noconnect” (which apparently was never intended to work anyway).

  8. sipa commented at 10:11 pm on September 19, 2012: member

    Since -connect=X means “connect only to X”, -noconnect would mean “don’t just connect to one peer”, which is exactly what it does :)

    Ok, I admit, it wasn’t ever considered.

  9. laanwj commented at 11:54 am on September 21, 2012: member
    -noconnect makes no sense. But aren’t there other ways already to say “don’t connect to any peers”, or limit the number of outgoing connections?
  10. dooglus commented at 4:02 pm on September 21, 2012: contributor

    I’ve used “-connect=127.0.0.1” in the past to get no connections.

    I’m wondering whether “-maxconnections=0” would work too:

     0Starting program: /home/chris/Bin/bitcoin -maxconnections=0
     1[...]
     2Bitcoin version v0.7.0rc3-dirty-beta (2012-09-12 14:07:22 -0400)
     3[...]
     4send version message: version 60002, blocks=199858, us=0.0.0.0:0, them=0.0.0.0:0, peer=127.0.0.1:0
     5[...]
     6CTxMemPool::accept() : accepted 058cdc9c4d (poolsz 1)
     7[...]
     8Added 4 addresses from 178.18.90.41: 1646 tried, 12503 new
     9Added 4 addresses from 87.155.86.94: 1646 tried, 12506 new
    10Added 4 addresses from 173.242.112.53: 1646 tried, 12507 new
    11[...]
    12Added 3 addresses from ::: 1646 tried, 12508 new
    1394 addresses found from DNS seeds
    14ThreadDNSAddressSeed exited
    

    The GUI says “0 active connections” but the log (above) appears to show some communication with peers, so I’m a bit confused about what’s going on there.

  11. dooglus commented at 4:24 pm on September 21, 2012: contributor

    In particular, my client somehow received notification of a payment to one of my addresses (not a payment to self) while I was running with “-maxconnections=0”. How can that happen?

    0CTxMemPool::accept() : accepted 058cdc9c4d (poolsz 1)
    

    When I try sending a payment it doesn’t send, as expected:

    0Status: 0/unconfirmed, has not been successfully broadcast yet
    1Date: 12-09-21 09:22
    2To: satoshidice lessthan 8000 1dice6YgEVBf88erBFra9BHf6ZMoyvG88
    

    but for some reason I was able to receive a payment.

  12. laanwj commented at 8:02 am on October 26, 2013: member
    The CPU issue was solved, and mapMultiArgs["-connect"].size() > 0 was added, so closing
  13. laanwj closed this on Oct 26, 2013

  14. suprnurd referenced this in commit 8c1e5e8384 on Dec 5, 2017
  15. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-10-05 04:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me