net: prevent duplication manual connections (take 2) #35600

pull willcl-ark wants to merge 2 commits into bitcoin:master from willcl-ark:duplicate-connections-2 changing 4 files +164 −5
  1. willcl-ark commented at 11:37 AM on June 25, 2026: member

    Fixes #5299.

    -connect and -addnode can both try to open the same manual connection during startup. Existing duplicate checks only see peers after they have been inserted into m_nodes, so two concurrent manual dial attempts can both pass the checks and create duplicate connections.

    Fix this by reserving a manual connection destination before connecting in OpenNetworkConnection(). The reservation is cleared after a failed connection, or after a successful node has been added to m_nodes, where the existing connected-peer checks take over.

    This is intentionally limited to ConnectionType::MANUAL, covering -connect, -addnode, manual reconnections, and one-shot manual RPC connection attempts only. Note that this does not try to prevent automatic outbound connections to addnode peers like 21f8426c823fd83c40fd81f08ff2bf39ec6a4575 from #28428 does.

    A functional regression test is added for the startup case where two nodes each configure the other with both -connect and -addnode. This fails before the fixing commit is applied.

    In comparison to #27804 this approach tracks in-flight manual connection attempts in OpenNetworkConnection(), instead of de-duplicating configuration arguments, which should be much less-brittle. Once the connection succeeds, the existing m_nodes duplicate checks take over.

    The remaining limitation is that hostnames are keyed by the original string, not resolved IPs. So -connect=example.com and -addnode=<resolved-ip> are not guaranteed to dedupe during the in-flight window, but I don't want to add DNS lookups in here, so I consider this acceptable.

  2. net: reserve pending manual connections
    Manual -connect and -addnode callers can run concurrently during
    startup. Existing duplicate checks only see m_nodes after a successful
    connection attempt, so both threads can open sockets to the same target.
    
    Reserve the normalized manual destination while connecting and release
    it after failure or after publishing the node, so the existing
    connected-peer checks take over.
    
    This applies to all `MANUAL` connection attempts, regardless of network,
    but the keying differs by net type.
    
    - IPv4: by resolved `CService` including port.
    - IPv6: by resolved `CService` including port.
    - CJDNS: by numeric CJDNS IPv6-style addresses, via `MaybeFlipIPv6toCJDNS(...)`.
    - Tor: by v3 onion addresses.
    - I2P: by b32.i2p addresses.
    - Hostnames: keyed by the original destination string, not resolved IPs, because the guard intentionally avoids doing DNS just for the reservation.
    90c9f73fd2
  3. test: duplicate manual connections regression test
    Starting peers with the same address in both -connect and -addnode can
    create duplicate manual connection attempts during startup. Add a
    regression test to prevent this from being re-introduced.
    f80f522d80
  4. DrahtBot added the label P2P on Jun 25, 2026
  5. DrahtBot commented at 11:37 AM on June 25, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35600.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #32278 (doc: better document NetEventsInterface and the deletion of "CNode"s by vasild)
    • #28463 (p2p: Increase inbound capacity for block-relay only connections by mzumsande)

    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-->

    LLM Linter (✨ experimental)

    Possible typos and grammar issues:

    • addednodeinfo -> added node info [misspelled/merged words in the comment; the intended meaning is clearer with spaces]

    <sup>2026-06-25 11:37:30</sup>


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-06-27 09:50 UTC

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