Skip to content

Commit f95fb79

Browse files
committed
Merge bitcoin#28521: net, net_processing: additional and consistent disconnect logging
06443b8 net: clarify if we ever sent or received from peer (Sjors Provoost) 1d01ad4 net: add LogIP() helper, use in net_processing (Sjors Provoost) 937ef9e net_processing: use CNode::DisconnectMsg helper (Sjors Provoost) ad22442 net: additional disconnection logging (Sjors Provoost) Pull request description: While debugging unexpected disconnections, possibly related to bitcoin#28331, I found some additional [net] logging to be useful. All cases where we disconnect now come with a log message that has the word `disconnecting`: * all calls to `CloseSocketDisconnect()` log `disconnecting peer=…` * wherever we set `pnode->fDisconnect = true;` * for all `InactivityCheck` cases (which in turn sets `fDisconnect`) * replaces "dropping" with "disconnecting" in `Network not active, dropping peer=…` A few exceptions are listed here: bitcoin#28521 (comment) I changed `CloseSocketDisconnect()` to no longer log `disconnecting`, and instead have all the call sites do so. This PR introduces two helper functions on `CNode`: `DisconnectMsg` and `LogIP`. The second and third commit use these helpers in `net_processing.cpp` so these disconnect messages are more consistent now (e.g. some didn't log the IP). No new messages are added there though. The `LogIP()` helper is rarely used outside of a disconnect event, but it's available for future use. Any `LogPrint` this PR touches is replaced with `LogDebug` (superseded by bitcoin#30750), and every `LogPrintf ` with `LogInfo`. ACKs for top commit: davidgumberg: reACK bitcoin@06443b8 vasild: ACK 06443b8 danielabrozzoni: ACK 06443b8 hodlinator: ACK 06443b8 Tree-SHA512: 525f4c11568616e1d48455a3fcab9e923da7432377fe9230468c15403d2e9b7ce712112df8fbd547cfec01dce0d1f26107cfc1b90f78cfc1fe13e08d57b08464
2 parents fc7b214 + 06443b8 commit f95fb79

File tree

9 files changed

+123
-75
lines changed

9 files changed

+123
-75
lines changed

src/net.cpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,6 @@ void CNode::CloseSocketDisconnect()
558558
fDisconnect = true;
559559
LOCK(m_sock_mutex);
560560
if (m_sock) {
561-
LogDebug(BCLog::NET, "disconnecting peer=%d\n", id);
562561
m_sock.reset();
563562
}
564563
m_i2p_sam_session.reset();
@@ -696,6 +695,18 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
696695
return true;
697696
}
698697

698+
std::string CNode::LogIP(bool log_ip) const
699+
{
700+
return log_ip ? strprintf(" peeraddr=%s", addr.ToStringAddrPort()) : "";
701+
}
702+
703+
std::string CNode::DisconnectMsg(bool log_ip) const
704+
{
705+
return strprintf("disconnecting peer=%d%s",
706+
GetId(),
707+
LogIP(log_ip));
708+
}
709+
699710
V1Transport::V1Transport(const NodeId node_id) noexcept
700711
: m_magic_bytes{Params().MessageStart()}, m_node_id{node_id}
701712
{
@@ -1635,7 +1646,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
16351646
// error
16361647
int nErr = WSAGetLastError();
16371648
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) {
1638-
LogDebug(BCLog::NET, "socket send error for peer=%d: %s\n", node.GetId(), NetworkErrorString(nErr));
1649+
LogDebug(BCLog::NET, "socket send error, %s: %s\n", node.DisconnectMsg(fLogIPs), NetworkErrorString(nErr));
16391650
node.CloseSocketDisconnect();
16401651
}
16411652
}
@@ -1879,7 +1890,7 @@ void CConnman::DisconnectNodes()
18791890
// Disconnect any connected nodes
18801891
for (CNode* pnode : m_nodes) {
18811892
if (!pnode->fDisconnect) {
1882-
LogDebug(BCLog::NET, "Network not active, dropping peer=%d\n", pnode->GetId());
1893+
LogDebug(BCLog::NET, "Network not active, %s\n", pnode->DisconnectMsg(fLogIPs));
18831894
pnode->fDisconnect = true;
18841895
}
18851896
}
@@ -1971,26 +1982,43 @@ bool CConnman::InactivityCheck(const CNode& node) const
19711982

19721983
if (!ShouldRunInactivityChecks(node, now)) return false;
19731984

