fix(sort): always install SIGINT handler to ensure temp dir cleanup

The `should_install_signal_handler()` check added in 87c332c72 skipped
handler installation on systems with many open FDs, meaning Ctrl+C left
`/tmp/uutils_sortXXXX` directories behind with no cleanup at all.

Now the handler is always attempted; `ctrlc::set_handler` failing
naturally is sufficient. Failure is treated as non-fatal.

Closes: #11728
This commit is contained in:
mattsu
2026-04-18 20:16:13 +02:00
committed by Sylvestre Ledru
parent 47c17df8b7
commit c1631da43f
+5 -14
View File
@@ -18,7 +18,7 @@ use uucore::error::UResult;
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use uucore::{error::USimpleError, show_error, translate};
use crate::{SortError, current_open_fd_count, fd_soft_limit};
use crate::SortError;
/// A wrapper around [`TempDir`] that may only exist once in a process.
///
@@ -44,16 +44,6 @@ struct HandlerRegistration {
static HANDLER_STATE: LazyLock<Arc<Mutex<HandlerRegistration>>> =
LazyLock::new(|| Arc::new(Mutex::new(HandlerRegistration::default())));
fn should_install_signal_handler() -> bool {
const CTRL_C_FDS: usize = 2;
const RESERVED_FOR_MERGE: usize = 3; // temp output + minimum inputs
let Some(limit) = fd_soft_limit() else {
return true;
};
let open_fds = current_open_fd_count().unwrap_or(3);
open_fds.saturating_add(CTRL_C_FDS + RESERVED_FOR_MERGE) <= limit
}
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
fn ensure_signal_handler_installed(state: Arc<Mutex<HandlerRegistration>>) -> UResult<()> {
// This shared state must originate from `HANDLER_STATE` so the handler always sees
@@ -140,9 +130,10 @@ impl TmpDirWrapper {
guard.path = Some(path);
}
if should_install_signal_handler() {
ensure_signal_handler_installed(state)?;
}
// Always attempt to install the signal handler so that Ctrl+C
// triggers cleanup. Failure is non-fatal: sort still works,
// just without SIGINT-triggered temp directory removal.
let _ = ensure_signal_handler_installed(state);
Ok(())
}