Skip to content

Commit

Permalink
Detect and remove (deleted) suffix from hq binary path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Dec 19, 2024
1 parent 7c59fbe commit 24345e3
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions crates/hyperqueue/src/server/autoalloc/queue/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub struct ExternalHandler {

impl ExternalHandler {
pub fn new(server_directory: PathBuf, name: Option<String>) -> anyhow::Result<Self> {
let hq_path = std::env::current_exe().context("Cannot get HyperQueue path")?;
let hq_path =
normalize_exe_path(std::env::current_exe().context("Cannot get HyperQueue path")?);
Ok(Self {
server_directory,
hq_path,
Expand All @@ -40,6 +41,21 @@ impl ExternalHandler {
}
}

/// For some reason, Linux sometimes thinks that the current executable has been deleted,
/// and adds a ` (deleted)` suffix to its path.
/// That is quite annoying, so we get rid of that suffix.
/// See the following issues for more context:
/// - https://github.com/It4innovations/hyperqueue/issues/791
/// - https://github.com/It4innovations/hyperqueue/issues/452
fn normalize_exe_path(mut path: PathBuf) -> PathBuf {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if let Some(name) = name.to_string().strip_suffix(" (deleted)") {
path.set_file_name(name);
}
}
path
}

pub fn create_allocation_dir(
server_directory: PathBuf,
id: QueueId,
Expand Down Expand Up @@ -187,7 +203,8 @@ pub fn wrap_worker_cmd(

#[cfg(test)]
mod tests {
use crate::server::autoalloc::queue::common::wrap_worker_cmd;
use crate::server::autoalloc::queue::common::{normalize_exe_path, wrap_worker_cmd};
use std::path::PathBuf;

#[test]
fn wrap_cmd_noop() {
Expand Down Expand Up @@ -220,4 +237,12 @@ mod tests {
"init.sh && foo bar; unload.sh".to_string()
);
}

#[test]
fn normalize_deleted_path() {
assert_eq!(
normalize_exe_path(PathBuf::from("/a/b/c/hq (deleted)"),),
PathBuf::from("/a/b/c/hq")
);
}
}

0 comments on commit 24345e3

Please sign in to comment.