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

Open
willcl-ark wants to merge 2 commits from duplicate-connections-2 into master AGit
willcl-ark commented 2026-06-25 11:37:10 +00:00 (Migrated from github.com)

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.

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 commented 2026-06-25 11:37:17 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35600 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35600.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35600#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/35600#issuecomment-5069081203), [w0xlt](https://github.com/bitcoin/bitcoin/pull/35600#issuecomment-5333640801) | | Stale ACK | [8144225309](https://github.com/bitcoin/bitcoin/pull/35600#issuecomment-5974965553), [Bicaru20](https://github.com/bitcoin/bitcoin/pull/35600#issuecomment-6779830591) | 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 No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
sedited commented 2026-07-24 10:55:27 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
DrahtBot commented 2026-07-24 23:25:16 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
w0xlt commented 2026-08-18 20:24:55 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
8144225309 commented 2026-10-04 00:24:13 +00:00 (Migrated from github.com)

ACK 081ba2b98c

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.

ACK 081ba2b98c 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.
First-time contributor

Earlier concerns remain unresolved; see the Run archive linked from the Review report.

🔵 Suggestion · Equivalent hostname spellings get separate reservations

The documented raw-hostname scope leaves a pre-existing duplicate-dial case for names with omitted versus explicit default ports or different case, although normalizing those forms requires no DNS lookup.

Location: src/net.cpp:376 (head).

🔵 Suggestion · In-progress rejection discards queued manual fallbacks

Overlapping manual dials can discard a queued v1 fallback or return early from onetry; production impact is unconfirmed, and no in-tree caller relying on onetry completion was found.

Location: src/net.cpp:3187 (head).

🔵 Suggestion · Proxy and Future waits ignore the timeout factor

The new functional test uses unscaled ten-second socket and Future waits, making slow CI runs more timing-sensitive; no flaky run has been demonstrated.

Location: test/functional/p2p_duplicate_manual_connections.py:104 (head).

Review coverage was incomplete.


Reviewed: 120d45ecac6b · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:8fa46da13758fe55dfdc78f36e5156483169b9353c6046c9c84f4c16d0cba459:eaa37ea1cdada8fdd806ab11180fd9e0f01062422d9d8c5ea7dcfd80d5014b95 --> <!-- ralph-base:791173ae952bb07716531dd01a78d70dc3b7c038 --> <!-- ralph-head:120d45ecac6b5483ce0e1cee0396bc69a58b6f5d --> <!-- ralph-case:39ee3c5c7ae5cbb18a9371626d475ffb0a10e6007d02cd3ad57265b7631974cb --> Earlier concerns remain unresolved; see the Run archive linked from the Review report. **🔵 Suggestion · Equivalent hostname spellings get separate reservations** The documented raw-hostname scope leaves a pre-existing duplicate-dial case for names with omitted versus explicit default ports or different case, although normalizing those forms requires no DNS lookup. Location: [`src/net.cpp:376`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/120d45ecac6b5483ce0e1cee0396bc69a58b6f5d/src/net.cpp#L376>) (head). **🔵 Suggestion · In-progress rejection discards queued manual fallbacks** Overlapping manual dials can discard a queued v1 fallback or return early from `onetry`; production impact is unconfirmed, and no in-tree caller relying on `onetry` completion was found. Location: [`src/net.cpp:3187`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/120d45ecac6b5483ce0e1cee0396bc69a58b6f5d/src/net.cpp#L3187>) (head). **🔵 Suggestion · Proxy and Future waits ignore the timeout factor** The new functional test uses unscaled ten-second socket and Future waits, making slow CI runs more timing-sensitive; no flaky run has been demonstrated. Location: [`test/functional/p2p_duplicate_manual_connections.py:104`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/120d45ecac6b5483ce0e1cee0396bc69a58b6f5d/test/functional/p2p_duplicate_manual_connections.py#L104>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`120d45ecac6b`](<https://git.fish.foo/bitcoin/bitcoin/commit/120d45ecac6b5483ce0e1cee0396bc69a58b6f5d>) · [Review report](<https://review.fish.foo/report/35600-120d45ecac6b5483ce0e1cee0396bc69a58b6f5d-2588-3.html>) · [Previous reports](<https://review.fish.foo/traces/35600/>) [permalink](<https://review.fish.foo/report/35600-120d45ecac6b5483ce0e1cee0396bc69a58b6f5d-2588-3.html>)
maflcko (Migrated from github.com) reviewed 2026-10-04 08:56:57 +00:00
maflcko (Migrated from github.com) commented 2026-10-04 08:56:57 +00:00

ralph says to drop them, which seems right? #35600

ralph says to drop them, which seems right? https://git.fish.foo/bitcoin/bitcoin/pulls/35600#author-ralph
willcl-ark (Migrated from github.com) reviewed 2026-10-05 08:12:59 +00:00
willcl-ark (Migrated from github.com) commented 2026-10-05 08:12:59 +00:00

Good bot. Taken.

Good bot. Taken.
Bicaru20 commented 2026-11-10 21:33:08 +00:00 (Migrated from github.com)

ACK 590947d783

I think this also partially fixes https://github.com/bitcoin/bitcoin/issues/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.

ACK 590947d78300912f020bf3ef91371cc4222eaa65 I think this also partially fixes https://github.com/bitcoin/bitcoin/issues/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: ```bash ./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.
DrahtBot commented 2026-11-12 12:01:39 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
willcl-ark commented 2026-11-12 14:44:40 +00:00 (Migrated from github.com)

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!

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 in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35600
No description provided.