Skip to content

Commit

Permalink
Add some linger time between fDisconnect=true and actually closing th…
Browse files Browse the repository at this point in the history
…e socket
  • Loading branch information
codablock committed Apr 17, 2020
1 parent d8bbdee commit 08b57c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,20 @@ void CConnman::DisconnectNodes()
CNode* pnode = *it;
if (pnode->fDisconnect)
{
if (pnode->nDisconnectLingerTime == 0) {
// let's not immediately close the socket but instead wait for at least 100ms so that there is a
// chance to flush all/some pending data. Otherwise the other side might not receive REJECT messages
// that were pushed right before setting fDisconnect=true
// Flushing must happen in two places to ensure data can be received by the other side:
// 1. vSendMsg must be empty and all messages sent via send(). This is ensured by SocketHandler()
// being called before DisconnectNodes and also by the linger time
// 2. Internal socket send buffers must be flushed. This is ensured solely by the linger time
pnode->nDisconnectLingerTime = GetTimeMillis() + 100;
continue;
} else if (GetTimeMillis() < pnode->nDisconnectLingerTime) {
continue;
}

if (fLogIPs) {
LogPrintf("ThreadSocketHandler -- removing node: peer=%d addr=%s nRefCount=%d fInbound=%d fMasternode=%d\n",
pnode->GetId(), pnode->addr.ToString(), pnode->GetRefCount(), pnode->fInbound, pnode->fMasternode);
Expand Down
1 change: 1 addition & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ class CNode
const bool fInbound;
std::atomic_bool fSuccessfullyConnected;
std::atomic_bool fDisconnect;
std::atomic<int64_t> nDisconnectLingerTime{0};
// We use fRelayTxes for two purposes -
// a) it allows us to not relay tx invs before receiving the peer's version message
// b) the peer may tell us in its version message that we should not relay tx invs
Expand Down

0 comments on commit 08b57c1

Please sign in to comment.