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:
Aleksandr Bogomiakov committed 2026-11-25 18:22:50 +02:00
1 parent 6a0a87e76c
commit fb7a959d23
8 files changed
+299 -59

No files matched your search

+5 -1
View File
@@ -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,
},
+12 -1
View File
@@ -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,
});
}
+17 -1
View File
@@ -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 {