fix(windows): make sure the worker thread for wintun shuts down cleanly (#2971)

This thread will go away when I change it to non-blocking, but for now
it was causing multiple sign ins during the same run of the client app
to fail.
This commit is contained in:
Reactor Scram
2023-12-20 14:17:55 -06:00
committed by GitHub
parent 7e6f2621b4
commit f4ffebda75

View File

@@ -11,7 +11,7 @@ use std::{
};
use tokio::sync::mpsc;
// TODO: Make sure all these get dropped gracefully on disconnect
// TODO: Double-check that all these get dropped gracefully on disconnect
pub struct Tun {
_adapter: Arc<wintun::Adapter>,
/// The index of our network adapter, we can use this when asking Windows to add / remove routes / DNS rules
@@ -23,6 +23,14 @@ pub struct Tun {
session: Arc<wintun::Session>,
}
impl Drop for Tun {
fn drop(&mut self) {
if let Err(e) = self.session.shutdown() {
tracing::error!("wintun::Session::shutdown: {e:#?}");
}
}
}
// Hides Powershell's console on Windows
// <https://stackoverflow.com/questions/59692146/is-it-possible-to-use-the-standard-library-to-spawn-a-process-without-showing-th#60958956>
const CREATE_NO_WINDOW: u32 = 0x08000000;
@@ -170,9 +178,22 @@ fn start_recv_thread(
session: Arc<wintun::Session>,
) -> std::thread::JoinHandle<()> {
std::thread::spawn(move || {
while let Ok(pkt) = session.receive_blocking() {
// TODO: Don't allocate here if we can help it
packet_tx.blocking_send(pkt).unwrap();
loop {
match session.receive_blocking() {
Ok(pkt) => {
// TODO: Don't allocate here if we can help it
if packet_tx.blocking_send(pkt).is_err() {
// Most likely the receiver was dropped and we're closing down the connlib session.
break;
}
}
// We get an Error::String when `Session::shutdown` is called
Err(wintun::Error::String(_)) => break,
Err(e) => {
tracing::error!("wintun::Session::receive_blocking: {e:#?}");
break;
}
}
}
tracing::debug!("recv_task exiting gracefully");
})