From bd188c19d8d78170fb2ca5a2e49a2eaebc8adad4 Mon Sep 17 00:00:00 2001 From: batistalucasv Date: Thu, 12 Nov 2026 16:42:47 -0300 Subject: [PATCH] Tests: only closure errors mean the control peer closed (#6448) CodeRabbit pointed out that read_line returns InvalidData for bytes that are not UTF-8 while the socket stays open, and the helper treated every non-timeout error as a close. closed_by_peer() now accepts only ConnectionReset, ConnectionAborted, NotConnected and BrokenPipe, and the regression test pins InvalidData, TimedOut and WouldBlock as still open; the new assertion fails without the stricter match. Co-authored-by: Qwen-Coder --- apps/wordcraft/src/control_server.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/wordcraft/src/control_server.rs b/apps/wordcraft/src/control_server.rs index 47178455cd..ad5b119797 100644 --- a/apps/wordcraft/src/control_server.rs +++ b/apps/wordcraft/src/control_server.rs @@ -397,11 +397,18 @@ mod tests { /// Whether a read means the peer closed the connection. End of stream does; so does a reset, /// which is how Windows can deliver a close when the writer had nothing to read back (the - /// Windows Print job failed here). Only a timeout means the connection is still open. + /// Windows Print job failed here). Any other error, such as `InvalidData` for bytes that are + /// not UTF-8, leaves the connection open. fn closed_by_peer(read: std::io::Result) -> bool { match read { Ok(n) => n == 0, - Err(e) => !matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock), + Err(e) => matches!( + e.kind(), + std::io::ErrorKind::ConnectionReset + | std::io::ErrorKind::ConnectionAborted + | std::io::ErrorKind::NotConnected + | std::io::ErrorKind::BrokenPipe + ), } } @@ -487,14 +494,15 @@ mod tests { } #[test] - fn a_reset_counts_as_closed_but_a_timeout_does_not() { + fn only_closure_errors_mean_the_peer_closed() { use std::io::{Error, ErrorKind}; assert!(closed_by_peer(Ok(0)), "end of stream"); for kind in [ErrorKind::ConnectionReset, ErrorKind::ConnectionAborted, ErrorKind::NotConnected, ErrorKind::BrokenPipe] { assert!(closed_by_peer(Err(Error::new(kind, "closed"))), "{kind:?}"); } assert!(!closed_by_peer(Ok(1)), "a reply arrived"); - for kind in [ErrorKind::TimedOut, ErrorKind::WouldBlock] { + // Not UTF-8, a timeout or a would-block leave the socket open. + for kind in [ErrorKind::InvalidData, ErrorKind::TimedOut, ErrorKind::WouldBlock] { assert!(!closed_by_peer(Err(Error::new(kind, "still open"))), "{kind:?}"); } }