Skip to content

Commit 6cb5ab3

Browse files
committed
TcpConnection::poll_read pcb guarded by spinlock
1 parent dc0de88 commit 6cb5ab3

File tree

1 file changed

+7
-11
lines changed
  • crates/shadowsocks-service/src/local/tun

1 file changed

+7
-11
lines changed

crates/shadowsocks-service/src/local/tun/tcp.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use smoltcp::{
2424
time::{Duration as SmolDuration, Instant as SmolInstant},
2525
wire::{IpAddress, IpCidr, Ipv4Address, Ipv6Address, TcpPacket},
2626
};
27+
use spin::Mutex as SpinMutex;
2728
use tokio::{
2829
io::{AsyncRead, AsyncWrite, ReadBuf},
2930
sync::mpsc,
@@ -93,7 +94,7 @@ struct TcpSocketManager {
9394
socket_creation_rx: mpsc::UnboundedReceiver<TcpSocketCreation>,
9495
}
9596

96-
type SharedTcpConnectionControl = Arc<ParkingMutex<TcpSocketControl>>;
97+
type SharedTcpConnectionControl = Arc<SpinMutex<TcpSocketControl>>;
9798

9899
struct TcpSocketCreation {
99100
control: SharedTcpConnectionControl,
@@ -122,7 +123,7 @@ impl TcpConnection {
122123
let send_buffer_size = tcp_opts.send_buffer_size.unwrap_or(DEFAULT_TCP_SEND_BUFFER_SIZE);
123124
let recv_buffer_size = tcp_opts.recv_buffer_size.unwrap_or(DEFAULT_TCP_RECV_BUFFER_SIZE);
124125

125-
let control = Arc::new(ParkingMutex::new(TcpSocketControl {
126+
let control = Arc::new(SpinMutex::new(TcpSocketControl {
126127
send_buffer: RingBuffer::new(vec![0u8; send_buffer_size as usize]),
127128
send_waker: None,
128129
recv_buffer: RingBuffer::new(vec![0u8; recv_buffer_size as usize]),
@@ -164,14 +165,9 @@ impl AsyncRead for TcpConnection {
164165
return Poll::Pending;
165166
}
166167

167-
while !control.recv_buffer.is_empty() {
168-
let recv_buf = unsafe { mem::transmute::<_, &mut [u8]>(buf.unfilled_mut()) };
169-
if recv_buf.is_empty() {
170-
break;
171-
}
172-
let n = control.recv_buffer.dequeue_slice(recv_buf);
173-
buf.advance(n);
174-
}
168+
let recv_buf = unsafe { mem::transmute::<_, &mut [u8]>(buf.unfilled_mut()) };
169+
let n = control.recv_buffer.dequeue_slice(recv_buf);
170+
buf.advance(n);
175171

176172
self.manager_notify.notify();
177173
Ok(()).into()
@@ -403,7 +399,7 @@ impl TcpTun {
403399

404400
let next_duration = iface
405401
.poll_delay(SmolInstant::now())
406-
.unwrap_or(SmolDuration::from_millis(50));
402+
.unwrap_or(SmolDuration::from_millis(1));
407403

408404
if next_duration.total_millis() != 0 {
409405
manager_notify.wait(Duration::from(next_duration));

0 commit comments

Comments
 (0)