Skip to content

Commit

Permalink
Add a mutex lock to protect CNode::nRefCount (#1321)
Browse files Browse the repository at this point in the history
* Add a mutex lock to protect CNode::nRefCount

* Added logging statement for CNode removal
  • Loading branch information
tgflynn authored and UdjinM6 committed Feb 5, 2017
1 parent 5520bf6 commit 8de7922
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,9 @@ void ThreadSocketHandler()
if (pnode->fDisconnect ||
(pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
{
LogPrintf("ThreadSocketHandler -- removing node: peer=%d addr=%s nRefCount=%d fNetworkNode=%d fInbound=%d fMasternode=%d\n",
pnode->id, pnode->addr.ToString(), pnode->GetRefCount(), pnode->fNetworkNode, pnode->fInbound, pnode->fMasternode);

// remove from vNodes
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());

Expand Down
5 changes: 5 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ class CNode
static uint64_t nMaxOutboundLimit;
static uint64_t nMaxOutboundTimeframe;

CCriticalSection cs_nRefCount;

CNode(const CNode&);
void operator=(const CNode&);

Expand All @@ -447,6 +449,7 @@ class CNode

int GetRefCount()
{
LOCK(cs_nRefCount);
assert(nRefCount >= 0);
return nRefCount;
}
Expand All @@ -473,12 +476,14 @@ class CNode

CNode* AddRef()
{
LOCK(cs_nRefCount);
nRefCount++;
return this;
}

void Release()
{
LOCK(cs_nRefCount);
nRefCount--;
assert(nRefCount >= 0);
}
Expand Down

0 comments on commit 8de7922

Please sign in to comment.