Skip to content

Commit

Permalink
[net processing] Improve documentation for Peer destruction/locking
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewbery committed Dec 19, 2020
1 parent f1dbf92 commit 717a374
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
LOCK(cs_main);
int misbehavior{0};
{
// We remove the PeerRef from g_peer_map here, but we don't always
// destruct the Peer. Sometimes another thread is still holding a
// PeerRef, so the refcount is >= 1. Be careful not to do any
// processing here that assumes Peer won't be changed before it's
// destructed.
PeerRef peer = RemovePeer(nodeid);
assert(peer != nullptr);
misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
Expand Down
5 changes: 4 additions & 1 deletion src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ struct CNodeStateStats {
* Memory is owned by shared pointers and this object is destructed when
* the refcount drops to zero.
*
* Mutexes inside this struct must not be held when locking m_peer_mutex.
*
* TODO: move most members from CNodeState to this structure.
* TODO: move remaining application-layer data members from CNode to this structure.
*/
Expand Down Expand Up @@ -210,7 +212,8 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
* on extra block-relay-only peers. */
bool m_initial_sync_finished{false};

/** Protects m_peer_map */
/** Protects m_peer_map. This mutex must not be locked while holding a lock
* on any of the mutexes inside a Peer object. */
mutable Mutex m_peer_mutex;
/**
* Map of all Peer objects, keyed by peer id. This map is protected
Expand Down

0 comments on commit 717a374

Please sign in to comment.