Add a mutex lock to protect CNode::nRefCount (#1321)

* Add a mutex lock to protect CNode::nRefCount

* Added logging statement for CNode removal
This commit is contained in:
Tim Flynn 2017-02-05 11:45:36 -05:00 committed by UdjinM6
parent 5520bf6895
commit 8de792291a
2 changed files with 8 additions and 0 deletions

View File

@ -1050,6 +1050,9 @@ void ThreadSocketHandler()
if (pnode->fDisconnect || if (pnode->fDisconnect ||
(pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty())) (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 // remove from vNodes
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end()); vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());

View File

@ -436,6 +436,8 @@ private:
static uint64_t nMaxOutboundLimit; static uint64_t nMaxOutboundLimit;
static uint64_t nMaxOutboundTimeframe; static uint64_t nMaxOutboundTimeframe;
CCriticalSection cs_nRefCount;
CNode(const CNode&); CNode(const CNode&);
void operator=(const CNode&); void operator=(const CNode&);
@ -447,6 +449,7 @@ public:
int GetRefCount() int GetRefCount()
{ {
LOCK(cs_nRefCount);
assert(nRefCount >= 0); assert(nRefCount >= 0);
return nRefCount; return nRefCount;
} }
@ -473,12 +476,14 @@ public:
CNode* AddRef() CNode* AddRef()
{ {
LOCK(cs_nRefCount);
nRefCount++; nRefCount++;
return this; return this;
} }
void Release() void Release()
{ {
LOCK(cs_nRefCount);
nRefCount--; nRefCount--;
assert(nRefCount >= 0); assert(nRefCount >= 0);
} }