Skip to content

Commit

Permalink
Merge pull request #961 from denravonska/change-process-lock-order
Browse files Browse the repository at this point in the history
Revert lock order from #708.
  • Loading branch information
denravonska committed Feb 23, 2018
2 parents ff7983b + f2b5691 commit 0da6da2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
28 changes: 15 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6596,7 +6596,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (vInv.size() > MAX_INV_SZ)
{
pfrom->Misbehaving(50);
printf("\r\n **Hacker tried to send inventory > MAX_INV_SZ **\r\n");
return error("message inv size() = %" PRIszu "", vInv.size());
}

Expand All @@ -6608,6 +6607,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
break;
}
}

LOCK(cs_main);
CTxDB txdb("r");
for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
{
Expand Down Expand Up @@ -6655,6 +6656,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (fDebug10) printf("received getdata (%" PRIszu " invsz)\n", vInv.size());
}

LOCK(cs_main);
for (auto const& inv : vInv)
{
if (fShutdown)
Expand Down Expand Up @@ -6723,6 +6725,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
uint256 hashStop;
vRecv >> locator >> hashStop;

LOCK(cs_main);

// Find the last block the caller has in the main chain
CBlockIndex* pindex = locator.GetBlockIndex();

Expand Down Expand Up @@ -6760,6 +6764,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
uint256 hashStop;
vRecv >> locator >> hashStop;

LOCK(cs_main);

CBlockIndex* pindex = NULL;
if (locator.IsNull())
{
Expand Down Expand Up @@ -6798,6 +6804,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CInv inv(MSG_TX, tx.GetHash());
pfrom->AddInventoryKnown(inv);

LOCK(cs_main);

bool fMissingInputs = false;
if (AcceptToMemoryPool(mempool, tx, &fMissingInputs))
{
Expand Down Expand Up @@ -6876,6 +6884,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CInv inv(MSG_BLOCK, hashBlock);
pfrom->AddInventoryKnown(inv);

LOCK(cs_main);

if (ProcessBlock(pfrom, &block, false))
{
mapAlreadyAskedFor.erase(inv);
Expand Down Expand Up @@ -6904,6 +6914,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,

else if (strCommand == "mempool")
{
LOCK(cs_main);

std::vector<uint256> vtxid;
mempool.queryHashes(vtxid);
vector<CInv> vInv;
Expand Down Expand Up @@ -7196,13 +7208,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,

// requires LOCK(cs_vRecvMsg)
bool ProcessMessages(CNode* pfrom)
{
LOCK(cs_main);

TRY_LOCK(pfrom->cs_vRecvMsg, lockRecv);
if (!lockRecv)
return true;

{
//
// Message format
// (4) message start
Expand Down Expand Up @@ -7270,7 +7276,7 @@ bool ProcessMessages(CNode* pfrom)
bool fRet = false;
try
{
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime);
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime);
if (fShutdown)
break;
}
Expand Down Expand Up @@ -7955,10 +7961,6 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
if(!lockMain)
return true;

TRY_LOCK(pto->cs_vSend, lockSend);
if (!lockSend)
return true;

// Don't send anything until we get their version message
if (pto->nVersion == 0)
return true;
Expand Down
15 changes: 12 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1984,14 +1984,23 @@ void ThreadMessageHandler2(void* parg)

//11-25-2015
// Receive messages
if (!ProcessMessages(pnode))
pnode->CloseSocketDisconnect();
{
TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
if (lockRecv)
if (!ProcessMessages(pnode))
pnode->CloseSocketDisconnect();
}

if (fShutdown)
return;

// Send messages
SendMessages(pnode, pnode == pnodeTrickle);
{
TRY_LOCK(pnode->cs_vSend, lockSend);
if (lockSend)
SendMessages(pnode, pnode == pnodeTrickle);
}

if (fShutdown)
return;
}
Expand Down

0 comments on commit 0da6da2

Please sign in to comment.