Skip to content

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

Open
saksham-1304 wants to merge 1 commit intobitcoin:masterfrom
saksham-1304:respect-networkactive-flag
Open

net: don't perform network activity when networkactive=0#34467
saksham-1304 wants to merge 1 commit intobitcoin:masterfrom
saksham-1304:respect-networkactive-flag

Conversation

@saksham-1304
Copy link

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

# Start with networkactive=0 - should see no network activity logs
bitcoind -networkactive=0

# Start with networkactive=1 (default) - normal behavior
bitcoind

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

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 bitcoin#34190
Copilot AI review requested due to automatic review settings January 30, 2026 23:44
@DrahtBot DrahtBot added the P2P label Jan 30, 2026
@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 30, 2026

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.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@luke-jr
Copy link
Member

luke-jr commented Feb 2, 2026

Those aren't strictly P2P network activity, but I guess it makes sense.

@saksham-1304
Copy link
Author

@luke-jr Sir can you please guide me what should i do further wrt this PR?

@willcl-ark
Copy link
Member

In my opinion this PR attemps to solve the log spam issue far too literally, without consideration of how the fix should integrate with our software on a larger scale. Whilst it does "fix the spam" (hooray, mission complete!), it breaks how setnetworkactive works, making boot-time and runtime work differently from each other. Setting the flag at boot time now permanently disables various subsystems, and they cannot be re-enabled at runtime. Toggling states also does not work as expected.

If bitcoind from PR is started with sertnetworkactive=0 then port mapping and Tor remain disabled for the duration of program execution, even after running bitcoin-cli setnetworkactive true, which I think is pretty undesirable.

I also believe my approach in #34486 is wired up in a much more logical way. The only downside of my own approach in my opinion, is that its adding more libevent code, which I know we are angling to drop at some point...

I will add this rationale to #34486

@sedited
Copy link
Contributor

sedited commented Mar 14, 2026

I agree with willcl-ark here, this PR is not comprehensive enough, while its alternative PR has already managed to get conceptual buy in. In order to keep our review resources focused I think this PR should be closed in favour of #34466.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bitcoind shouldn't perform network activity if the option networkactive=0, e.g. [addrman] Selected IP:PORT from tried

6 participants