node/reactor: Terminate transports whose socket is dead

A transport whose peer has gone away is never terminated, and the
reactor then spins on it at 100% of a core. On every iteration the reactor
[re-registers the socket with whatever interests the transport still
reports][1], `poll` returns it as ready immediately, and the I/O
handlers do nothing with it. The service thread stops serving the
control socket, so `rad node status` times out and the node looks
offline while its container is still up. Restarting a node only helps
until the next peer disconnect.

How the transport loses track of the socket:

1. A peer resets the connection between `poll` returning and the event
   being handled. The event carries no error flag, so the reactor
   [hands it to the transport instead of reaping it][2].
2. On the writable event the transport [first asks whether the session
   is established][3]. That answer is [derived from the session
   artifact][4], which for a TCP stream [comes from `getpeername`][5].
   On a reset socket that fails with ENOTCONN, so the transport takes
   the "handshake in progress" branch.
3. That branch [pokes the session with a zero-length write and
   discards the result][6]. The write [passes straight through to the
   socket][7], and a zero-length send is exactly what surfaces the
   pending ECONNRESET. The one signal that the peer is gone is thrown
   away, and the transport [re-arms its write interest][8] instead.
4. On the readable event the read also [passes straight through to
   the socket][9] and returns zero bytes, which the transport
   [ignores as well][10].

Nothing ever [marks the transport terminated][11], so its [interest
set][12] stays READABLE and WRITABLE on a closed socket forever.

On an affected node, strace shows thousands of failing `getpeername`
calls in 5 seconds, 5 per iteration, with `epoll_pwait` returning 1,200
times a second.

Two changes:

- Propagate a real error from the handshake-branch zero-length write
  as a termination instead of discarding it. WouldBlock, Interrupted
  and WriteZero stay non-fatal, matching the established branch. A
  socket whose error was already consumed elsewhere reports EPIPE
  here, so this also reaps transports that are already stuck.
- Treat a zero-byte read as end-of-file once the transport is
  [active][13]. From that point reads pass straight through to the
  socket. During the handshake the protocol layer [returns zero after
  consuming a handshake step][14], so that case keeps its current
  behaviour. Without this a half-closed peer also spins, until the
  service's [stale connection reaper][15] disconnects it about
  [2 minutes][16] later.

The termination event [reaches the wire layer][17], which [unregisters
the transport whether or not it has a peer entry for it][18]. That
covers inbound connections that die mid-handshake, which have no node
ID and so are invisible to the stale reaper.

Tests use real loopback sockets: a peer that resets (SO_LINGER 0), the
same after the pending error has already been taken, and a peer that
half-closes. All three failed before this change.

[1]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor.rs#L350
[2]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor.rs#L444
[3]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L206
[4]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/session.rs#L21
[5]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/session.rs#L180
[6]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L207
[7]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/session.rs#L109
[8]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L208
[9]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/session.rs#L78
[10]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L298
[11]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L144
[12]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L317
[13]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/transport.rs#L45
[14]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/reactor/session.rs#L99
[15]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-protocol/src/service.rs#L2417
[16]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-protocol/src/service.rs#L91
[17]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/wire.rs#L819
[18]: https://radicle.network/nodes/seed.radicle.garden/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5/tree/0e5a56f25f51d07290613a9363bd8c0c9fc8e0be/crates/radicle-node/src/wire.rs#L356
This commit is contained in:
Yorgos Saslis
2026-10-08 12:12:15 +02:00
committed by Lorenz Leutgeb
parent 0f71581033
commit 2f930e3561
+100 -2
View File
@@ -204,8 +204,12 @@ impl<S: Session> Transport<S> {
use io::ErrorKind::*;
if !self.session.is_established() {
let _ = self.session.write(&[]);
self.write_intent = true;
match self.session.write(&[]) {
Err(err) if !matches!(err.kind(), WouldBlock | Interrupted | WriteZero) => {
events.push(self.terminate(err));
}
_ => self.write_intent = true,
}
return;
}
@@ -295,6 +299,9 @@ impl<S: Session> Transport<S> {
loop {
match self.session.read(self.read_buffer.as_mut()) {
Ok(0) if self.state == TransportState::Active => {
events.push(self.terminate(UnexpectedEof.into()));
}
Ok(0) => {}
Ok(len) => {
events.push(SessionEvent::Data(self.read_buffer[..len].to_vec()));
@@ -364,3 +371,94 @@ impl<S: Session> super::BufferWrite for Transport<S> {
self.write_intent = true;
}
}
#[cfg(all(test, unix, feature = "socket2"))]
mod tests {
use std::net::{Shutdown, TcpListener};
use std::time::Duration;
use std::{net, thread};
use mio::net::TcpStream;
use super::*;
fn connected_pair() -> (TcpStream, net::TcpStream) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let local = net::TcpStream::connect(listener.local_addr().unwrap()).unwrap();
let (remote, _) = listener.accept().unwrap();
local.set_nonblocking(true).unwrap();
(TcpStream::from_std(local), remote)
}
fn active_transport() -> (Transport<TcpStream>, net::TcpStream) {
let (local, remote) = connected_pair();
let transport = Transport::with_session(local).unwrap();
assert_eq!(transport.state, TransportState::Active);
(transport, remote)
}
fn reset(remote: net::TcpStream) {
socket2::SockRef::from(&remote)
.set_linger(Some(Duration::ZERO))
.unwrap();
drop(remote);
thread::sleep(Duration::from_millis(100));
}
fn handle_ready(transport: &mut Transport<TcpStream>) -> Vec<SessionEvent<TcpStream>> {
let mut events = Vec::new();
transport.handle_io(Interest::WRITABLE, &mut events);
transport.handle_io(Interest::READABLE, &mut events);
events
}
fn assert_terminated(transport: &Transport<TcpStream>, events: &[SessionEvent<TcpStream>]) {
assert!(
events
.iter()
.any(|event| matches!(event, SessionEvent::Terminated(_))),
"transport was not terminated"
);
assert_eq!(transport.state, TransportState::Terminated);
assert_eq!(transport.interests(), None);
}
#[test]
fn reset_peer_is_terminated() {
let (mut transport, remote) = active_transport();
reset(remote);
let events = handle_ready(&mut transport);
assert_terminated(&transport, &events);
}
#[test]
fn reset_peer_is_terminated_after_error_was_consumed() {
let (mut transport, remote) = active_transport();
reset(remote);
let error = transport.session.take_error().unwrap();
assert!(
error.is_some(),
"expected a pending socket error after reset"
);
let events = handle_ready(&mut transport);
assert_terminated(&transport, &events);
}
#[test]
fn half_closed_peer_is_terminated_on_eof() {
let (mut transport, remote) = active_transport();
remote.shutdown(Shutdown::Write).unwrap();
thread::sleep(Duration::from_millis(100));
let mut events = Vec::new();
transport.handle_io(Interest::READABLE, &mut events);
assert_terminated(&transport, &events);
}
}