From 84f8d1c0e33a84ddd5da4b8cd5f928252ce693d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 9 Dec 2024 13:04:42 +0100 Subject: [PATCH] Detect and remove ` (deleted)` suffix from `hq` binary path --- .../src/server/autoalloc/queue/common.rs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/hyperqueue/src/server/autoalloc/queue/common.rs b/crates/hyperqueue/src/server/autoalloc/queue/common.rs index 6b5bc02a7..5020c55c0 100644 --- a/crates/hyperqueue/src/server/autoalloc/queue/common.rs +++ b/crates/hyperqueue/src/server/autoalloc/queue/common.rs @@ -25,7 +25,8 @@ pub struct ExternalHandler { impl ExternalHandler { pub fn new(server_directory: PathBuf, name: Option) -> anyhow::Result { - 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, @@ -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, @@ -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() { @@ -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") + ); + } }