Restore WireGuard tunnels after rapid reconnects and log QUIC closes
A peer can leave and return with the same WireGuard public key between reconciliation passes. The old reader then stays attached to a closed logical channel even though the carrier is up. Retain a tunnel only while its endpoint, announced key and open channel still match. Stop retired readers and start a handshake on the replacement channel. Carrier changes under the same channel keep the session and traffic counters. Record the newest channel before waking the worker, including while a network key is loading. A burst that fills the bounded command queue can now delay a sync without losing the channel the core handed over. Capture the QUIC close cause before local session cleanup. Include the network name and last known peer hostname alongside their IDs in the disconnect log. Report transport and application codes without copying arbitrary peer-supplied close text. The in-process disconnect event gains name fields. Mesh protocols, local IPC and storage formats stay compatible. On macOS arm64 both the loopback reconnect and close-code tests failed before the fix. The locked workspace suite now passes 606 tests with five opt-in tests ignored. It covers bidirectional IP after reconnect and a saturated notification queue. Formatting and strict workspace Clippy passed.
This commit is contained in:
1 parent
6a0a87e76c
commit
fb7a959d23
8 files changed
+299
-59
No files matched your search
@@ -9,7 +9,7 @@ use std::time::Duration;
|
||||
|
||||
use iroh::EndpointId;
|
||||
|
||||
use crate::identity::NetworkId;
|
||||
use crate::identity::{NetworkId, NetworkName};
|
||||
use crate::net::TransportKind;
|
||||
use crate::proto::ControlMessage;
|
||||
use crate::proto::handshake::Role;
|
||||
@@ -48,8 +48,12 @@ pub enum Event {
|
||||
PeerDisconnected {
|
||||
/// The network the session belonged to.
|
||||
network: NetworkId,
|
||||
/// Its locally configured name.
|
||||
network_name: NetworkName,
|
||||
/// The peer.
|
||||
peer: EndpointId,
|
||||
/// Last known name of the authenticated peer, when available.
|
||||
hostname: Option<String>,
|
||||
/// Why the session ended. Free of secrets.
|
||||
reason: String,
|
||||
},
|
||||
|
||||
@@ -2855,13 +2855,18 @@ impl Runtime {
|
||||
SessionEvent::Closed {
|
||||
session_id,
|
||||
peer,
|
||||
reason,
|
||||
mut reason,
|
||||
} => {
|
||||
let current = self.sessions.get(&peer).map(|session| session.id);
|
||||
if current != Some(session_id) {
|
||||
return;
|
||||
}
|
||||
if let Some(session) = self.sessions.remove(&peer) {
|
||||
// Read the cause before local cleanup closes a still-open connection.
|
||||
if let Some(cause) = session.conn.close_reason() {
|
||||
reason =
|
||||
format!("{reason} ({})", super::session::describe_quic_close(cause));
|
||||
}
|
||||
session.abort();
|
||||
session.conn.close(0u32.into(), b"session ended");
|
||||
}
|
||||
@@ -2885,7 +2890,13 @@ impl Runtime {
|
||||
self.params.roster_changed.send_replace(());
|
||||
self.emit(Event::PeerDisconnected {
|
||||
network: self.network_id,
|
||||
network_name: self.params.keys.name().clone(),
|
||||
peer,
|
||||
hostname: self
|
||||
.known_hostnames
|
||||
.get(&peer)
|
||||
.cloned()
|
||||
.or_else(|| self.state.hostname_of(&peer).map(str::to_owned)),
|
||||
reason,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::Instant;
|
||||
|
||||
use iroh::EndpointId;
|
||||
use iroh::endpoint::{Connection, RecvStream, SendStream};
|
||||
use iroh::endpoint::{Connection, ConnectionError, RecvStream, SendStream};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
@@ -60,6 +60,22 @@ pub(crate) enum SessionEvent {
|
||||
},
|
||||
}
|
||||
|
||||
/// The transport category and code are useful in logs. Remote close text is
|
||||
/// arbitrary peer-supplied data and may contain secrets or control characters.
|
||||
pub(super) fn describe_quic_close(cause: ConnectionError) -> String {
|
||||
match cause {
|
||||
ConnectionError::ApplicationClosed(close) => {
|
||||
format!("QUIC closed by peer, application code {}", close.error_code)
|
||||
}
|
||||
ConnectionError::ConnectionClosed(close) => {
|
||||
format!("QUIC closed by peer, transport code {}", close.error_code)
|
||||
}
|
||||
ConnectionError::TransportError(error) => format!("QUIC transport error {}", error.code),
|
||||
ConnectionError::LocallyClosed => "QUIC closed locally".to_owned(),
|
||||
other => format!("QUIC: {other}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// A live session, as held by the network runtime.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Session {
|
||||
|
||||
@@ -47,6 +47,79 @@ async fn open_control_stream(
|
||||
|
||||
const LIMIT: usize = 64 * 1024;
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_authenticated_disconnect_reports_the_quic_close_code() {
|
||||
let agent = TestAgent::spawn(&SharedMemoryDiscovery::new())
|
||||
.await
|
||||
.unwrap();
|
||||
let (name, secret) = network("disconnect-diagnostics");
|
||||
let id = agent.agent.join_network(&name, &secret).await.unwrap();
|
||||
let member = raw_endpoint().await;
|
||||
let (conn, mut send, mut recv) = open_control_stream(&member, agent.agent.local_addr()).await;
|
||||
let mut events = agent.agent.subscribe();
|
||||
tsunagi::proto::handshake::initiate(
|
||||
&conn,
|
||||
&mut send,
|
||||
&mut recv,
|
||||
member.id(),
|
||||
&NetworkKeys::derive(&name, &secret),
|
||||
&tsunagi::Limits::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let message = ControlMessage::Announce(tsunagi::proto::message::Announcement {
|
||||
broadcast: true,
|
||||
exit_node: false,
|
||||
hostname: "returning-peer".into(),
|
||||
capabilities: Vec::new(),
|
||||
agent_version: None,
|
||||
});
|
||||
write_frame(
|
||||
&mut send,
|
||||
&encode(&Envelope {
|
||||
network_id: *id.as_bytes(),
|
||||
message,
|
||||
})
|
||||
.unwrap(),
|
||||
LIMIT,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
tsunagi::testing::wait_until("peer hostname is known", || async {
|
||||
agent
|
||||
.agent
|
||||
.network_status(id)
|
||||
.await
|
||||
.ok()?
|
||||
.peers
|
||||
.into_iter()
|
||||
.find(|p| {
|
||||
p.endpoint_id == member.id() && p.hostname.as_deref() == Some("returning-peer")
|
||||
})
|
||||
})
|
||||
.await;
|
||||
conn.close(42u32.into(), b"private remote text\nsecond line");
|
||||
let (reported_network, hostname, reason) = wait_event(&mut events, |event| match event {
|
||||
Event::PeerDisconnected {
|
||||
network,
|
||||
network_name,
|
||||
peer,
|
||||
hostname,
|
||||
reason,
|
||||
} if *network == id && *peer == member.id() => {
|
||||
Some((network_name.clone(), hostname.clone(), reason.clone()))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(reported_network, name);
|
||||
assert_eq!(hostname.as_deref(), Some("returning-peer"));
|
||||
assert!(reason.contains("application code 42"), "{reason}");
|
||||
assert!(!reason.contains("private remote text"));
|
||||
member.close().await;
|
||||
agent.agent.shutdown().await;
|
||||
}
|
||||
|
||||
/// Sends `Hello` and reads the responder's `HelloAck`.
|
||||
async fn exchange_hellos(
|
||||
send: &mut SendStream,
|
||||
|
||||
Reference in new issue
Block a user