11use std:: net:: { TcpStream , ToSocketAddrs } ;
22use std:: path:: PathBuf ;
3+ #[ cfg( target_family = "unix" ) ]
34use std:: process:: Command ;
45use std:: sync:: Arc ;
56use std:: sync:: atomic:: { AtomicBool , Ordering } ;
@@ -105,11 +106,7 @@ pub async fn run_foreground(config_path: Option<PathBuf>, with_setup: bool) -> R
105106 state:: mark_running ( & paths, pid) ?;
106107 let ui_managed_shutdown = Arc :: new ( AtomicBool :: new ( false ) ) ;
107108 let ( shutdown_tx, shutdown_rx) = watch:: channel ( false ) ;
108- let watchdog = spawn_ui_lease_watchdog (
109- paths. clone ( ) ,
110- ui_managed_shutdown. clone ( ) ,
111- shutdown_tx,
112- ) ;
109+ let watchdog = spawn_ui_lease_watchdog ( paths. clone ( ) , ui_managed_shutdown. clone ( ) , shutdown_tx) ;
113110 let result = run_proxy ( config. clone ( ) , paths. clone ( ) , bundle, shutdown_rx) . await ;
114111 watchdog. abort ( ) ;
115112 let _ = state:: clear_pid_if_matches ( & paths, pid) ;
@@ -193,9 +190,7 @@ pub fn helper_start(config_path: Option<PathBuf>) -> Result<()> {
193190 log_warn (
194191 & paths,
195192 "helper-start" ,
196- & format ! (
197- "启动流程报告异常,但检测到现有服务仍在运行,已修复状态:{error:#}"
198- ) ,
193+ & format ! ( "启动流程报告异常,但检测到现有服务仍在运行,已修复状态:{error:#}" ) ,
199194 ) ;
200195 return Ok ( ( ) ) ;
201196 }
@@ -466,6 +461,7 @@ fn wait_until_running(paths: &AppPaths, config: &AppConfig, timeout: Duration) -
466461 bail ! ( "daemon start timed out" )
467462}
468463
464+ #[ cfg_attr( not( target_family = "unix" ) , allow( unused_variables) ) ]
469465fn discover_running_daemon_pid ( paths : & AppPaths ) -> Option < u32 > {
470466 #[ cfg( target_family = "unix" ) ]
471467 {
@@ -727,8 +723,18 @@ fn spawn_ui_lease_watchdog(
727723 shutdown_tx : watch:: Sender < bool > ,
728724) -> tokio:: task:: JoinHandle < ( ) > {
729725 tokio:: spawn ( async move {
730- if state:: read_ui_lease ( & paths) . ok ( ) . flatten ( ) . is_none ( ) {
731- return ;
726+ match state:: read_ui_lease ( & paths) {
727+ Ok ( Some ( _) ) => { }
728+ Ok ( None ) => return ,
729+ Err ( error) => {
730+ let _ = runtime_log:: append (
731+ & paths,
732+ "WARN" ,
733+ "ui-watchdog" ,
734+ & format ! ( "failed to read initial ui lease state: {error:#}" ) ,
735+ ) ;
736+ return ;
737+ }
732738 }
733739 let stale_after = Duration :: from_secs ( 8 ) ;
734740 let mut missing_since: Option < u64 > = None ;
@@ -739,31 +745,43 @@ fn spawn_ui_lease_watchdog(
739745 . duration_since ( UNIX_EPOCH )
740746 . map ( |duration| duration. as_secs ( ) )
741747 . unwrap_or ( 0 ) ;
742- let Some ( lease) = state:: read_ui_lease ( & paths) . ok ( ) . flatten ( ) else {
743- let first_missing_at = * missing_since. get_or_insert ( now) ;
744- let missing_for = now. saturating_sub ( first_missing_at) ;
745- if missing_for < stale_after. as_secs ( ) {
748+ let lease = match state:: read_ui_lease ( & paths) {
749+ Ok ( Some ( lease) ) => lease,
750+ Ok ( None ) => {
751+ let first_missing_at = * missing_since. get_or_insert ( now) ;
752+ let missing_for = now. saturating_sub ( first_missing_at) ;
753+ if missing_for < stale_after. as_secs ( ) {
754+ continue ;
755+ }
756+ ui_managed_shutdown. store ( true , Ordering :: SeqCst ) ;
757+ let _ = runtime_log:: append (
758+ & paths,
759+ "WARN" ,
760+ "ui-watchdog" ,
761+ & format ! (
762+ "ui lease missing for {}s while daemon is ui-managed; requesting shutdown" ,
763+ missing_for
764+ ) ,
765+ ) ;
766+ let _ = shutdown_tx. send ( true ) ;
767+ break ;
768+ }
769+ Err ( error) => {
770+ let _ = runtime_log:: append (
771+ & paths,
772+ "WARN" ,
773+ "ui-watchdog" ,
774+ & format ! ( "failed to read ui lease state, keeping daemon alive: {error:#}" ) ,
775+ ) ;
746776 continue ;
747777 }
748- ui_managed_shutdown. store ( true , Ordering :: SeqCst ) ;
749- let _ = runtime_log:: append (
750- & paths,
751- "WARN" ,
752- "ui-watchdog" ,
753- & format ! (
754- "ui lease missing for {}s while daemon is ui-managed; requesting shutdown" ,
755- missing_for
756- ) ,
757- ) ;
758- let _ = shutdown_tx. send ( true ) ;
759- break ;
760778 } ;
761779 missing_since = None ;
762780
763781 let now = now. max ( lease. updated_at ) ;
764782 let stale = now. saturating_sub ( lease. updated_at ) >= stale_after. as_secs ( ) ;
765- let owner_dead = ! is_process_running ( lease . owner_pid ) ;
766- if stale || owner_dead {
783+ if stale {
784+ let owner_dead = ! is_process_running ( lease . owner_pid ) ;
767785 ui_managed_shutdown. store ( true , Ordering :: SeqCst ) ;
768786 let _ = runtime_log:: append (
769787 & paths,
0 commit comments