Don't return nodes with fDisconnect=true in FindNode

FindNode is only interested in active connections, especially when called
from OpenNetworkConnection. Connections which are about to get disconnected
and removed should be treated as if they are not existent anymore, as
otherwise there is a small race between disconnecting and reconnecting
nodes, causing OpenNetworkConnection to return early.
This commit is contained in:
Alexander Block 2020-04-21 18:21:19 +02:00
parent d235364534
commit 33bfaffbea
2 changed files with 20 additions and 8 deletions

View File

@ -323,10 +323,13 @@ bool IsReachable(const CNetAddr& addr)
} }
CNode* CConnman::FindNode(const CNetAddr& ip) CNode* CConnman::FindNode(const CNetAddr& ip, bool fExcludeDisconnecting)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) { for (CNode* pnode : vNodes) {
if (fExcludeDisconnecting && pnode->fDisconnect) {
continue;
}
if ((CNetAddr)pnode->addr == ip) { if ((CNetAddr)pnode->addr == ip) {
return pnode; return pnode;
} }
@ -334,10 +337,13 @@ CNode* CConnman::FindNode(const CNetAddr& ip)
return nullptr; return nullptr;
} }
CNode* CConnman::FindNode(const CSubNet& subNet) CNode* CConnman::FindNode(const CSubNet& subNet, bool fExcludeDisconnecting)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) { for (CNode* pnode : vNodes) {
if (fExcludeDisconnecting && pnode->fDisconnect) {
continue;
}
if (subNet.Match((CNetAddr)pnode->addr)) { if (subNet.Match((CNetAddr)pnode->addr)) {
return pnode; return pnode;
} }
@ -345,10 +351,13 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
return nullptr; return nullptr;
} }
CNode* CConnman::FindNode(const std::string& addrName) CNode* CConnman::FindNode(const std::string& addrName, bool fExcludeDisconnecting)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) { for (CNode* pnode : vNodes) {
if (fExcludeDisconnecting && pnode->fDisconnect) {
continue;
}
if (pnode->GetAddrName() == addrName) { if (pnode->GetAddrName() == addrName) {
return pnode; return pnode;
} }
@ -356,10 +365,13 @@ CNode* CConnman::FindNode(const std::string& addrName)
return nullptr; return nullptr;
} }
CNode* CConnman::FindNode(const CService& addr) CNode* CConnman::FindNode(const CService& addr, bool fExcludeDisconnecting)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) { for (CNode* pnode : vNodes) {
if (fExcludeDisconnecting && pnode->fDisconnect) {
continue;
}
if ((CService)pnode->addr == addr) { if ((CService)pnode->addr == addr) {
return pnode; return pnode;
} }

View File

@ -497,10 +497,10 @@ private:
uint64_t CalculateKeyedNetGroup(const CAddress& ad) const; uint64_t CalculateKeyedNetGroup(const CAddress& ad) const;
CNode* FindNode(const CNetAddr& ip); CNode* FindNode(const CNetAddr& ip, bool fExcludeDisconnecting = true);
CNode* FindNode(const CSubNet& subNet); CNode* FindNode(const CSubNet& subNet, bool fExcludeDisconnecting = true);
CNode* FindNode(const std::string& addrName); CNode* FindNode(const std::string& addrName, bool fExcludeDisconnecting = true);
CNode* FindNode(const CService& addr); CNode* FindNode(const CService& addr, bool fExcludeDisconnecting = true);
bool AttemptToEvictConnection(); bool AttemptToEvictConnection();
CNode* ConnectNode(CAddress addrConnect, const char *pszDest = nullptr, bool fCountFailure = false); CNode* ConnectNode(CAddress addrConnect, const char *pszDest = nullptr, bool fCountFailure = false);