@@ -10,6 +10,7 @@ use crate::config::Config;
1010/// and cleans up stale resources.
1111pub async fn run ( db : StateStore , cfg : Config ) -> Result < ( ) > {
1212 tracing:: info!( "ECC daemon started" ) ;
13+ resume_crashed_sessions ( & db) ?;
1314
1415 let heartbeat_interval = Duration :: from_secs ( cfg. heartbeat_interval_secs ) ;
1516 let timeout = Duration :: from_secs ( cfg. session_timeout_secs ) ;
@@ -23,6 +24,43 @@ pub async fn run(db: StateStore, cfg: Config) -> Result<()> {
2324 }
2425}
2526
27+ pub fn resume_crashed_sessions ( db : & StateStore ) -> Result < ( ) > {
28+ let failed_sessions = resume_crashed_sessions_with ( db, pid_is_alive) ?;
29+ if failed_sessions > 0 {
30+ tracing:: warn!( "Marked {failed_sessions} crashed sessions as failed during daemon startup" ) ;
31+ }
32+ Ok ( ( ) )
33+ }
34+
35+ fn resume_crashed_sessions_with < F > ( db : & StateStore , is_pid_alive : F ) -> Result < usize >
36+ where
37+ F : Fn ( u32 ) -> bool ,
38+ {
39+ let sessions = db. list_sessions ( ) ?;
40+ let mut failed_sessions = 0 ;
41+
42+ for session in sessions {
43+ if session. state != SessionState :: Running {
44+ continue ;
45+ }
46+
47+ let is_alive = session. pid . is_some_and ( & is_pid_alive) ;
48+ if is_alive {
49+ continue ;
50+ }
51+
52+ tracing:: warn!(
53+ "Session {} was left running with stale pid {:?}; marking it failed" ,
54+ session. id,
55+ session. pid
56+ ) ;
57+ db. update_state_and_pid ( & session. id , & SessionState :: Failed , None ) ?;
58+ failed_sessions += 1 ;
59+ }
60+
61+ Ok ( failed_sessions)
62+ }
63+
2664fn check_sessions ( db : & StateStore , timeout : Duration ) -> Result < ( ) > {
2765 let sessions = db. list_sessions ( ) ?;
2866
@@ -38,9 +76,102 @@ fn check_sessions(db: &StateStore, timeout: Duration) -> Result<()> {
3876
3977 if elapsed > timeout {
4078 tracing:: warn!( "Session {} timed out after {:?}" , session. id, elapsed) ;
41- db. update_state ( & session. id , & SessionState :: Failed ) ?;
79+ db. update_state_and_pid ( & session. id , & SessionState :: Failed , None ) ?;
4280 }
4381 }
4482
4583 Ok ( ( ) )
4684}
85+
86+ #[ cfg( unix) ]
87+ fn pid_is_alive ( pid : u32 ) -> bool {
88+ if pid == 0 {
89+ return false ;
90+ }
91+
92+ // SAFETY: kill(pid, 0) probes process existence without delivering a signal.
93+ let result = unsafe { libc:: kill ( pid as libc:: pid_t , 0 ) } ;
94+ if result == 0 {
95+ return true ;
96+ }
97+
98+ matches ! (
99+ std:: io:: Error :: last_os_error( ) . raw_os_error( ) ,
100+ Some ( code) if code == libc:: EPERM
101+ )
102+ }
103+
104+ #[ cfg( not( unix) ) ]
105+ fn pid_is_alive ( _pid : u32 ) -> bool {
106+ false
107+ }
108+
109+ #[ cfg( test) ]
110+ mod tests {
111+ use super :: * ;
112+ use crate :: session:: { Session , SessionMetrics , SessionState } ;
113+ use std:: path:: PathBuf ;
114+
115+ fn temp_db_path ( ) -> PathBuf {
116+ std:: env:: temp_dir ( ) . join ( format ! ( "ecc2-daemon-test-{}.db" , uuid:: Uuid :: new_v4( ) ) )
117+ }
118+
119+ fn sample_session ( id : & str , state : SessionState , pid : Option < u32 > ) -> Session {
120+ let now = chrono:: Utc :: now ( ) ;
121+ Session {
122+ id : id. to_string ( ) ,
123+ task : "Recover crashed worker" . to_string ( ) ,
124+ agent_type : "claude" . to_string ( ) ,
125+ state,
126+ pid,
127+ worktree : None ,
128+ created_at : now,
129+ updated_at : now,
130+ metrics : SessionMetrics :: default ( ) ,
131+ }
132+ }
133+
134+ #[ test]
135+ fn resume_crashed_sessions_marks_dead_running_sessions_failed ( ) -> Result < ( ) > {
136+ let path = temp_db_path ( ) ;
137+ let store = StateStore :: open ( & path) ?;
138+ store. insert_session ( & sample_session (
139+ "deadbeef" ,
140+ SessionState :: Running ,
141+ Some ( 4242 ) ,
142+ ) ) ?;
143+
144+ resume_crashed_sessions_with ( & store, |_| false ) ?;
145+
146+ let session = store
147+ . get_session ( "deadbeef" ) ?
148+ . expect ( "session should still exist" ) ;
149+ assert_eq ! ( session. state, SessionState :: Failed ) ;
150+ assert_eq ! ( session. pid, None ) ;
151+
152+ let _ = std:: fs:: remove_file ( path) ;
153+ Ok ( ( ) )
154+ }
155+
156+ #[ test]
157+ fn resume_crashed_sessions_keeps_live_running_sessions_running ( ) -> Result < ( ) > {
158+ let path = temp_db_path ( ) ;
159+ let store = StateStore :: open ( & path) ?;
160+ store. insert_session ( & sample_session (
161+ "alive123" ,
162+ SessionState :: Running ,
163+ Some ( 7777 ) ,
164+ ) ) ?;
165+
166+ resume_crashed_sessions_with ( & store, |_| true ) ?;
167+
168+ let session = store
169+ . get_session ( "alive123" ) ?
170+ . expect ( "session should still exist" ) ;
171+ assert_eq ! ( session. state, SessionState :: Running ) ;
172+ assert_eq ! ( session. pid, Some ( 7777 ) ) ;
173+
174+ let _ = std:: fs:: remove_file ( path) ;
175+ Ok ( ( ) )
176+ }
177+ }
0 commit comments