Skip to content

Commit

Permalink
chore: improve client health checking (windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Oct 9, 2024
1 parent 699f5c6 commit 317a662
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/els/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ molc = { version = "0.3" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.85"
lsp-types = { version = "0.93.2", features = ["proposed"] }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
[target.'cfg(windows)'.dependencies]
windows = { version = "0.58", features = ["Win32_System_Threading"] }

[dev-dependencies]
erg_proc_macros = { workspace = true }
Expand Down
38 changes: 30 additions & 8 deletions crates/els/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,37 @@ use crate::server::{DefaultFeatures, ELSResult, RedirectableStdout, Server};
use crate::server::{ASK_AUTO_SAVE_ID, HEALTH_CHECKER_ID};
use crate::util::{self, NormalizedUrl};

pub fn is_parent_alive(parent_pid: i32) -> bool {
#[cfg(unix)]
pub fn is_process_alive(pid: i32) -> bool {
unsafe {
// sig 0: check if the process exists
let alive = libc::kill(parent_pid, 0);
let alive = libc::kill(pid, 0);
alive == 0
}
}
#[cfg(windows)]
pub fn is_process_alive(pid: i32) -> bool {
unsafe {
use windows::Win32::System::Threading::{
GetExitCodeProcess, OpenProcess, PROCESS_QUERY_INFORMATION,
};

const STILL_ACTIVE: u32 = 0x103u32;

let Ok(handle) = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid as u32) else {
return false;
};
let mut code = 0;
let Ok(_) = GetExitCodeProcess(handle, &mut code) else {
return false;
};
code == STILL_ACTIVE
}
}
#[cfg(all(not(windows), not(unix)))]
pub fn is_process_alive(_pid: i32) -> bool {
false
}

#[derive(Debug)]
pub enum BuildASTError {
Expand Down Expand Up @@ -531,6 +555,9 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
if self.stdout_redirect.is_some() {
return;
}
let Some(client_pid) = self.init_params.process_id.map(|x| x as i32) else {
return;
};
let _self = self.clone();
// FIXME: close this thread when the server is restarted
spawn_new_thread(
Expand All @@ -554,11 +581,6 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
},
"start_client_health_checker_sender",
);
let parent_pid = self
.init_params
.process_id
.map(|x| x as i32)
.unwrap_or_else(|| unsafe { libc::getppid() });
spawn_new_thread(
move || {
loop {
Expand All @@ -575,7 +597,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
// _log!(self, "Restart the server");
// send_error_info("Something went wrong, ELS has been restarted").unwrap();
// self_.restart();
if !is_parent_alive(parent_pid) {
if !is_process_alive(client_pid) {
lsp_log!("Client seems to be dead");
panic!("Client seems to be dead");
}
Expand Down

0 comments on commit 317a662

Please sign in to comment.