Skip to content

Commit 717a374

Browse files
committed
[net processing] Improve documentation for Peer destruction/locking
Suggested here: - #19607 (comment) - #19829 (comment)
1 parent f1dbf92 commit 717a374

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/net_processing.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,11 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
791791
LOCK(cs_main);
792792
int misbehavior{0};
793793
{
794+
// We remove the PeerRef from g_peer_map here, but we don't always
795+
// destruct the Peer. Sometimes another thread is still holding a
796+
// PeerRef, so the refcount is >= 1. Be careful not to do any
797+
// processing here that assumes Peer won't be changed before it's
798+
// destructed.
794799
PeerRef peer = RemovePeer(nodeid);
795800
assert(peer != nullptr);
796801
misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);

src/net_processing.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct CNodeStateStats {
4646
* Memory is owned by shared pointers and this object is destructed when
4747
* the refcount drops to zero.
4848
*
49+
* Mutexes inside this struct must not be held when locking m_peer_mutex.
50+
*
4951
* TODO: move most members from CNodeState to this structure.
5052
* TODO: move remaining application-layer data members from CNode to this structure.
5153
*/
@@ -210,7 +212,8 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
210212
* on extra block-relay-only peers. */
211213
bool m_initial_sync_finished{false};
212214

213-
/** Protects m_peer_map */
215+
/** Protects m_peer_map. This mutex must not be locked while holding a lock
216+
* on any of the mutexes inside a Peer object. */
214217
mutable Mutex m_peer_mutex;
215218
/**
216219
* Map of all Peer objects, keyed by peer id. This map is protected

0 commit comments

Comments
 (0)