Skip to content

Commit 8de7922

Browse files
tgflynnUdjinM6
authored andcommitted
Add a mutex lock to protect CNode::nRefCount (#1321)
* Add a mutex lock to protect CNode::nRefCount * Added logging statement for CNode removal
1 parent 5520bf6 commit 8de7922

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/net.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,9 @@ void ThreadSocketHandler()
10501050
if (pnode->fDisconnect ||
10511051
(pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
10521052
{
1053+
LogPrintf("ThreadSocketHandler -- removing node: peer=%d addr=%s nRefCount=%d fNetworkNode=%d fInbound=%d fMasternode=%d\n",
1054+
pnode->id, pnode->addr.ToString(), pnode->GetRefCount(), pnode->fNetworkNode, pnode->fInbound, pnode->fMasternode);
1055+
10531056
// remove from vNodes
10541057
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
10551058

src/net.h

+5
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ class CNode
436436
static uint64_t nMaxOutboundLimit;
437437
static uint64_t nMaxOutboundTimeframe;
438438

439+
CCriticalSection cs_nRefCount;
440+
439441
CNode(const CNode&);
440442
void operator=(const CNode&);
441443

@@ -447,6 +449,7 @@ class CNode
447449

448450
int GetRefCount()
449451
{
452+
LOCK(cs_nRefCount);
450453
assert(nRefCount >= 0);
451454
return nRefCount;
452455
}
@@ -473,12 +476,14 @@ class CNode
473476

474477
CNode* AddRef()
475478
{
479+
LOCK(cs_nRefCount);
476480
nRefCount++;
477481
return this;
478482
}
479483

480484
void Release()
481485
{
486+
LOCK(cs_nRefCount);
482487
nRefCount--;
483488
assert(nRefCount >= 0);
484489
}

0 commit comments

Comments
 (0)