From afbe61c547b767aa90947b23d0be080b7f4bd9f5 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 08:48:26 +0700 Subject: [PATCH] automation: the control server ends a connection after its last reply by closing its write side and reading off what the client still sends, so the close no longer resets the connection and loses the reply or the end of the stream --- crates/automation/src/server.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs index b2f9fdd1d..c6808156d 100644 --- a/crates/automation/src/server.rs +++ b/crates/automation/src/server.rs @@ -1,7 +1,7 @@ //! The loopback JSON-lines TCP transport: one request per line, one reply per line. -use std::io::{BufReader, Write}; -use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::io::{BufReader, Read, Write}; +use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{Receiver, Sender, channel}; @@ -89,6 +89,7 @@ pub fn serve_with_deadline( let Some(permit) = limiter.try_acquire() else { let _ = configure_stream(&stream); let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "connection limit reached"})); + close_after_reply(&stream); continue; }; let tx = tx.clone(); @@ -179,4 +180,27 @@ fn serve_connection(stream: TcpStream, token: &str, tx: &Sender, break; } } + close_after_reply(&out); +} + +/// How long a closing connection's leftover input is read and dropped ([`close_after_reply`]). +const CLOSE_LINGER: Duration = Duration::from_millis(250); + +/// Closes `stream` after its last reply without resetting it: the write side ends first, so the client reads the +/// reply and then the end of the stream, and what the client still sends is read and dropped until it closes too, for +/// at most [`CLOSE_LINGER`]. A socket closed with input unread is reset instead, and the client loses the reply +/// (Linux) or reads the reset where the end of the stream should be (Windows). +fn close_after_reply(mut stream: &TcpStream) { + let _ = stream.shutdown(Shutdown::Write); + let deadline = motion::now() + CLOSE_LINGER; + let mut sink = [0; 1024]; + loop { + let left = deadline.saturating_duration_since(motion::now()); + if left.is_zero() || stream.set_read_timeout(Some(left)).is_err() { + return; + } + if matches!(stream.read(&mut sink), Ok(0) | Err(_)) { + return; + } + } }