Skip to content

Commit 7a72a41

Browse files
committed
[net processing] Improve documentation for Peer destruction/locking
Summary: As per title, only comments. Partial backport of [[bitcoin/bitcoin#19829 | core#19829]]: bitcoin/bitcoin@717a374 Ref T1696. Test Plan: Read the comments. Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10865
1 parent 929af2d commit 7a72a41

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/net_processing.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ void PeerManager::FinalizeNode(const Config &config, const CNode &node,
11191119
LOCK(cs_main);
11201120
int misbehavior{0};
11211121
{
1122+
// We remove the PeerRef from g_peer_map here, but we don't always
1123+
// destruct the Peer. Sometimes another thread is still holding a
1124+
// PeerRef, so the refcount is >= 1. Be careful not to do any
1125+
// processing here that assumes Peer won't be changed before it's
1126+
// destructed.
11221127
PeerRef peer = RemovePeer(nodeid);
11231128
assert(peer != nullptr);
11241129
misbehavior = WITH_LOCK(peer->m_misbehavior_mutex,

src/net_processing.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ struct CNodeStateStats {
5656
* Memory is owned by shared pointers and this object is destructed when
5757
* the refcount drops to zero.
5858
*
59+
* Mutexes inside this struct must not be held when locking m_peer_mutex.
60+
*
5961
* TODO: move most members from CNodeState to this structure.
6062
* TODO: move remaining application-layer data members from CNode to this
6163
* structure.
@@ -295,7 +297,10 @@ class PeerManager final : public CValidationInterface,
295297
//! Next time to check for stale tip
296298
int64_t m_stale_tip_check_time;
297299

298-
/** Protects m_peer_map */
300+
/**
301+
* Protects m_peer_map. This mutex must not be locked while holding a lock
302+
* on any of the mutexes inside a Peer object.
303+
*/
299304
mutable Mutex m_peer_mutex;
300305
/**
301306
* Map of all Peer objects, keyed by peer id. This map is protected

0 commit comments

Comments
 (0)