1974-
if (last_recv.count() == 0 || last_send.count() == 0) {
1975-
LogDebug(BCLog::NET, "socket no message in first %i seconds, %d %d peer=%d\n", count_seconds(m_peer_connect_timeout), last_recv.count() != 0, last_send.count() != 0, node.GetId());
1985+
bool has_received{last_recv.count() != 0};
1986+
bool has_sent{last_send.count() != 0};
1987+
1988+
if (!has_received || !has_sent) {
1989+
std::string has_never;
1990+
if (!has_received) has_never += ", never received from peer";
1991+
if (!has_sent) has_never += ", never sent to peer";
1992+
LogDebug(BCLog::NET,
1993+
"socket no message in first %i seconds%s, %s\n",
1994+
count_seconds(m_peer_connect_timeout),
1995+
has_never,
1996+
node.DisconnectMsg(fLogIPs)
1997+
);
19761998
return true;
19771999
}
19782000

19792001
if (now > last_send + TIMEOUT_INTERVAL) {
1980-
LogDebug(BCLog::NET, "socket sending timeout: %is peer=%d\n", count_seconds(now - last_send), node.GetId());
2002+
LogDebug(BCLog::NET,
2003+
"socket sending timeout: %is, %s\n", count_seconds(now - last_send),
2004+
node.DisconnectMsg(fLogIPs)
2005+
);
19812006
return true;
19822007
}
19832008

19842009
if (now > last_recv + TIMEOUT_INTERVAL) {
1985-
LogDebug(BCLog::NET, "socket receive timeout: %is peer=%d\n", count_seconds(now - last_recv), node.GetId());
2010+
LogDebug(BCLog::NET,
2011+
"socket receive timeout: %is, %s\n", count_seconds(now - last_recv),
2012+
node.DisconnectMsg(fLogIPs)
2013+
);
19862014
return true;
19872015
}
19882016

19892017
if (!node.fSuccessfullyConnected) {
19902018
if (node.m_transport->GetInfo().transport_type == TransportProtocolType::DETECTING) {
1991-
LogDebug(BCLog::NET, "V2 handshake timeout peer=%d\n", node.GetId());
2019+
LogDebug(BCLog::NET, "V2 handshake timeout, %s\n", node.DisconnectMsg(fLogIPs));
19922020
} else {
1993-
LogDebug(BCLog::NET, "version handshake timeout peer=%d\n", node.GetId());
2021+
LogDebug(BCLog::NET, "version handshake timeout, %s\n", node.DisconnectMsg(fLogIPs));
19942022
}
19952023
return true;
19962024
}
@@ -2118,6 +2146,10 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
21182146
{
21192147
bool notify = false;
21202148
if (!pnode->ReceiveMsgBytes({pchBuf, (size_t)nBytes}, notify)) {
2149+
LogDebug(BCLog::NET,
2150+
"receiving message bytes failed, %s\n",
2151+
pnode->DisconnectMsg(fLogIPs)
2152+
);
21212153
pnode->CloseSocketDisconnect();
21222154
}
21232155
RecordBytesRecv(nBytes);
@@ -2130,7 +2162,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
21302162
{
21312163
// socket closed gracefully
21322164
if (!pnode->fDisconnect) {
2133-
LogDebug(BCLog::NET, "socket closed for peer=%d\n", pnode->GetId());
2165+
LogDebug(BCLog::NET, "socket closed, %s\n", pnode->DisconnectMsg(fLogIPs));
21342166
}
21352167
pnode->CloseSocketDisconnect();
21362168
}
@@ -2141,7 +2173,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
21412173
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
21422174
{
21432175
if (!pnode->fDisconnect) {
2144-
LogDebug(BCLog::NET, "socket recv error for peer=%d: %s\n", pnode->GetId(), NetworkErrorString(nErr));
2176+
LogDebug(BCLog::NET, "socket recv error, %s: %s\n", pnode->DisconnectMsg(fLogIPs), NetworkErrorString(nErr));
21452177
}
21462178
pnode->CloseSocketDisconnect();
21472179
}
@@ -3411,6 +3443,7 @@ void CConnman::StopNodes()
34113443
std::vector<CNode*> nodes;
34123444
WITH_LOCK(m_nodes_mutex, nodes.swap(m_nodes));
34133445
for (CNode* pnode : nodes) {
3446+
LogDebug(BCLog::NET, "%s\n", pnode->DisconnectMsg(fLogIPs));
34143447
pnode->CloseSocketDisconnect();
34153448
DeleteNode(pnode);
34163449
}

src/net.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,22 @@ class CNode
947947

948948
std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
949949

950+
/**
951+
* Helper function to optionally log the IP address.
952+
*
953+
* @param[in] log_ip whether to include the IP address
954+
* @return " peeraddr=..." or ""
955+
*/
956+
std::string LogIP(bool log_ip) const;
957+
958+
/**
959+
* Helper function to log disconnects.
960+
*
961+
* @param[in] log_ip whether to include the IP address
962+
* @return "disconnecting peer=..." and optionally "peeraddr=..."
963+
*/
964+
std::string DisconnectMsg(bool log_ip) const;
965+
950966
/** A ping-pong round trip has completed successfully. Update latest and minimum ping times. */
951967
void PongReceived(std::chrono::microseconds ping_time) {
952968
m_last_ping_time = ping_time;

0 commit comments

Comments
 (0)