From 27515933cd8b83506f9371593bfed2a17eb0c4c1 Mon Sep 17 00:00:00 2001 From: David Goldstein Date: Mon, 9 Nov 2026 14:43:32 -0500 Subject: [PATCH] Row 3029: a write to a client that has left is a disconnect, not an error A shop owner quitting with a visitor inside logged ERROR 'Closing client connection ... Broken pipe'. The reader already treated a reset as the client leaving; the writer now does too, for resets and broken pipes. --- runtime/src/io/mod.rs | 14 +++++++++++++ runtime/src/io/read.rs | 14 +++---------- runtime/src/io/write.rs | 45 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/runtime/src/io/mod.rs b/runtime/src/io/mod.rs index 472d788b2..8847b2314 100644 --- a/runtime/src/io/mod.rs +++ b/runtime/src/io/mod.rs @@ -1,5 +1,19 @@ pub mod read; pub mod write; +use std::io; + pub use read::PacketReader; pub use write::PacketWriter; + +/// Whether an I/O error only means the peer has gone: a client that is +/// killed, or closes with data still unread, resets the connection, and a +/// write after it left breaks the pipe. It has still only left. +pub(crate) fn peer_left(error: &io::Error) -> bool { + matches!( + error.kind(), + io::ErrorKind::ConnectionReset + | io::ErrorKind::ConnectionAborted + | io::ErrorKind::BrokenPipe + ) +} diff --git a/runtime/src/io/read.rs b/runtime/src/io/read.rs index b5a6ffe70..e52a03302 100644 --- a/runtime/src/io/read.rs +++ b/runtime/src/io/read.rs @@ -1,7 +1,6 @@ use crate::error::RuntimeError; use crypt::MapleAES; use packet::{Packet, MAX_PACKET_LENGTH}; -use std::io; use tokio::io::{AsyncReadExt, BufReader}; use tokio::net::tcp::OwnedReadHalf; @@ -48,22 +47,15 @@ impl PacketReader { match self.reader.read_buf(&mut self.pending_bytes).await { Ok(0) => return Err(RuntimeError::ClientDisconnected), Ok(_) => {} - // A client that is killed, or closes with data still unread, - // resets the connection. It has still only left. - Err(error) if is_reset(&error) => return Err(RuntimeError::ClientDisconnected), + Err(error) if super::peer_left(&error) => { + return Err(RuntimeError::ClientDisconnected) + } Err(error) => return Err(error.into()), } } } } -fn is_reset(error: &io::Error) -> bool { - matches!( - error.kind(), - io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionAborted - ) -} - #[cfg(test)] mod tests { use super::*; diff --git a/runtime/src/io/write.rs b/runtime/src/io/write.rs index 86c454b90..e055c57ff 100644 --- a/runtime/src/io/write.rs +++ b/runtime/src/io/write.rs @@ -9,6 +9,15 @@ use tokio::net::tcp::OwnedWriteHalf; /// reading would otherwise block its connection's actor forever. pub const WRITE_TIMEOUT: Duration = Duration::from_secs(10); +/// A write to a peer that has gone only means it left. +fn left(error: std::io::Error) -> RuntimeError { + if super::peer_left(&error) { + RuntimeError::ClientDisconnected + } else { + error.into() + } +} + pub struct PacketWriter { writer: BufWriter, crypt: MapleAES, @@ -24,8 +33,8 @@ impl PacketWriter { /// Send the handshake packet (unencrypted). pub async fn send_handshake(&mut self, packet: &[u8]) -> Result<(), RuntimeError> { - self.writer.write_all(packet).await?; - self.writer.flush().await?; + self.writer.write_all(packet).await.map_err(left)?; + self.writer.flush().await.map_err(left)?; Ok(()) } @@ -46,7 +55,7 @@ impl PacketWriter { self.writer.flush().await }; match tokio::time::timeout(WRITE_TIMEOUT, write).await { - Ok(result) => Ok(result?), + Ok(result) => result.map_err(left), Err(_) => Err(RuntimeError::Io(std::io::Error::new( std::io::ErrorKind::TimedOut, "peer is not reading", @@ -54,3 +63,33 @@ impl PacketWriter { } } } + +#[cfg(test)] +mod tests { + use super::*; + use tokio::net::{TcpListener, TcpStream}; + + #[tokio::test] + async fn writing_to_a_client_that_left_is_a_disconnect() { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let (client, server) = tokio::join!( + TcpStream::connect(listener.local_addr().unwrap()), + listener.accept() + ); + let client = client.unwrap(); + let (_, write_half) = server.unwrap().0.into_split(); + let mut writer = PacketWriter::new(write_half, &[1, 2, 3, 4]); + client.set_zero_linger().unwrap(); + drop(client); + // The first write may still be accepted before the reset arrives. + let mut result = Ok(()); + for _ in 0..50 { + result = writer.send_packet(&mut Packet::new(&[0x0a, 0, 1])).await; + if result.is_err() { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + assert!(matches!(result, Err(RuntimeError::ClientDisconnected))); + } +}