Skip to content

Conversation

vasild
Copy link
Contributor

@vasild vasild commented May 1, 2025

The only case of a recursive lock was a nested ForNode() call to trim
the size of lNodesAnnouncingHeaderAndIDs to <= 3. This need not be
nested, so take it out.

Before:

// we know we will push one new element at the back
if (size >= 3) pop from front
push at the back

After:

// maybe push at the back
if (size > 3) pop from the front

lNodesAnnouncingHeaderAndIDs is protected by cs_main which is locked
during the entire operation.

Partially resolves: #19303


This PR includes #32326 (first 3 commits in this PR). If that PR gets merged first, then the size of this will be reduced. If this gets merged first then that can be closed (will be fully merged via this one).

@DrahtBot
Copy link
Contributor

DrahtBot commented May 1, 2025

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

Code Coverage & Benchmarks

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

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK hebasto

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #32983 (rpc: refactor: use string_view in Arg/MaybeArg by stickies-v)
  • #32326 (net: improve the interface around FindNode() and avoid a recursive mutex lock by vasild)
  • #32278 (doc: better document NetEventsInterface and the deletion of "CNode"s by vasild)
  • #32065 (i2p: make a time gap between creating transient sessions and using them by vasild)
  • #29641 (scripted-diff: Use LogInfo over LogPrintf [WIP, NOMERGE, DRAFT] by maflcko)
  • #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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • Search for a "preferred" network, a reachable network to which we -> * Search for a "preferred" network, a reachable network to which we can connect. [incomplete sentence]

Bonobirho99

This comment was marked as spam.

@hebasto
Copy link
Member

hebasto commented May 1, 2025

Partially resolves: #19303

Concept ACK on that.

vasild added 4 commits May 21, 2025 13:26
`CConnman::AlreadyConnectedToAddress()` is the only caller of
`CConnman::FindNode(CNetAddr)`, so merge the two in one function and
rename it to `IsConnectedToAddr()` to show that it compares just the
address and not the port.

The unit test that checked whether `AlreadyConnectedToAddress()` ignores
the port is now unnecessary because now the function takes a `CNetAddr`
argument. It has no access to the port.
Have `CConnman::DisconnectNode()` iterate `m_nodes` itself instead of
using `FindNode()`. This avoids recursive mutex lock and drops the only
caller of `FindNode()` which used the return value for something else
than a boolean found/notfound.
All callers of `CConnman::FindNode()` use its return value `CNode*` only
as a boolean null/notnull. So change that method to return `bool`.

This removes the dangerous pattern of handling a `CNode` object (the
return value of `FindNode()`) without holding `CConnman::m_nodes_mutex`
and without having that object's reference count incremented for the
duration of the usage.

Also rename the method to better describe what it does.
The only case of a recursive lock was a nested `ForNode()` call to trim
the size of `lNodesAnnouncingHeaderAndIDs` to `<= 3`. This need not be
nested, so take it out.

Before:
```
// we know we will push one new element at the back
if (size >= 3) pop from front
push at the back
```

After:
```
// maybe push at the back
if (size > 3) pop from the front
```

`lNodesAnnouncingHeaderAndIDs` is protected by `cs_main` which is locked
during the entire operation.

Partially resolves: bitcoin#19303
@vasild
Copy link
Contributor Author

vasild commented May 21, 2025

98bba932bf...22d4c57a99: rebase due to conflicts

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.

Replace all of the RecursiveMutex instances with the Mutex ones
4 participants