From 26c2e45f04a540765faaf4c04ac0534dc5b7e525 Mon Sep 17 00:00:00 2001 From: KKRainbow <5665404+KKRainbow@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:51:07 +0800 Subject: [PATCH] fix(peers): yield to connection writers after filling the queue A ready packet source can enqueue more than 32 packets without yielding. After ordinary sends switched to try_send, the writer can lose its turn until the remainder of a burst has been dropped. This loses fragments of a maximum-size UDP datagram and stalls data-plane TCP teardown. Yield once after a successful ordinary enqueue consumes the last slot. Already-full queues still return BufferFull immediately. There is no capacity wait, retry or queue-size change; heartbeat admission and fatal connection errors keep their existing behavior. Pre-fill the overflow fixture through the sink so it retains a blocked writer while checking actual PeerConn admission and closure semantics. The original failing gateway tests remain unchanged. Validation: - Reproduce both CI failures and confirm BufferFull events during each. - Pass both original tests and 31 relevant data-plane, queue, heartbeat, writer-cleanup and Portal lifecycle regressions. - Pass cargo fmt --all -- --check and strict workspace all-target clippy with full features. - Complete independent read-only review of the frozen diff: no findings. --- easytier-core/src/peers/conn/peer_conn.rs | 17 ++++++++++------- easytier-core/src/tunnel/mpsc.rs | 4 ++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/easytier-core/src/peers/conn/peer_conn.rs b/easytier-core/src/peers/conn/peer_conn.rs index bac79a43..d58697c0 100644 --- a/easytier-core/src/peers/conn/peer_conn.rs +++ b/easytier-core/src/peers/conn/peer_conn.rs @@ -1476,7 +1476,13 @@ impl PeerConn { let result = if is_heartbeat(&msg) { self.sink.send(msg).await } else { - self.sink.try_send(msg) + let result = self.sink.try_send(msg); + if result.is_ok() && self.sink.ordinary_queue_is_full() { + // Let the writer run before a ready producer fills the next + // turn with drops. Do not wait for capacity or retry the item. + tokio::task::yield_now().await; + } + result }; if let Err(error) = result { if !matches!(error, TunnelError::BufferFull) { @@ -1655,13 +1661,10 @@ mod tests { packet.fill_peer_manager_hdr(1, 2, kind as u8); packet }; - // Do not yield to the writer: exercise the real connection's - // admission policy with its ordinary queue deterministically full. + // Pre-fill without yielding to the writer, then exercise the + // connection's admission policy with its ordinary queue full. for _ in 0..32 { - conn.send_msg(packet(PacketType::Data)) - .now_or_never() - .unwrap() - .unwrap(); + conn.sink.try_send(packet(PacketType::Data)).unwrap(); } let session = Arc::new(PeerSession::new( 2, diff --git a/easytier-core/src/tunnel/mpsc.rs b/easytier-core/src/tunnel/mpsc.rs index c5148400..f8201649 100644 --- a/easytier-core/src/tunnel/mpsc.rs +++ b/easytier-core/src/tunnel/mpsc.rs @@ -52,6 +52,10 @@ impl MpscTunnelSender { TrySendError::Closed(_) => TunnelError::Shutdown, }) } + + pub(crate) fn ordinary_queue_is_full(&self) -> bool { + self.sender.capacity() == 0 + } } /// Only connection writers expand batches. Internal RPC rings keep one entry