@@ -34,13 +34,37 @@ use crate::server::{DefaultFeatures, ELSResult, RedirectableStdout, Server};
34
34
use crate :: server:: { ASK_AUTO_SAVE_ID , HEALTH_CHECKER_ID } ;
35
35
use crate :: util:: { self , NormalizedUrl } ;
36
36
37
- pub fn is_parent_alive ( parent_pid : i32 ) -> bool {
37
+ #[ cfg( unix) ]
38
+ pub fn is_process_alive ( pid : i32 ) -> bool {
38
39
unsafe {
39
40
// sig 0: check if the process exists
40
- let alive = libc:: kill ( parent_pid , 0 ) ;
41
+ let alive = libc:: kill ( pid , 0 ) ;
41
42
alive == 0
42
43
}
43
44
}
45
+ #[ cfg( windows) ]
46
+ pub fn is_process_alive ( pid : i32 ) -> bool {
47
+ unsafe {
48
+ use windows:: Win32 :: System :: Threading :: {
49
+ GetExitCodeProcess , OpenProcess , PROCESS_QUERY_INFORMATION ,
50
+ } ;
51
+
52
+ const STILL_ACTIVE : u32 = 0x103u32 ;
53
+
54
+ let Ok ( handle) = OpenProcess ( PROCESS_QUERY_INFORMATION , false , pid as u32 ) else {
55
+ return false ;
56
+ } ;
57
+ let mut code = 0 ;
58
+ let Ok ( _) = GetExitCodeProcess ( handle, & mut code) else {
59
+ return false ;
60
+ } ;
61
+ code == STILL_ACTIVE
62
+ }
63
+ }
64
+ #[ cfg( all( not( windows) , not( unix) ) ) ]
65
+ pub fn is_process_alive ( _pid : i32 ) -> bool {
66
+ false
67
+ }
44
68
45
69
#[ derive( Debug ) ]
46
70
pub enum BuildASTError {
@@ -531,6 +555,9 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
531
555
if self . stdout_redirect . is_some ( ) {
532
556
return ;
533
557
}
558
+ let Some ( client_pid) = self . init_params . process_id . map ( |x| x as i32 ) else {
559
+ return ;
560
+ } ;
534
561
let _self = self . clone ( ) ;
535
562
// FIXME: close this thread when the server is restarted
536
563
spawn_new_thread (
@@ -554,11 +581,6 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
554
581
} ,
555
582
"start_client_health_checker_sender" ,
556
583
) ;
557
- let parent_pid = self
558
- . init_params
559
- . process_id
560
- . map ( |x| x as i32 )
561
- . unwrap_or_else ( || unsafe { libc:: getppid ( ) } ) ;
562
584
spawn_new_thread (
563
585
move || {
564
586
loop {
@@ -575,7 +597,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
575
597
// _log!(self, "Restart the server");
576
598
// send_error_info("Something went wrong, ELS has been restarted").unwrap();
577
599
// self_.restart();
578
- if !is_parent_alive ( parent_pid ) {
600
+ if !is_process_alive ( client_pid ) {
579
601
lsp_log ! ( "Client seems to be dead" ) ;
580
602
panic ! ( "Client seems to be dead" ) ;
581
603
}
0 commit comments