Skip to content

Commit c1631da

Browse files
mattsu2020sylvestre
authored andcommitted
fix(sort): always install SIGINT handler to ensure temp dir cleanup
The `should_install_signal_handler()` check added in 87c332c 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
1 parent 47c17df commit c1631da

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

src/uu/sort/src/tmp_dir.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use uucore::error::UResult;
1818
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
1919
use uucore::{error::USimpleError, show_error, translate};
2020

21-
use crate::{SortError, current_open_fd_count, fd_soft_limit};
21+
use crate::SortError;
2222

2323
/// A wrapper around [`TempDir`] that may only exist once in a process.
2424
///
@@ -44,16 +44,6 @@ struct HandlerRegistration {
4444
static HANDLER_STATE: LazyLock<Arc<Mutex<HandlerRegistration>>> =
4545
LazyLock::new(|| Arc::new(Mutex::new(HandlerRegistration::default())));
4646

47-
fn should_install_signal_handler() -> bool {
48-
const CTRL_C_FDS: usize = 2;
49-
const RESERVED_FOR_MERGE: usize = 3; // temp output + minimum inputs
50-
let Some(limit) = fd_soft_limit() else {
51-
return true;
52-
};
53-
let open_fds = current_open_fd_count().unwrap_or(3);
54-
open_fds.saturating_add(CTRL_C_FDS + RESERVED_FOR_MERGE) <= limit
55-
}
56-
5747
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
5848
fn ensure_signal_handler_installed(state: Arc<Mutex<HandlerRegistration>>) -> UResult<()> {
5949
// This shared state must originate from `HANDLER_STATE` so the handler always sees
@@ -140,9 +130,10 @@ impl TmpDirWrapper {
140130
guard.path = Some(path);
141131
}
142132

143-
if should_install_signal_handler() {
144-
ensure_signal_handler_installed(state)?;
145-
}
133+
// Always attempt to install the signal handler so that Ctrl+C
134+
// triggers cleanup. Failure is non-fatal: sort still works,
135+
// just without SIGINT-triggered temp directory removal.
136+
let _ = ensure_signal_handler_installed(state);
146137
Ok(())
147138
}
148139

0 commit comments

Comments
 (0)