Skip to content

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

Open
willcl-ark wants to merge 2 commits into
bitcoin:masterfrom
willcl-ark:duplicate-connections-2
Open

willcl-ark wants to merge 2 commits into
bitcoin:masterfrom
willcl-ark:duplicate-connections-2

Conversation

@willcl-ark

@willcl-ark willcl-ark commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #5299.

-connect and -addnode can attempt the same manual connection concurrently during startup. The existing duplicate checks cannot see a peer until it has been added to m_nodes, so both attempts can pass and open connections.

Reserve the destination in OpenNetworkConnection() before checking connected peers or starting the connection. The reservation stays in place until the attempt fails or the new peer has been added to m_nodes.

This applies to ConnectionType::MANUAL, including -connect, -addnode, manual reconnections, and addnode onetry. Automatic outbound connections are outside this change.

The functional test checks the original two-node startup case. It also holds a manual connection in a local proxy handshake and verifies that an overlapping attempt is rejected, that a failed attempt releases its reservation, and that equivalent numeric addresses share a reservation.

Unlike #27804, this handles attempts in the connection manager rather than deduplicating configuration arguments. Hostnames use their original string as the reservation key, so different names that resolve to the same endpoint may still overlap. This avoids an extra DNS lookup.

@DrahtBot DrahtBot added the P2P label Jun 25, 2026
@DrahtBot

DrahtBot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Type Reviewers
Concept ACK sedited, w0xlt
Stale ACK 8144225309, Bicaru20

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

Conflicts

No conflicts as of last run.

@sedited

sedited commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@w0xlt

w0xlt commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@8144225309

Copy link
Copy Markdown
Contributor

ACK 081ba2b

Built and ran the new test, the functional suite and the net unit tests. The new test fails with the duplicate check disabled, and also when the key normalization or the release after a failed attempt is broken.

The test still passes if the reservation covers every connection type (net.cpp:3139). An addconnection to the same destination while a manual attempt is in progress would catch that. Non-blocking.

@DrahtBot
DrahtBot requested a review from sedited October 4, 2026 00:24
Comment thread src/net.cpp Resolved
@Bicaru20

Copy link
Copy Markdown
Contributor

ACK 590947d

I think this also partially fixes #22559. In there, there is another bug that it is related and I think it may make sense to address it in this PR. I left my suggestions in the comments of the issue so feel free to check it out or ignore it if you think it deserve its own pr.

That being said, I tested the code by passing the test and also in I2P. In I2P it is very easy to reproduce this bug. You can do so by connecting to the same address multiple times at the same time:

./build/bin/bitcoin-cli addnode "$ADDR" onetry &
./build/bin/bitcoin-cli addnode "$ADDR" onetry &
wait

With this you can create duplicated connections to your node (as many as you want in theory, I've just tried with 3 duplicated connections).

If you run the comand above after the fix of this pr, the duplicates connections are gone.

Concurrent -connect and -addnode attempts could pass connected-peer checks
before either published a node. Reserve a normalized destination until the
dial fails or its node enters m_nodes. Check connected peers while holding
the reservation, so proxied hostnames cannot use a stale precheck.

Keep the reservation scoped to this call and leave the addnode scheduler
unchanged. Numeric destinations include their port; unresolved hostnames
use the original string without an extra DNS lookup.
The startup topology alone can pass without exercising pending-attempt
exclusion. Hold one manual connection in a local proxy handshake and require a
second attempt to be rejected while the first is pending.
@willcl-ark

Copy link
Copy Markdown
Member Author

Thanks for reviewing @Bicaru20.

I agree that this PR only partially fixes #22559, but I think in this case I'm going to leave the fix for that up to a separate PR as IMO the nonce tie-breaker probably wants some decent independent scrutiny.

I'd encourage you (or anyone) to perhaps followup this PR with a complete fix for it though!

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.

Initial connection management

7 participants