Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34824. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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 places where named args for integral literals may be used (e.g.
2026-09-15 20:05:05 |
sedited
left a comment
There was a problem hiding this comment.
Concept ACK
I looked this over and also didn't find any cases where locking recursion would be required, but the change seems brittle to me. Not sure if just changing these is the best way to handle this.
ed2a83c to
8ed9eb0
Compare
|
Thanks @sedited for the feedback. The original PR intentionally included only the first commit to avoid non-trivial refactoring and focus on the A broader refactor seems justified: both
|
hebasto
left a comment
There was a problem hiding this comment.
Replace
m_bloom_filter_mutexandm_tx_inventory_mutexinPeer::TxRelayfromRecursiveMutextoMutex.
Concept ACK.
|
Concept ACK.
See also the (Note that unless |
(also note that we currently use Clang 22.1.2 in the CI) |
sedited
left a comment
There was a problem hiding this comment.
Approach ACK
I think I prefer this over a bare bones mutex type change. The changes in the first four commits are pretty straight forward and make sense to me. The last commit is a bit more involved, but git color coding the moved bits does help a bit. I'd be curious to see what others think.
8ed9eb0 to
475366b
Compare
|
|
475366b to
67527c7
Compare
sedited
left a comment
There was a problem hiding this comment.
re-ACK 67527c70975e1c3bb4918ba0ebeb4313d8c741be
pablomartin4btc
left a comment
There was a problem hiding this comment.
Concept ACK. The first commits look straightforward (as said earlier), and commit 5 seems to preserve the existing lock order while moving the remaining TxRelay state access in SendMessages() behind annotated helpers.
Non-blocking/ follow-up idea: after this PR, it may be worth considering a small refactor of the tx-relay section of SendMessages(). The code could maybe be split into a few conceptual helpers for periodic relay scheduling, BIP35 mempool responses, and regular tx-inventory relay selection. I would not suggest perhaps one helper per comment/ filter condition, but grouping those areas into conceptual helpers could make SendMessages() cleaner and easier to follow, while keeping the change behaviour-preserving.
There was a problem hiding this comment.
ACK 67527c7.
Checked both m_bloom_filter_mutex and m_tx_inventory_mutex in Peer::TxRelay aren't acquired recursively, makes sense to move them into non-recursive mutexes (the 18 lock sites mentioned in the description are enumerated in the first commit body for reference).
The PR introduces ACQUIRED_BEFORE(m_tx_inventory_mutex) on m_bloom_filter_mutex (and ACQUIRED_AFTER on the other side) - as suggested. This establishes a lock ordering that didn't exist before, that ordering annotation becomes load-bearing for deadlock prevention, not just documentation.
Also, reviewed the replacement of direct member access to TxRelay internals with accessor methods. Members are now private — the only access path is through accessors that either self-lock (negative capability !mutex) or require the caller to hold the lock, with Clang TSA enforcing both at compile time.
| @@ -925,7 +925,7 @@ class PeerManagerImpl final : public PeerManager | |||
|
|
|||
| /** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */ | |||
| CTransactionRef FindTxForGetData(const Peer::TxRelay& tx_relay, const GenTxid& gtxid) | |||
| EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, !tx_relay.m_tx_inventory_mutex); | |||
| EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, !tx_relay.GetTxInventoryMutex()); | |||
There was a problem hiding this comment.
05b3bbcb0e2b7d66a8107c26b837cb68f4eb5dc0: idk, what is the point of this pull request?
Just changing a RecursiveMutex to a Mutex without proper TSA doesn't make sense, imo. This just risks unsafe code down the line in the future.
This pull is now moving locking to happen in different files/classes, and still exposes the mutex field globally, just like before.
I don't see how this makes anything safer/easier.
Either, this should be done properly or not at all. I know this isn't easy, which is probably why it hasn't been done for such a long time, but that shouldn't be an excuse to do it improperly.
There was a problem hiding this comment.
Updated. Direct TxRelay data access is gone and state changes now go through TSA-annotated helpers. The remaining mutex accessors are kept for the existing SendMessages() batching/lock-order annotations.
| { | ||
| LOCK(m_bloom_filter_mutex); | ||
| if (!m_bloom_filter) return false; | ||
| callable(*m_bloom_filter); |
There was a problem hiding this comment.
05b3bbcb0e2b7d66a8107c26b837cb68f4eb5dc0: Is this pattern even safe?
Does clang TSA detect if callable were to lock the mutex and thus lead to UB?
There was a problem hiding this comment.
Done. Removed this callback pattern. The filtered-block path now uses TxRelay::MakeMerkleBlock() instead.
There was a problem hiding this comment.
I think the pattern could work, but only if the mutex getter is not defined and only exists to provide compile-time annotations: #19303 (comment)
There was a problem hiding this comment.
Thanks, that makes sense.
The current version no longer has the public mutex getter or the callback-under-lock pattern.
The filtered block path now uses TxRelay::MakeMerkleBlock(), and the mutexes are not exposed, so I think this concern is now addressed.
67527c7 to
dab04e0
Compare
|
Update after the latest changes: The specific callback/TSA concern raised above has been addressed: The broader goal here is not just “remove
There is still some intentional mutex exposure through I think keeping that structure avoids a larger behavioral refactor in this PR, while still making the existing locking more explicit and reviewable. This should make future follow-ups easier, since |
|
Regarding the new It covers bloom-filter lifecycle, merkle-block creation, known-inventory handling, queued announcement drain/erase paths, BIP35 mempool one-shot handling, fee-filter, and sequence bookkeeping while accounting for rolling-bloom false positives. |
Yeah, and those aren't check with Clang TSA, are they? I am just not convinced that moving to a non-recursive mutex is useful, when it is less safe and requires manual review to avoid UB. All of the changes you list have nothing to do with the mutex change. It is possible to do all of the changes independently while keeping the recursive mutex. Some of the changes are required to make the mutex non-recursive, but I don't think they are sufficient. IIUC making the data and the mutex private (and not exposing references to them) are required changes. However, by exposing references to them in a public getter, those changes are basically undone. I know this pattern may not have been followed in the past (in #19303 related pulls and elsewhere), but I think there is value in first defining a set of possible safe code patterns, and then apply them consistently and consequently. IIUC the pattern is that non-recursive mutexes can be used for fully private fields and that recursive mutexes should be used for globals/public fields? If so, then I think mixing the patterns does not make sense. |
dab04e0 to
73d972b
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
73d972b to
d88df07
Compare
|
Thanks for the feedback. The concern is valid. The original goal of this PR was fairly small, just replacing those variables with Mutex, and I was trying to keep the change scoped and avoid a larger To address the mutex exposure issue, I reworked the branch so that encapsulation comes first, with the mutex replacement as the final step. The public mutex accessors have been removed, the guarded state is now private, and The I updated the PR title and description to better reflect the current scope. The PR has evolved beyond a simple mutex replacement and now introduces a new networking component. |
|
Unrelated CI error |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
I think there is a silent merge conflict here. |
5a34f55 to
e8dbc6e
Compare
|
Rebased. Thanks |
pablomartin4btc
left a comment
There was a problem hiding this comment.
re-ACK e8dbc6e
Since my last ACK: WithBloomFilterIfSet() and the public mutex getter were removed - addressing maflcko's concerns that exposing the mutex reference undoes the TSA safety guarantees. State changes now go exclusively through EXCLUSIVE_LOCKS_REQUIRED(!mutex) annotated helpers, and MakeMerkleBlock() replaces the callable-under-lock pattern.
Move the per-peer transaction relay state and helper methods out of net_processing.cpp into a new node::TxRelay type. Keep the existing locking model and behavior intact while giving the transaction relay code a single home. Net processing still owns the relay flow. This commit only moves the state, bloom-filter helpers, and inventory helpers so later commits can tighten the interface without mixing in behavior changes.
Move the remaining SendMessages transaction-inventory access behind TxRelay methods. StartTxInventoryBatch() snapshots queued inventory while holding the TxRelay mutex, lets SendMessages drain the batch without holding that mutex, and ReturnTxInventory() merges unsent entries back into the live queue. This removes the remaining direct TxRelay state access from net_processing.cpp and makes the guarded TxRelay data private without changing intended relay behavior. Document the caller serialization required across relay scheduling and batch processing, provided by g_msgproc_mutex in SendMessages().
Make TxInventoryBatch move-only and hide its backing vector from net_processing.cpp. Expose semantic operations for batch state, queued candidate snapshots, draining, clearing, and erasure. ReturnTxInventory() takes the batch object itself. Candidate snapshots and drains return owned vectors without exposing references to batch storage.
Add focused unit coverage for node::TxRelay's public behavior: bloom-filter lifecycle, inventory gating before the first send schedule, known-inventory deduplication, state accessors, BIP37 merkle block creation, and the snapshot/return inventory-batch API (including conservation across repeated drain rounds and under concurrent PushInventory()). These tests exercise TxRelay entirely through its public interface and provide guardrails for the private-state and locking changes.
Add a fuzz target that exercises TxRelay's public operations in random order: relay flag changes, bloom-filter updates, known transaction inserts, send scheduling, inventory queueing, fee-filter state, and the snapshot/return inventory-batch API. Seed the test random context because CRollingBloomFilter uses global random state. This keeps the target deterministic for a given fuzz input.
Replace TxRelay's RecursiveMutex members with plain Mutex now that callers no longer take those locks directly. Annotate the bloom-filter mutex as acquired after the inventory mutex, matching the only nested locking path in StartTxInventoryBatch(). Require callers of inventory-locking methods to hold neither mutex so thread-safety analysis also rejects reversed lock order through helper calls, while retaining the checks against recursive locking.
e8dbc6e to
4ac05f0
Compare
|
Rebased. |
Part of #19303.
This PR refactors per-peer transaction relay state so
TxRelayowns its guarded data and lock boundaries before replacing itsRecursiveMutexmembers with plainMutex.Before this change,
Peer::TxRelaywas mostly a collection of public state insidenet_processing.cpp.Callers directly locked its mutexes, inspected its fields, and mutated its containers.
That made the mutex type change hard to review: replacing
RecursiveMutexwithMutexis only clearly safe if outside code cannot accidentally recurse, take locks in the wrong order, or bypass the intended state transitions.The main changes are:
Peer::TxRelayintonode::TxRelay.TxRelaymethods.TxRelayfield and mutex access fromnet_processing.cpp.TxInventoryBatchinSendMessages(), process it without holding theTxRelaymutex, and merge unsent entries back afterward.TxRelaybehavior.RecursiveMutexmembers withMutexand annotate the inventory-before-bloom lock order.No P2P behavior change is intended. The purpose is to move from "external code locks and mutates TxRelay internals" to "TxRelay owns its state transitions and locking contract."
The most important commit to review is the
SendMessages()batch snapshot refactor, because that is where the lock ownership model changes. The final mutex replacement is intentionally small after the state has been encapsulated.