From 50f1e9a5fafa4863998ece8a7e354cbe10aa8240 Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Wed, 10 Aug 2022 12:15:27 +0300 Subject: [PATCH 1/3] sort: fix test_tmp_files_deleted_on_sigint, wait for signal handling at the end of the program --- src/uu/sort/src/sort.rs | 4 +++- src/uu/sort/src/tmp_dir.rs | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 86347a28a3b..c723502287b 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1267,7 +1267,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { settings.init_precomputed(); - exec(&mut files, &settings, output, &mut tmp_dir) + let result = exec(&mut files, &settings, output, &mut tmp_dir); + tmp_dir.wait_if_signal(); + result } pub fn uu_app<'a>() -> Command<'a> { diff --git a/src/uu/sort/src/tmp_dir.rs b/src/uu/sort/src/tmp_dir.rs index 32ffbbf0d4e..f96d2620ad9 100644 --- a/src/uu/sort/src/tmp_dir.rs +++ b/src/uu/sort/src/tmp_dir.rs @@ -69,6 +69,10 @@ impl TmpDirWrapper { path, )) } + + pub fn wait_if_signal(&self) { + let _ = self.lock.lock().unwrap(); + } } /// Remove the directory at `path` by deleting its child files and then itself. From e43872d4c76eb9f7f13a75eb316d3cf50b519f2c Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Wed, 10 Aug 2022 12:20:25 +0300 Subject: [PATCH 2/3] sort: rename lock binding variable --- src/uu/sort/src/tmp_dir.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/sort/src/tmp_dir.rs b/src/uu/sort/src/tmp_dir.rs index f96d2620ad9..62880571766 100644 --- a/src/uu/sort/src/tmp_dir.rs +++ b/src/uu/sort/src/tmp_dir.rs @@ -71,7 +71,7 @@ impl TmpDirWrapper { } pub fn wait_if_signal(&self) { - let _ = self.lock.lock().unwrap(); + let _lock = self.lock.lock().unwrap(); } } From 898be12a33a223fd3bc179ff507fdcb8345a365e Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Wed, 10 Aug 2022 15:31:03 +0300 Subject: [PATCH 3/3] sort: add comments to wait_if_signal function and its usage --- src/uu/sort/src/sort.rs | 2 ++ src/uu/sort/src/tmp_dir.rs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index c723502287b..7a0ac21d2eb 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1268,6 +1268,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { settings.init_precomputed(); let result = exec(&mut files, &settings, output, &mut tmp_dir); + // Wait here if `SIGINT` was received, + // for signal handler to do its work and terminate the program. tmp_dir.wait_if_signal(); result } diff --git a/src/uu/sort/src/tmp_dir.rs b/src/uu/sort/src/tmp_dir.rs index 62880571766..3fc405244be 100644 --- a/src/uu/sort/src/tmp_dir.rs +++ b/src/uu/sort/src/tmp_dir.rs @@ -45,7 +45,8 @@ impl TmpDirWrapper { let path = self.temp_dir.as_ref().unwrap().path().to_owned(); let lock = self.lock.clone(); ctrlc::set_handler(move || { - // Take the lock so that `next_file_path` returns no new file path. + // Take the lock so that `next_file_path` returns no new file path, + // and the program doesn't terminate before the handler has finished let _lock = lock.lock().unwrap(); if let Err(e) = remove_tmp_dir(&path) { show_error!("failed to delete temporary directory: {}", e); @@ -70,6 +71,7 @@ impl TmpDirWrapper { )) } + /// Function just waits if signal handler was called pub fn wait_if_signal(&self) { let _lock = self.lock.lock().unwrap(); }