net: don’t perform network activity when networkactive=0 #34467

pull saksham-1304 wants to merge 1 commits into bitcoin:master from saksham-1304:respect-networkactive-flag changing 2 files +15 −3
  1. saksham-1304 commented at 11:44 pm on January 30, 2026: none

    Problem

    When starting bitcoind with -networkactive=0, the node still performs various network activities despite the user explicitly requesting that the P2P network be inactive. This results in unnecessary log messages and network overhead:

    • [addrman] Selected IP:PORT from tried - Address manager selections
    • [net:error] Could not get best interface for default route - Gateway detection attempts
    • [tor] Error connecting to Tor control socket - Tor control connection attempts
    • [tor] Not connected to Tor control port - Tor reconnection attempts
    • [net] portmap: Could not determine IPv4/IPv6 default gateway - UPnP/NAT-PMP attempts

    This defeats the purpose of the -networkactive flag, which is documented as “Enable all P2P network activity (default: 1)”.

    Steps to Reproduce

    1. Start bitcoind with bitcoind.exe -networkactive=0
    2. Observe logs - despite the flag being set to disable network activity, addrman selections and network-related errors are logged

    Expected Behavior

    No network activity should occur when -networkactive=0 is set at startup. The node should remain idle until network is activated via RPC command setnetworkactive true.

    Solution

    This PR ensures that when networkactive=0, no network initialization or activity occurs by:

    1. ThreadOpenConnections (src/net.cpp)

    Added a check at the beginning of the main while loop to pause all outbound connection logic when fNetworkActive is false. This prevents:

    • Address selection from addrman
    • Connection attempts to peers
    • Feeler connections
    • Block relay connection attempts

    This check follows the existing pattern used for DNS seed querying (lines 2345-2349) and uses the same synchronization mechanism (fNetworkActive atomic flag and sleep_for with interruption support).

    2. StartMapPort (src/init.cpp)

    Made NAT-PMP port mapping startup conditional on -networkactive being true. This prevents:

    • Port mapping initialization
    • Gateway detection attempts
    • Related network errors and log spam

    3. StartTorControl (src/init.cpp)

    Made Tor control thread startup conditional on -networkactive being true. This prevents:

    • Tor control socket connection attempts
    • Tor-related error logs
    • Unnecessary event loop setup

    Testing

    The fix has been verified to:

    • Compile without errors or warnings
    • Follow existing code patterns and conventions used in the same functions
    • Use the same argument parsing method as existing code (args.GetBoolArg("-networkactive", true))
    • Respect the interruption mechanism used throughout the codebase

    Manual Testing

    0# Start with networkactive=0 - should see no network activity logs
    1bitcoind -networkactive=0
    2
    3# Start with networkactive=1 (default) - normal behavior
    4bitcoind
    

    Affected Code Paths src/net.cpp: CConnman::ThreadOpenConnections() - Main connection management loop src/init.cpp Bitcoin Core initialization - MapPort and TorControl startup

    Notes This change is backward compatible - default behavior (networkactive=1) is unchanged Dynamic network activation via setnetworkactive true RPC will resume ThreadOpenConnections activity MapPort and TorControl remain stopped if started with networkactive=0 (can be addressed in follow-up if needed for dynamic activation)

    Fixes #34190

  2. net: don't perform network activity when networkactive=0
    When starting bitcoind with -networkactive=0, various network activities
    should not occur, including:
    - Address selection from addrman
    - NAT-PMP port mapping attempts
    - Tor control socket connection attempts
    
    Fix this by:
    1. Adding a check in ThreadOpenConnections to pause the main outbound
       connection loop when fNetworkActive is false, preventing unnecessary
       addrman selections and connection attempts
    2. Making StartMapPort conditional on -networkactive flag
    3. Making StartTorControl conditional on -networkactive flag
    
    Fixes #34190
    717d6ce3bb
  3. saksham-1304 requested review from Copilot on Jan 30, 2026
  4. DrahtBot added the label P2P on Jan 30, 2026
  5. DrahtBot commented at 11:44 pm on January 30, 2026: contributor

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

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #34486 (net: Reduce local network activity when networkactive=0 by willcl-ark)

    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.

  6. Copilot commented at 0:26 am on January 31, 2026: none
    Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
  7. luke-jr commented at 4:19 pm on February 2, 2026: member
    Those aren’t strictly P2P network activity, but I guess it makes sense.
  8. saksham-1304 commented at 11:18 pm on February 6, 2026: none
    @luke-jr Sir can you please guide me what should i do further wrt this PR?

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-02-17 12:13 UTC

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