From 2f590c103ad7210b4539a24b4c7ba938afc4be46 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 15 Oct 2026 17:49:40 +0000 Subject: [PATCH] fix(noq): don't stall tokio UDP sends on OpenBSD send lock contention OpenBSD fails a non-blocking UDP sendmsg with EAGAIN while another thread holds the socket's send buffer lock: sosend takes it with RW_NOSLEEP under MSG_DONTWAIT, which sendit sets for non-blocking descriptors. The buffer itself never fills for UDP. The tokio runtime's try_send went through try_io, which clears write readiness on WouldBlock, so the sender then waited for a kqueue EVFILT_WRITE event that a UDP socket never raises again (nothing ever drains its send buffer). Every connection sending on that socket stalled for good: an endpoint shared by several connections, or a multipath connection sending on several paths at once, went silent until its connections timed out. On OpenBSD, retry such a send a bounded number of times without touching readiness, and drop the datagram if contention persists, as for other send errors. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012baC35vUbLPPhuK4pz4uRz --- noq/src/runtime/tokio.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/noq/src/runtime/tokio.rs b/noq/src/runtime/tokio.rs index ae26aa30cd..252adf3f83 100644 --- a/noq/src/runtime/tokio.rs +++ b/noq/src/runtime/tokio.rs @@ -62,12 +62,35 @@ impl UdpSenderHelperSocket for UdpSocket { } fn try_send(&self, transmit: &udp::Transmit<'_>) -> io::Result<()> { + // OpenBSD fails a non-blocking UDP send with EAGAIN while another thread holds + // the socket's send buffer lock (`sosend` does not sleep for it under + // MSG_DONTWAIT), never because that buffer is full: UDP does not queue there. + // Clearing write readiness on such an error, as `try_io` does, waits for a + // kqueue write event that a UDP socket never raises again, stalling every + // sender on the socket for good. Retry instead, and drop the datagram if the + // contention persists, as for other send errors. + #[cfg(target_os = "openbsd")] + { + for _ in 0..OPENBSD_SEND_RETRIES { + match self.inner.send((&self.io).into(), transmit) { + Err(e) if e.kind() == io::ErrorKind::WouldBlock => std::thread::yield_now(), + result => return result, + } + } + Ok(()) + } + #[cfg(not(target_os = "openbsd"))] self.io.try_io(Interest::WRITABLE, || { self.inner.send((&self.io).into(), transmit) }) } } +/// How often [`UdpSocket::try_send`] retries a send that lost the race for the +/// socket's send buffer lock on OpenBSD. +#[cfg(target_os = "openbsd")] +const OPENBSD_SEND_RETRIES: usize = 64; + impl AsyncUdpSocket for UdpSocket { fn create_sender(&self) -> Pin> { Box::pin(UdpSenderHelper::new(self.clone(), |socket: &Self| {