Skip to content

Commit

Permalink
AddRef/Release when adding/erasing CNode* entries to/from mapNodesWit…
Browse files Browse the repository at this point in the history
…hDataToSend
  • Loading branch information
codablock committed Apr 17, 2020
1 parent a0832e3 commit 43c67fe
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,10 @@ void CNode::CloseSocketDisconnect(CConnman* connman)
connman->mapSendableNodes.erase(GetId());
{
LOCK(connman->cs_mapNodesWithDataToSend);
connman->mapNodesWithDataToSend.erase(GetId());
if (connman->mapNodesWithDataToSend.erase(GetId()) != 0) {
// See comment in PushMessage
Release();
}
}

LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
Expand Down Expand Up @@ -1679,6 +1682,8 @@ void CConnman::SocketHandler()
vSendableNodes.reserve(mapNodesWithDataToSend.size());
for (auto it = mapNodesWithDataToSend.begin(); it != mapNodesWithDataToSend.end(); ) {
if (it->second->nSendMsgSize == 0) {
// See comment in PushMessage
it->second->Release();
it = mapNodesWithDataToSend.erase(it);
} else {
if (it->second->fCanSendData) {
Expand Down Expand Up @@ -3610,6 +3615,11 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)

{
LOCK(cs_mapNodesWithDataToSend);
// we're not holding cs_vNodes here, so there is a chance of this node being disconnected shortly before
// we get here. Whoever called PushMessage still has a ref to CNode*, but will later Release() it, so we
// might end up having an entry in mapNodesWithDataToSend that is not in vNodes anymore. We need to
// Add/Release refs when adding/erasing mapNodesWithDataToSend.
pnode->AddRef();
mapNodesWithDataToSend.emplace(pnode->GetId(), pnode);
}

Expand Down

0 comments on commit 43c67fe

Please sign in to comment.