From e490bc7829f6bb0c049a8668ea7d8f34c7d9ae04 Mon Sep 17 00:00:00 2001
From: ssrlive <30760636+ssrlive@users.noreply.github.com>
Date: Tue, 9 Sep 2025 13:47:11 +0800
Subject: [PATCH] Fix process stuck issue while exiting
---
src/bin/main.rs | 19 +++++++------
src/general_api.rs | 68 ++++++++++++++++++++++++++++------------------
src/lib.rs | 2 ++
src/win_svc.rs | 17 ++++++------
4 files changed, 63 insertions(+), 43 deletions(-)
diff --git a/src/bin/main.rs b/src/bin/main.rs
index 0c6e89e..192abf9 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -24,7 +24,16 @@ fn main() -> Result<(), BoxError> {
}
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
- rt.block_on(main_async(args))
+ rt.block_on(async move {
+ let res = main_async(args).await;
+ let _h = tokio::spawn(async move {
+ // Delay some seconds then try to exit current process if not exited yet, normally this case should not happen
+ tokio::time::sleep(std::time::Duration::from_secs(tun2proxy::FORCE_EXIT_TIMEOUT)).await;
+ log::info!("Forcing exit now.");
+ std::process::exit(-1);
+ });
+ res
+ })
}
fn setup_logging(args: &Args) {
@@ -78,7 +87,7 @@ async fn main_async(args: Args) -> Result<(), BoxError> {
true
})?;
- let tasks = main_loop_handle.await??;
+ let _tasks = main_loop_handle.await??;
if ctrlc_fired.load(std::sync::atomic::Ordering::SeqCst) {
log::info!("Ctrl-C fired, waiting the handler to finish...");
@@ -89,12 +98,6 @@ async fn main_async(args: Args) -> Result<(), BoxError> {
}
}
- if args.exit_on_fatal_error && tasks >= args.max_sessions {
- // Because `main_async` function perhaps stuck in `await` state, so we need to exit the process forcefully
- log::info!("Internal fatal error, max sessions reached ({tasks}/{})", args.max_sessions);
- std::process::exit(-1);
- }
-
Ok(())
}
diff --git a/src/general_api.rs b/src/general_api.rs
index e713409..bdf0371 100644
--- a/src/general_api.rs
+++ b/src/general_api.rs
@@ -4,8 +4,6 @@ use crate::{
};
use std::os::raw::{c_char, c_int, c_ushort};
-static TUN_QUIT: std::sync::Mutex