From f2bf56a7773b3f15653dffdadf3b1085083ddbcd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 4 Apr 2024 01:12:39 +1100 Subject: [PATCH] chore(connlib): remove `Mutex` from windows TUN device (#4472) This is a legacy from before the single-threaded connlib design. --- rust/connlib/tunnel/src/device_channel/tun_windows.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/rust/connlib/tunnel/src/device_channel/tun_windows.rs b/rust/connlib/tunnel/src/device_channel/tun_windows.rs index c9151b8e9..f619efa91 100644 --- a/rust/connlib/tunnel/src/device_channel/tun_windows.rs +++ b/rust/connlib/tunnel/src/device_channel/tun_windows.rs @@ -31,8 +31,7 @@ pub struct Tun { /// The index of our network adapter, we can use this when asking Windows to add / remove routes / DNS rules /// It's stable across app restarts and I'm assuming across system reboots too. iface_idx: u32, - // TODO: Get rid of this mutex. It's a hack to deal with `poll_read` taking a `&self` instead of `&mut self` - packet_rx: std::sync::Mutex>, + packet_rx: mpsc::Receiver, _recv_thread: std::thread::JoinHandle<()>, session: Arc, routes: HashSet, @@ -93,7 +92,6 @@ impl Tun { let (packet_tx, packet_rx) = mpsc::channel(5); let recv_thread = start_recv_thread(packet_tx, Arc::clone(&session))?; - let packet_rx = std::sync::Mutex::new(packet_rx); Ok(Self { adapter, @@ -206,10 +204,8 @@ impl Tun { Ok(()) } - pub fn poll_read(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll> { - let mut packet_rx = self.packet_rx.try_lock().unwrap(); - - let pkt = ready!(packet_rx.poll_recv(cx)); + pub fn poll_read(&mut self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll> { + let pkt = ready!(self.packet_rx.poll_recv(cx)); match pkt { Some(pkt) => {