Skip to content

Commit 852635e

Browse files
simonsanaawsome
andauthored
feat: shut down gracefully with ctrl+c (#1364)
If you have a long-running process, like `webdav` or `backup` or others, pressing CTRL+C usually results in a non-zero exit code: `process didn't exit successfully: P:\CARGO\.cargo-target-win\debug\rustic.exe -P <PROFILE> webdav (exit code: 0xc000013a, STATUS_CONTROL_C_EXIT)` This shouldn't be the case, as it was user initiated and we can shut down gracefully. This PR adds this functionality, so CTRL+C shuts down `rustic` gracefully and exits with a 0 exit-code. --------- Signed-off-by: simonsan <[email protected]> Co-authored-by: aawsome <[email protected]>
1 parent c571279 commit 852635e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ clap = { version = "4", features = ["derive", "env", "wrap_help"] }
103103
clap_complete = "4"
104104
conflate = "0.3.3"
105105
convert_case = "0.6.0"
106+
ctrlc = { version = "3.4.5", features = ["termination"] }
106107
dateparser = "0.2.1"
107108
derive_more = { version = "1", features = ["debug"] }
108109
dialoguer = "0.11.0"

src/commands.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::fmt::Debug;
3535
use std::fs::File;
3636
use std::path::PathBuf;
3737
use std::str::FromStr;
38+
use std::sync::mpsc::channel;
3839

3940
#[cfg(feature = "mount")]
4041
use crate::commands::mount::MountCmd;
@@ -64,7 +65,7 @@ use clap::builder::{
6465
};
6566
use convert_case::{Case, Casing};
6667
use human_panic::setup_panic;
67-
use log::{log, Level};
68+
use log::{info, log, Level};
6869
use simplelog::{CombinedLogger, LevelFilter, TermLogger, TerminalMode, WriteLogger};
6970

7071
use self::find::FindCmd;
@@ -179,6 +180,20 @@ impl Runnable for EntryPoint {
179180
// Set up panic hook for better error messages and logs
180181
setup_panic!();
181182

183+
// Set up Ctrl-C handler
184+
let (tx, rx) = channel();
185+
186+
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
187+
.expect("Error setting Ctrl-C handler");
188+
189+
_ = std::thread::spawn(move || {
190+
// Wait for Ctrl-C
191+
rx.recv().expect("Could not receive from channel.");
192+
info!("Ctrl-C received, shutting down...");
193+
RUSTIC_APP.shutdown(Shutdown::Graceful)
194+
});
195+
196+
// Run the subcommand
182197
self.commands.run();
183198
RUSTIC_APP.shutdown(Shutdown::Graceful)
184199
}

0 commit comments

Comments
 (0)