From 79d276ca328f045f265c08ebe0abccd3fed533ad Mon Sep 17 00:00:00 2001 From: oars Date: Wed, 15 Oct 2025 14:03:53 +0300 Subject: [PATCH] net,dht,error: Include the endpoint address in connector error messages so upstream code can log it. In transport mixing scenarios, the mixed URL will appear in logs. --- src/dht/mod.rs | 2 +- src/error.rs | 8 ++++---- src/net/connector.rs | 12 ++++++++---- src/net/hosts.rs | 2 +- src/net/session/manual_session.rs | 30 +++++++++++++++-------------- src/net/session/outbound_session.rs | 12 ++++++------ src/net/session/refine_session.rs | 2 +- src/net/session/seedsync_session.rs | 23 ++++++++++++---------- 8 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/dht/mod.rs b/src/dht/mod.rs index 0ce818a72..c914cb92a 100644 --- a/src/dht/mod.rs +++ b/src/dht/mod.rs @@ -566,7 +566,7 @@ impl Dht { return Err(Error::ConnectTimeout); }; if connect_res.is_err() { - warn!(target: "dht::get_channel()", "Error while connecting to {addr}: {}", connect_res.unwrap_err()); + warn!(target: "dht::get_channel()", "Error while connecting: {}", connect_res.unwrap_err()); continue; } let (_, channel) = connect_res.unwrap(); diff --git a/src/error.rs b/src/error.rs index d2067c152..b9ae70c38 100644 --- a/src/error.rs +++ b/src/error.rs @@ -115,8 +115,8 @@ pub enum Error { #[error("Transport request exceeds number of accepted transports")] InvalidTransportRequest, - #[error("Connection failed")] - ConnectFailed, + #[error("Connection failed: {0}")] + ConnectFailed(String), #[cfg(feature = "system")] #[error(transparent)] @@ -146,8 +146,8 @@ pub enum Error { #[error("Accept a new tls connection from the listener {0} failed")] AcceptTlsConnectionFailed(String), - #[error("Connector stopped")] - ConnectorStopped, + #[error("Connector stopped: {0}")] + ConnectorStopped(String), #[error("Network operation failed")] NetworkOperationFailed, diff --git a/src/net/connector.rs b/src/net/connector.rs index 0510d4bf8..31111f50d 100644 --- a/src/net/connector.rs +++ b/src/net/connector.rs @@ -59,7 +59,7 @@ impl Connector { let hosts = self.session.upgrade().unwrap().p2p().hosts(); if hosts.container.contains(HostColor::Black as usize, url) || hosts.block_all_ports(url) { warn!(target: "net::connector::connect", "Peer {url} is blacklisted"); - return Err(Error::ConnectFailed) + return Err(Error::ConnectFailed(format!("[{url}]: Peer is blacklisted"))); } let settings = self.settings.read().await; @@ -86,7 +86,11 @@ impl Connector { (url.clone(), false) }; - let dialer = Dialer::new(endpoint.clone(), datastore, Some(i2p_socks5_proxy)).await?; + let dialer = match Dialer::new(endpoint.clone(), datastore, Some(i2p_socks5_proxy)).await { + Ok(dialer) => dialer, + Err(err) => return Err(Error::ConnectFailed(format!("[{endpoint}]: {err}"))), + }; + let timeout = Duration::from_secs(outbound_connect_timeout); let stop_fut = async { @@ -121,10 +125,10 @@ impl Connector { .ipv6_available .store(false, Ordering::SeqCst); } - Err(e.into()) + Err(Error::ConnectFailed(format!("[{endpoint}]: {e}"))) } - Either::Right((_, _)) => Err(Error::ConnectorStopped), + Either::Right((_, _)) => Err(Error::ConnectorStopped(format!("[{endpoint}]"))), } } diff --git a/src/net/hosts.rs b/src/net/hosts.rs index 9d8155c19..68dedee3e 100644 --- a/src/net/hosts.rs +++ b/src/net/hosts.rs @@ -1133,7 +1133,7 @@ impl Hosts { // This will error if we are already connected to this peer, this peer // is suspended, or this peer is currently being inserted into the hostlist. // None of these scenarios should ever happen. - if let Err(e) = self.try_register(address.clone(), HostState::Connected(channel.clone())) { + if let Err(e) = self.try_register(address, HostState::Connected(channel.clone())) { warn!(target: "net::hosts::register_channel", "Error while registering channel {channel:?}: {e:?}"); return } diff --git a/src/net/session/manual_session.rs b/src/net/session/manual_session.rs index daf5e7b01..76bc2e138 100644 --- a/src/net/session/manual_session.rs +++ b/src/net/session/manual_session.rs @@ -185,7 +185,7 @@ impl Slot { } match self.connector.connect(&self.addr).await { - Ok((url, channel)) => { + Ok((_, channel)) => { info!( target: "net::manual_session", "[P2P] Manual outbound connected [{}]", @@ -209,12 +209,25 @@ impl Slot { ); } Err(e) => { - self.handle_failure(e, &url); + warn!( + target: "net::manual_session", + "[P2P] Unable to connect to manual outbound [{}]: {e}", + channel.display_address(), + ); + + // Free up this addr for future operations. + self.p2p().hosts().unregister(channel.address()); } } } Err(e) => { - self.handle_failure(e, &self.addr); + warn!( + target: "net::manual_session", + "[P2P] Unable to connect to manual outbound: {e}", + ); + + // Free up this addr for future operations. + self.p2p().hosts().unregister(&self.addr); } } @@ -228,17 +241,6 @@ impl Slot { } } - fn handle_failure(&self, error: Error, addr: &Url) { - warn!( - target: "net::manual_session", - "[P2P] Unable to connect to manual outbound [{}]: {error}", - self.addr - ); - - // Free up this addr for future operations. - self.p2p().hosts().unregister(addr); - } - fn session(&self) -> ManualSessionPtr { self.session.upgrade().unwrap() } diff --git a/src/net/session/outbound_session.rs b/src/net/session/outbound_session.rs index d849a20c3..5ddc7a6db 100644 --- a/src/net/session/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -306,7 +306,7 @@ impl Slot { addr: host.clone(), }); - let (addr, channel) = match self.try_connect(host.clone(), last_seen).await { + let (_, channel) = match self.try_connect(host.clone(), last_seen).await { Ok(connect_info) => connect_info, Err(err) => { debug!( @@ -397,15 +397,15 @@ impl Slot { Err(err) => { info!( target: "net::outbound_session::try_connect()", - "[P2P] Unable to connect outbound slot #{} [{addr}]: {err}", + "[P2P] Unable to connect outbound slot #{} {err}", self.slot ); // Immediately return if the Connector has stopped. // This indicates a shutdown of the P2P network and // should not result in hostlist modifications. - if let Error::ConnectorStopped = err { - return Err(Error::ConnectFailed); + if let Error::ConnectorStopped(message) = err { + return Err(Error::ConnectFailed(message)); } // At this point we failed to connect. We'll downgrade this peer now. @@ -415,9 +415,9 @@ impl Slot { self.p2p().hosts().try_register(addr.clone(), HostState::Suspend).unwrap(); // Notify that channel processing failed - self.p2p().hosts().channel_publisher.notify(Err(Error::ConnectFailed)).await; + self.p2p().hosts().channel_publisher.notify(Err(err.clone())).await; - Err(Error::ConnectFailed) + Err(err) } } } diff --git a/src/net/session/refine_session.rs b/src/net/session/refine_session.rs index 6c384cfd2..032bf47bd 100644 --- a/src/net/session/refine_session.rs +++ b/src/net/session/refine_session.rs @@ -163,7 +163,7 @@ impl RefineSession { } Err(e) => { - debug!(target: "net::refinery::handshake_node()", "Failed to connect to {addr}, ({e})"); + debug!(target: "net::refinery::handshake_node()", "Failed to connect ({e})"); false } } diff --git a/src/net/session/seedsync_session.rs b/src/net/session/seedsync_session.rs index 42c3132b1..f04c79d1b 100644 --- a/src/net/session/seedsync_session.rs +++ b/src/net/session/seedsync_session.rs @@ -213,7 +213,7 @@ impl Slot { } match self.connector.connect(&self.addr).await { - Ok((url, ch)) => { + Ok((_, ch)) => { info!( target: "net::session::seedsync_session", "[P2P] Connected seed [{}]", @@ -242,7 +242,12 @@ impl Slot { } Err(e) => { - self.handle_failure(e, ch.address()); + warn!( + target: "net::session::seedsync_session", + "[P2P] Unable to connect to seed [{}]: {e}", + ch.display_address() + ); + self.handle_failure(ch.address()); continue } @@ -250,7 +255,11 @@ impl Slot { } Err(e) => { - self.handle_failure(e, &self.addr); + warn!( + target: "net::session::seedsync_session", + "[P2P] Unable to connect to seed: {e}", + ); + self.handle_failure(&self.addr); continue } @@ -262,13 +271,7 @@ impl Slot { } } - fn handle_failure(&self, error: Error, addr: &Url) { - warn!( - target: "net::session::seedsync_session", - "[P2P] Unable to connect to seed [{}]: {error}", - self.addr - ); - + fn handle_failure(&self, addr: &Url) { self.failed.store(true, SeqCst); // Free up this addr for future operations.