diff --git a/src/desktop_api.rs b/src/desktop_api.rs index 5d8bb69..0e85803 100644 --- a/src/desktop_api.rs +++ b/src/desktop_api.rs @@ -74,11 +74,6 @@ pub unsafe extern "C" fn tun2proxy_with_name_run( }, }; - // release shutdown token before exit. - if let Ok(mut lock) = TUN_QUIT.lock() { - let _ = lock.take(); - } - exit_code } @@ -150,8 +145,8 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can /// Shutdown the tun2proxy component. #[no_mangle] pub unsafe extern "C" fn tun2proxy_with_name_stop() -> c_int { - if let Ok(lock) = TUN_QUIT.lock() { - if let Some(shutdown_token) = lock.as_ref() { + if let Ok(mut lock) = TUN_QUIT.lock() { + if let Some(shutdown_token) = lock.take() { shutdown_token.cancel(); return 0; } diff --git a/src/mobile_api.rs b/src/mobile_api.rs index 6d04390..8e67cef 100644 --- a/src/mobile_api.rs +++ b/src/mobile_api.rs @@ -15,12 +15,16 @@ pub async fn desktop_run_async(_: Args, _: tokio_util::sync::CancellationToken) pub fn mobile_run(args: Args, tun_mtu: u16) -> c_int { let shutdown_token = tokio_util::sync::CancellationToken::new(); { - let mut lock = TUN_QUIT.lock().unwrap(); - if lock.is_some() { - log::error!("tun2proxy already started"); - return -1; + if let Ok(mut lock) = TUN_QUIT.lock() { + if lock.is_some() { + log::error!("tun2proxy already started"); + return -1; + } + *lock = Some(shutdown_token.clone()); + } else { + log::error!("failed to lock tun2proxy quit token"); + return -2; } - *lock = Some(shutdown_token.clone()); } let block = async move { @@ -57,16 +61,12 @@ pub fn mobile_run(args: Args, tun_mtu: u16) -> c_int { }, }; - // release shutdown token before exit. - let mut lock = TUN_QUIT.lock().unwrap(); - let _ = lock.take(); - exit_code } pub fn mobile_stop() -> c_int { - if let Ok(lock) = TUN_QUIT.lock() { - if let Some(shutdown_token) = lock.as_ref() { + if let Ok(mut lock) = TUN_QUIT.lock() { + if let Some(shutdown_token) = lock.take() { shutdown_token.cancel(); return 0; }