Skip to content

Commit

Permalink
Merge pull request #531 from erg-lang/client-health
Browse files Browse the repository at this point in the history
chore: improve client health checking
  • Loading branch information
mtshiba authored Oct 9, 2024
2 parents e8af24d + 317a662 commit 83c57ed
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/els/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ 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
40 changes: 39 additions & 1 deletion crates/els/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ use crate::server::{DefaultFeatures, ELSResult, RedirectableStdout, Server};
use crate::server::{ASK_AUTO_SAVE_ID, HEALTH_CHECKER_ID};
use crate::util::{self, NormalizedUrl};

#[cfg(unix)]
pub fn is_process_alive(pid: i32) -> bool {
unsafe {
// sig 0: check if the process exists
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 {
NoFile,
Expand Down Expand Up @@ -523,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 Down Expand Up @@ -562,7 +597,10 @@ 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();
panic!("Client health check timed out");
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 83c57ed

Please sign in to comment.