Skip to content

armgit / herdr

Public Imported from GitHub
0

the runtime your coding agents live on

test: deflake observer write timeout on loaded runners (#5280)

JJ Liebig3b0c3e9e26b8
diff --git a/src/server/client_transport.rs b/src/server/client_transport.rs
index db985baca..b49cf7c7a 100644
--- a/src/server/client_transport.rs
+++ b/src/server/client_transport.rs
@@ -1701,29 +1701,62 @@ mod tests {
));
}
+ /// Keeps both kernel socket buffers far below the frame size so the writer must
+ /// block for the observer instead of accepting the whole frame at once. Without
+ /// this the reset path would only be exercised when the platform default buffers
+ /// happen to be smaller than the frame.
+ #[cfg(unix)]
+ fn limit_local_stream_buffers(stream: &LocalStream) {
+ use std::os::fd::AsRawFd as _;
+
+ let LocalStream::UdSocket(socket) = stream;
+ let bytes: libc::c_int = 64 * 1024;
+ for option in [libc::SO_SNDBUF, libc::SO_RCVBUF] {
+ let result = unsafe {
+ libc::setsockopt(
+ socket.inner().as_raw_fd(),
+ libc::SOL_SOCKET,
+ option,
+ std::ptr::addr_of!(bytes).cast(),
+ std::mem::size_of::<libc::c_int>() as libc::socklen_t,
+ )
+ };
+ assert_eq!(result, 0, "setsockopt({option}) failed");
+ }
+ }
+
#[cfg(unix)]
#[test]
fn observer_write_timeout_resets_when_sending_makes_progress() {
use std::io::Read as _;
+ // A slow observer that keeps draining must not be disconnected, because every
+ // partial write resets the writer's deadline. Keep the drain cadence far below
+ // the write timeout so a loaded runner's scheduler jitter cannot manufacture a
+ // stall, while the transfer still outlasts the timeout so the reset is required
+ // (otherwise the writer could finish within one timeout window without resetting).
+ const WRITE_TIMEOUT: Duration = Duration::from_secs(1);
+ const DRAIN_INTERVAL: Duration = Duration::from_millis(25);
+ const TOTAL_BYTES: usize = 1024 * 1024;
+
let (mut client, mut server, _path) = local_stream_pair("slow-observer");
- server
- .set_send_timeout(Some(Duration::from_millis(100)))
- .unwrap();
+ limit_local_stream_buffers(&server);
+ limit_local_stream_buffers(&client);
+ server.set_send_timeout(Some(WRITE_TIMEOUT)).unwrap();
server.set_nonblocking(true).unwrap();
let worker = std::thread::spawn(move || {
- assert!(write_framed_bytes(&mut server, &vec![b'x'; 1024 * 1024]));
+ assert!(write_framed_bytes(&mut server, &vec![b'x'; TOTAL_BYTES]));
});
client
- .set_recv_timeout(Some(Duration::from_secs(3)))
+ .set_recv_timeout(Some(Duration::from_secs(5)))
.unwrap();
let mut received = 0;
let mut buffer = [0; 16 * 1024];
- while received < 1024 * 1024 {
+ while received < TOTAL_BYTES {
let count = client.read(&mut buffer).unwrap();
assert_ne!(count, 0, "observer disconnected while making progress");
received += count;
- std::thread::sleep(Duration::from_millis(5));
+ std::thread::sleep(DRAIN_INTERVAL);
}
worker.join().unwrap();
}