From db6a8ee8e4328ff91e88be244f63dde42a093c1f Mon Sep 17 00:00:00 2001 From: mattsu Date: Sat, 18 Apr 2026 08:45:43 +0900 Subject: [PATCH] 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 --- src/uu/sort/src/tmp_dir.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/uu/sort/src/tmp_dir.rs b/src/uu/sort/src/tmp_dir.rs index 0e5bd1f34d4..65b0f10bf55 100644 --- a/src/uu/sort/src/tmp_dir.rs +++ b/src/uu/sort/src/tmp_dir.rs @@ -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>> = 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>) -> 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(()) }