From 7ce1f83fe5f33d66d44bddfc9ba3ec17dd28a4c2 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 8 Sep 2026 14:02:13 +0800 Subject: [PATCH] fix(stat): keep outbound UDP session alive across datagram split Datagram::split() takes the inner datagram and then drops the wrapper, which notified the cleanup task and retired the session counters right when traffic was about to start. Only end the session on drop if the datagram was never split; once split, the recv/send halves drive retirement through move_to_recent. Add tests for both paths. --- leaf/src/app/stat_manager.rs | 102 ++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/leaf/src/app/stat_manager.rs b/leaf/src/app/stat_manager.rs index dbde65a..672dead 100644 --- a/leaf/src/app/stat_manager.rs +++ b/leaf/src/app/stat_manager.rs @@ -93,7 +93,14 @@ pub struct Datagram { impl Drop for Datagram { fn drop(&mut self) { - let _ = self.tx.send(self.id); + // If the datagram is dropped before being split, treat it as a completed + // session and notify the cleanup task immediately. Once split, the recv/send + // halves own the completion state and will drive retirement via move_to_recent. + if self.inner.is_some() { + self.recv_completed.store(true, Ordering::Relaxed); + self.send_completed.store(true, Ordering::Relaxed); + let _ = self.tx.send(self.id); + } } } @@ -468,6 +475,10 @@ impl StatManager { #[cfg(test)] mod tests { use super::*; + use crate::proxy::{OutboundDatagram, OutboundDatagramRecvHalf, OutboundDatagramSendHalf}; + use crate::session::SocksAddr; + use async_trait::async_trait; + use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use tokio::io::ReadBuf; struct MockStream { @@ -508,6 +519,44 @@ mod tests { } } + struct MockOutboundDatagramRecvHalf; + + #[async_trait] + impl OutboundDatagramRecvHalf for MockOutboundDatagramRecvHalf { + async fn recv_from(&mut self, _buf: &mut [u8]) -> io::Result<(usize, SocksAddr)> { + Err(io::Error::new(io::ErrorKind::UnexpectedEof, "eof")) + } + } + + struct MockOutboundDatagramSendHalf; + + #[async_trait] + impl OutboundDatagramSendHalf for MockOutboundDatagramSendHalf { + async fn send_to(&mut self, _buf: &[u8], _dst_addr: &SocksAddr) -> io::Result { + Ok(0) + } + + async fn close(&mut self) -> io::Result<()> { + Ok(()) + } + } + + struct MockOutboundDatagram; + + impl OutboundDatagram for MockOutboundDatagram { + fn split( + self: Box, + ) -> ( + Box, + Box, + ) { + ( + Box::new(MockOutboundDatagramRecvHalf), + Box::new(MockOutboundDatagramSendHalf), + ) + } + } + #[tokio::test] async fn test_stat_stream_non_empty_buf() { let mock = MockStream { @@ -549,4 +598,55 @@ mod tests { let received = bytes_recvd.load(Ordering::Relaxed); assert_eq!(received, 5, "Expected 5 bytes received, got {}", received); } + + #[test] + fn stat_outbound_datagram_does_not_end_on_split() { + let mut sm = StatManager::new(); + let sess = Session { + outbound_tag: "ss-cabi".to_string(), + destination: SocksAddr::from(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8001)), + ..Default::default() + }; + + let dgram = sm.stat_outbound_datagram(Box::new(MockOutboundDatagram), sess); + assert_eq!(sm.counters.len(), 1); + + let (recv_half, send_half) = dgram.split(); + assert_eq!(sm.counters.len(), 1); + assert!( + sm.rx.as_mut().expect("rx should exist").try_recv().is_err(), + "splitting datagram should not end the session immediately" + ); + + drop(recv_half); + drop(send_half); + sm.move_to_recent(); + + assert!(sm.counters.is_empty()); + assert!( + sm.rx.as_mut().expect("rx should exist").try_recv().is_err(), + "split datagram retirement should be driven by half completion, not drop notifications" + ); + } + + #[test] + fn stat_outbound_datagram_unsplit_drop_ends_session() { + let mut sm = StatManager::new(); + let sess = Session { + outbound_tag: "ss-cabi".to_string(), + destination: SocksAddr::from(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8001)), + ..Default::default() + }; + + let dgram = sm.stat_outbound_datagram(Box::new(MockOutboundDatagram), sess); + drop(dgram); + + let id = sm + .rx + .as_mut() + .expect("rx should exist") + .try_recv() + .expect("unsplit datagram drop should enqueue cleanup"); + assert_eq!(id, 1); + } }