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:?}"); } }