torcontrol: Improve connection logging #36759

pull ViniciusCestarii wants to merge 2 commits into bitcoin:master from ViniciusCestarii:prevent-tor-log-spam changing 4 files +47 −11
  1. ViniciusCestarii commented at 4:36 PM on November 4, 2026: contributor

    Before eae193e750235e4023b3e6313284eede2cd7a022, Connect() only started the connection and returned true. If tor wasn't running the refusal was reported later and only logged under -debug=tor. Since eae193e750 (in v32), every failed reconnect attempt logs:

    2026-11-04T14:14:08Z connect() to 127.0.0.1:9051 failed after wait: Connection refused (111)
    2026-11-04T14:14:08Z [warning] tor: Error connecting to address 127.0.0.1:9051
    2026-11-04T14:14:08Z [warning] tor: Initiating connection to Tor control port 127.0.0.1:9051 failed
    

    On a default configured node without tor, this pollutes debug.log with 18 lines every hour.

    This improves it

  2. jpk68 commented at 6:40 PM on November 4, 2026: contributor

    ACK ac6d31e7a9320773b98934478d0e2ea3c17dfb65

  3. fanquake commented at 6:48 PM on November 4, 2026: member
  4. in src/torcontrol.cpp:98 in ac6d31e7a9
      94 | @@ -95,9 +95,9 @@ bool TorControlConnection::Connect(const std::string& tor_control_center)
      95 |          return false;
      96 |      }
      97 |  
      98 | -    m_sock = ConnectDirectly(control_service.value(), /*manual_connection=*/true);
      99 | +    m_sock = ConnectDirectly(control_service.value(), /*manual_connection=*/false);
    


    pinheadmz commented at 7:03 PM on November 4, 2026:

    Why did you change this line? I think it actually shouldn't matter, its just residue of how all of our socket handling code has the perspective of bitcoin p2p even when that's not what we're doing with the socket.


    ViniciusCestarii commented at 12:12 AM on November 5, 2026:

    Because it is used only on:

        if (manual_connection) {
            LogInfo("%s", error_message);
        } else {
            LogDebug(BCLog::NET, "%s", error_message);
        }
    

    Setting it to false makes it log as debug, otherwise it would still log "connect() to 127.0.0.1:9051 failed after wait: Connection refused (111)"

  5. pinheadmz commented at 7:07 PM on November 4, 2026: member

    I've noticed these annoying warnings as well on my own node without tor and looked into it. There is an exponential backoff with limit of RECONNECT_TIMEOUT_MAX{600.0}; // 10 min cap so after ~20 minutes or so the log messages drop to once every ten minutes. We keep attempting this connection so that if the user starts tor later in the node's lifetime, we just pick it up and work with it.

    Personally I think it's annoying when you first start the node but then I dont mind seeing the attempts 6x/hour. Reminds me I need to start tor!

    So I need help understanding the motivation for this change.

  6. ViniciusCestarii commented at 12:23 AM on November 5, 2026: contributor

    So I need help understanding the motivation for this change.

    My main motivation is that it can be confusing for users that are not running tor since it is a warning level message they might think it is a config error or open new issue (#36691) and also pollutes logs. If the user wants to see tor debug logs they can set -debug=tor like it was before v32

  7. pinheadmz approved
  8. pinheadmz commented at 8:36 PM on November 5, 2026: member

    ACK ac6d31e7a9320773b98934478d0e2ea3c17dfb65

    Built and tested on macos/arm64. Code changes are justified and makes the logs cleaner / more like pre-32 behavior. Thanks @ViniciusCestarii for answering my questions!

    Also wanted to throw out the option of using the new LogWarnThenDebug() if you think that makes sense. Happy to re review.

    <details><summary>Show Signature</summary>

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA256
    
    ACK ac6d31e7a9320773b98934478d0e2ea3c17dfb65
    -----BEGIN PGP SIGNATURE-----
    
    iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrs6EcbFIAAAAAABAAO
    bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6250P/2A3R5tuB0dFQVp//prO
    MvcblYzRWR5XwS84Pq0lnJYWWgy2g5AtPnHx1Kxuy68VVuU2BCtA7c8Cniwaa3gb
    yPZiBNswAksLjV5mwPiw8rBc3ht+D4Zplp1dpMaZKLM6lmBUJSMRjWE5U6dm7iGq
    rPB9OWvja2qmPmInItoZcA0VXc0pMwMHm2SrHKFj0bVfAd5LeMnLU9j20zH7Z1eB
    /mdSJ5fYiVJOfqNCh0wjDZh9YOswzpDxVOpLsSZ8AqzK70n6xJxfF//Oh53dSEvt
    QZBYkUVmgR5JJXStEMbEV+xQimbwj9yXgqefix1W6hZjcteG5jQSZ5esU7r5AVAx
    u64OrkAe1hJ1hKb1qK8a18SIv2Iuj4KdYOUoNdxdMGXBvOcKAZh6/gJCrAzIKt5D
    S/UyvDicKPpzAJLGNhoAC5G7D2TttuOtWB6+Qod9KF91TufvLfhoVtEJGk/LiukQ
    hi9cmPcq48mfdD7Oa39tDuoRMRmRDvSauM3tDdbkxV6eNo3VxrIJtrDeLO8djveA
    6urwiVLbnbW9o/Lw+akMCVxHv5GcRy1jsjSON7zminnrX+8WeI6IA5Ayj53Nb/9I
    s2KrNx/n2/CJ7hV24wcwAf5Uf2++pWlDO9B4hwlT3RzH458O/idUVtMBvY6KhqMP
    knK0OD71w1vhpa5U4dtovh+4
    =KbM6
    -----END PGP SIGNATURE-----
    

    pinheadmz's public key is on openpgp.org

    </details>

  9. DrahtBot commented at 8:36 PM on November 5, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK fjahr
    Concept ACK ismaelsadeeq
    Stale ACK jpk68, pinheadmz, winterrdog

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36970 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36970.svg"></sub> (RFC: netbase: Remove manual_connection and return the failure reason to callers by fjahr)
    • #36880 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36880.svg"></sub> (init: Fix surprising behaviors of negated options by fjahr)
    • #36170 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36170.svg"></sub> (net: stop sharing P2P listeners with automatic Tor services by l0rinc)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  10. fanquake commented at 9:05 PM on November 5, 2026: member

    If we decide to do this, we should probably also backport into 32.x.

  11. sedited requested review from fjahr on Nov 5, 2026
  12. winterrdog commented at 11:15 PM on November 5, 2026: contributor

    tested ACK ac6d31e7a9320773b98934478d0e2ea3c17dfb65

    successfully built and tested with clang++-20 on Debian, targeting x86_64.


    logs look less noisy (quite peaceful now) when i ran bitcoind in regtest

    re: #36759#pullrequestreview-5791717944

    LogWarnThenDebug strikes a balance between various user needs, i think it is OK to take it up. the log message that shows failure to connect to tor control is logged exactly once. logs will look sth like this:

    <details open><summary>logs</summary>

    ...
    2026-11-05T22:59:01Z torcontrol thread start
    2026-11-05T22:59:01Z initload thread exit
    2026-11-05T22:59:01Z [warning] Error connecting to address 127.0.0.1:9051
    2026-11-05T22:59:01Z [warning] Initiating connection to Tor control port 127.0.0.1:9051 failed
    2026-11-05T22:59:01Z Bound to 127.0.0.1:18445
    2026-11-05T22:59:01Z Bound to [::]:18444
    2026-11-05T22:59:01Z Bound to 0.0.0.0:18444
    2026-11-05T22:59:01Z Loaded 0 addresses from "anchors.dat"
    ...
    

    </details>

  13. fjahr commented at 2:04 AM on November 6, 2026: contributor

    I agree that the current behavior isn't ideal but it seems that an bigger problem than noisy logs is that you wouldn't see any logs unless you had -debug=tor if you had -torcontrol set explicitly, i.e. you clearly want to use Tor but there is an issue and it doesn't work. That's why I think the current v32 behavior was an improvement to the previous behavior. Instead of reverting, I would suggest a more complete fix that works well for both scenarios: When the user configured Tor they should see logs and when they didn't they should not see them (only with -debug=tor).

    I have drafted the change so people can decide if they like this more: https://github.com/fjahr/bitcoin/commits/2026-11-torlog/ But note that this also includes a bit of additional improvements like making the logging less noisy when it does happen and deals with losing the connection along the way.

  14. ViniciusCestarii commented at 12:20 PM on November 6, 2026: contributor

    Thanks for reviewing! I agree that logging for tor users is useful. I like @fjahr proposal but also tor is meant to work out of box (without the need of any explicit config), so users that never set tor specific config would not know that tor isn't being used unless they run with -debug=tor.

    The best both worlds would be what @fjahr proposed + if no explicit tor config, log the first failure once at info level (e.g. "Not using Tor (no control port at 127.0.0.1:9051)") similar to @pinheadmz's LogWarnThenDebug() but at info. @fjahr if you're OK with it, I can pull your commits into this PR (keeping you as author) and add the info-once part on top. Or if you'd rather open your own PR, I'll close this one in favor of it.

  15. fjahr commented at 10:12 AM on November 7, 2026: contributor

    if you're OK with it, I can pull your commits into this PR

    Sure, go ahead!

  16. ViniciusCestarii force-pushed on Nov 7, 2026
  17. ViniciusCestarii renamed this:
    torcontrol: log control port connection failures at debug level
    torcontrol: Improve connection logging
    on Nov 7, 2026
  18. ViniciusCestarii force-pushed on Nov 7, 2026
  19. DrahtBot added the label CI failed on Nov 7, 2026
  20. ViniciusCestarii commented at 4:23 PM on November 7, 2026: contributor

    Force-pushed d5f6a9b6c96b30eaa9740eda5b9a98474a3aef59 adding @fjahr proposed changes + log the first failure once at info level. Updated PR title and description.

  21. DrahtBot removed the label CI failed on Nov 7, 2026
  22. in src/torcontrol.h:144 in 996846efe6
     140 | @@ -140,6 +141,7 @@ class TorController
     141 |      CThreadInterrupt m_interrupt;
     142 |      std::thread m_thread;
     143 |      const std::string m_tor_control_center;
     144 | +    const bool m_manual_connection{false};
    


    pinheadmz commented at 3:52 PM on November 9, 2026:

    996846efe6d0e7ee67d7fc7857321f46d26df1bc

    I feel like the value should keep the name m_tor_control_configured that is declared by init.cpp. "Manual" connection doesn't make sense to me in the tor control context. For that matter, I also feel like the the value in netbase could probably also be renamed log_as_info (or verbose_failure) or something because it has no effect until LogConnectFailure() is called which could be called by P2P, torcontrol, Proxy, even bitcoin-cli...


    ViniciusCestarii commented at 12:08 PM on November 10, 2026:

    Agree it should be renamed, but it's out of scope here. I could do it as follow up

  23. pinheadmz commented at 6:49 PM on November 9, 2026: member

    I think this has gotten perhaps too complicated. Even with Claude making me a chart I can't wrap my head around all the details:

    • There's messages logging at Warn, Info, and Debug levels.
    • There's torcontrol options being set manually and by default.
    • There's the user's log level setting at default (info) or debug.
    • There's first connection error and not-first error.
    • There's a re-implementation of LogWarnThenDebug() but for Info level and then debug (I think?)
    • Netbase and torcontrol both write logs, at different levels

    I think we should discuss simpler logic that cleans up the log and still follows the guidelines about log level.

    1. Always set "manual" to false" netbase only logs debug. Handle more interesting logging directly from torcontrol
    2. Use LogWarnThenDebug instead of adding new state and effectively LogInfoThenDebug

    I "generated" a commit along with @fjahr "lost connection" commit here: https://github.com/bitcoin/bitcoin/compare/master...pinheadmz:bitcoin:torcontrol-log-simple

    Tested by turning tor on and off during / before bitcoind. This feels right to me:

    default user

    start with tor: Got tor service ID... break tor: Lost connection... (only once) restart tor: Got tor service ID...

    start without tor: <nothing> restart tor: Got service ID... break tor: Lost connection... (only once)

    -listenonion / -torcontrol / -torpassword user

    Same as default, plus a single [warning] tor: Error connecting to address... the first time any connection attempt fails (at startup if tor isn't running, or on the first reconnect attempt after tor breaks). Never repeated for the lifetime of the process.

    -debug user

    logs every single retry attempt

  24. torcontrol: Log when an active onion service is lost e998f12b55
  25. ViniciusCestarii force-pushed on Nov 10, 2026
  26. ViniciusCestarii commented at 12:23 PM on November 10, 2026: contributor

    Thanks for reviewing! Force-pushed ffd928d32de2137ff7178847d078ade064671563 with @pinheadmz suggested commits. I agree with renaming tor_control_configured and that net logs were "leaking".

    This drops the info log on d5f6a9b6c96b30eaa9740eda5b9a98474a3aef59, which purpose was to remind a default user that wanted tor to activate tor. Fine leaving it out to keep this simple.

  27. fjahr commented at 9:39 PM on November 10, 2026: contributor

    I think this has gotten perhaps too complicated. Even with Claude making me a chart I can't wrap my head around all the details

    I "generated" a commit

    The only behavior changes from my suggested changes earlier is 1. dropping ConnectDirectly(..., /*manual_connection=*/false) to prevent logging the failure reason when tor is configured (which I would prefer to keep). 2. Replace LogWarning with LogWarnThenDebug (where I also think being more noisy would be better, it's also more in line with our logging behavior for similar cases like -proxy, -i2psam etc.). The rest is basically just the variable rename. I also don't see how this is any less complicated than what I suggested. It seems you just didn't like the last commit and for the rest inline comments would have been a better choice allowing for deeper discussion on these changes? It seems claude mostly reimplemented my suggestions with minor improvements but didn't give me credit for it.

  28. pinheadmz commented at 1:58 PM on November 11, 2026: member

    but didn't give me credit for it.

    Sorry @fjahr I'm not trying to step on toes or take credit, I didn't intend for the author to merge my commit, I just wanted to start a discussion:

    I think we should discuss simpler logic that cleans up the log and still follows the guidelines about log level.

    And since its "just" logging I'm not dying on any hill here, and just asked claude to whip up something I could test out the user experience.

    So just to take it back a step, here's things I like a lot:

    • (warn then debug) log the first time tor control disconnects, or on startup if its not available
    • (info) log again when tor control reconnects
    • (debug) move every connection retry (after the first) down a log level so its less spammy

    Something I don't care about but won't oppose:

    • Changing logging behavior based on user configuration (my opinion is that debug=tor is the only configuration this PR should react to)

    Something I don't like but don't care enough to push back against if out-voted:

    • setting manual_connection=true so that every error is logged a second time from netbase
  29. ViniciusCestarii commented at 5:20 PM on November 11, 2026: contributor

    My goal was only to stop the log spam for users who don't run tor and both versions do that. Since @pinheadmz said they wouldn't oppose the config-dependent behavior or manual_connection, I'll go back to @fjahr's version (manual_connection=tor_control_configured and LogWarning), keeping the tor_control_configured rename. I'll add @fjahr as author accordingly. Happy to switch if anyone objects.

  30. ViniciusCestarii force-pushed on Nov 11, 2026
  31. ViniciusCestarii commented at 5:53 PM on November 11, 2026: contributor

    Force-pushed 11486ab3c8fc756fa9d8b93235be27b9cb11efe5 as mentioned in #36759 (comment)

  32. ismaelsadeeq commented at 1:53 PM on November 12, 2026: member

    Concept ACK, this log is indeed noisy. Should be behind a category.

  33. sedited commented at 2:10 PM on November 12, 2026: contributor

    (warn then debug) log the first time tor control disconnects, or on startup if its not available

    Yes, this seems like a good candidate for LogWarnThenDebug to me. I'd prefer that over a single LogDebug here.

  34. in src/init.cpp:2319 in 11486ab3c8 outdated
    2313 | @@ -2314,7 +2314,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
    2314 |                                      "for the automatically created Tor onion service."),
    2315 |                                    onion_service_target.ToStringAddrPort()));
    2316 |          }
    2317 | -        node.tor_controller = std::make_unique<TorController>(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target);
    2318 | +        const bool tor_control_configured{gArgs.IsArgSet("-listenonion") || gArgs.IsArgSet("-torcontrol") || gArgs.IsArgSet("-torpassword")};
    2319 | +        node.tor_controller = std::make_unique<TorController>(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target,
    2320 | +                                                              tor_control_configured);
    


    fjahr commented at 2:25 PM on November 24, 2026:

    nit: Could switch to the local args here instead of gArgs if you retouch.


    ViniciusCestarii commented at 7:12 PM on November 25, 2026:

    Done in bd9945c1a5656c7e39bad008951e6780e6654bd1

  35. fjahr commented at 7:48 PM on November 24, 2026: contributor

    this seems like a good candidate for LogWarnThenDebug

    There seems to be consensus for this now and I am fine with switching to LogWarnThenDebug here. As I mentioned, my concern was that torcontrol would then log differently from similar options like -proxy and -i2psam, which still log every failed attempt. I opened #36969 which uses LogWarnThenDebug there too, to address this.

    setting manual_connection=true so that every error is logged a second time from netbase

    Netbase and the caller logging the same failure at different levels is not great. With LogWarnThenDebug I guess it makes sense to pass false here, #36969 does the same for the proxies. What I wanted to keep is the failure reason, which then only shows up with -debug=net and that did seem to feel right. What makes this annoying is that ConnectDirectly() picks the log level while only the caller knows how much the failure matters, so I am suggesting to remove manual_connection entirely and return the reason to the caller instead which then can choose the log level, feedback welcome: #36970

  36. ViniciusCestarii force-pushed on Nov 25, 2026
  37. ViniciusCestarii commented at 7:12 PM on November 25, 2026: contributor

    Thanks for the reviews! It seems consensus was reached so I force-pushed bd9945c1a5656c7e39bad008951e6780e6654bd1 to use LogWarnThenDebug instead of LogWarning, set manual_connection=false back and also addressed nit #36759 (review).

  38. in src/torcontrol.cpp:112 in bd9945c1a5
     105 | @@ -106,9 +106,13 @@ bool TorControlConnection::Connect(const std::string& tor_control_center)
     106 |          return false;
     107 |      }
     108 |  
     109 | -    m_sock = ConnectDirectly(control_service.value(), /*manual_connection=*/true);
     110 | +    m_sock = ConnectDirectly(control_service.value(), /*manual_connection=*/false);
     111 |      if (!m_sock) {
     112 | -        LogWarning("tor: Error connecting to address %s", tor_control_center);
     113 | +        if (tor_control_configured) {
     114 | +            LogWarnThenDebug(BCLog::TOR, "Error connecting to address %s", tor_control_center);
    


    fjahr commented at 10:04 PM on November 25, 2026:

    As this stands now the initial warning log doesn't have a reference to Tor anymore so users might miss it or find it confusing: [warning] Error connecting to address 127.0.0.1:9051. Keeping the prefix would work but it would make the debug output a bit ugly: [tor] tor: Error connecting.... I would suggest making the message itself a bit more precise and mentioning Tor explicitly (ideally aligning the message below as well).

                LogWarnThenDebug(BCLog::TOR, "Error connecting to Tor control port %s", tor_control_center);
    

    I also looked into fixing this on the macro level itself since this could be a general problem but the other call sides are not as severe for different reasons and I wouldn't want to change the output generally here since logging is a hotly debated topic.


    ViniciusCestarii commented at 12:31 PM on November 26, 2026:

    Well spotted, done in 4ec95781e0c8fed7577f661bbe04967bea76365d. It looks nicer

    2026-11-26T12:21:09Z [tor] Attempting to connect to Tor control port 127.0.0.1:9051
    2026-11-26T12:21:09Z [tor] Error connecting to Tor control port 127.0.0.1:9051
    2026-11-26T12:21:09Z [tor] Not connected to Tor control port 127.0.0.1:9051, retrying in 2.25 s
    
  39. ViniciusCestarii force-pushed on Nov 26, 2026
  40. torcontrol: Warn about failed connections only if Tor control is configured
    The Tor control thread retries the connection indefinitely. Previously
    every failed attempt logged two warnings plus an info line from netbase,
    even for default nodes where the user never asked for Tor.
    
    Only warn if the user explicitly configured Tor control with
    -listenonion, -torcontrol or -torpassword, and treat the connection as
    manual in that case. Otherwise log failures at debug level only. The
    redundant "Initiating connection" warning is removed.
    
    Co-authored-by: Matthew Zipkin <pinheadmz@pm.me>
    4ec95781e0
  41. ViniciusCestarii force-pushed on Nov 26, 2026
  42. ViniciusCestarii commented at 12:29 PM on November 26, 2026: contributor

    Thanks @fjahr for the review! Force-pushed 4ec95781e0c8fed7577f661bbe04967bea76365d addressing #36759 (review)

  43. fjahr commented at 12:38 PM on November 26, 2026: contributor

    ACK 4ec95781e0c8fed7577f661bbe04967bea76365d

  44. DrahtBot requested review from winterrdog on Nov 26, 2026
  45. DrahtBot requested review from pinheadmz on Nov 26, 2026
  46. DrahtBot requested review from ismaelsadeeq on Nov 26, 2026

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: 2026-12-01 02:51 UTC

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