Skip to content

Commit

Permalink
fix(main): Gracefully handle exit signals
Browse files Browse the repository at this point in the history
Instead of letting the kernel handle the shutdown,
listen to the SIGINT, SIGTERM and SIGHUP signals
and shutdown the daemon gracefully.
  • Loading branch information
danyspin97 committed Nov 13, 2024
1 parent 3578597 commit 7865433
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ wayland-egl = "0.32.4"
khronos-egl = { version = "6.0.0", features = [ "static" ] }
format-bytes = "0.3.0"
tikv-jemallocator = "0.6.0"
ctrlc = { version = "3.4.2", features = ["termination"] }

[build-dependencies]
clap = { version = "4.5.20", features = ["derive", "cargo"] }
Expand Down
21 changes: 21 additions & 0 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,28 @@ fn run(opts: Opts, xdg_dirs: BaseDirectories) -> Result<()> {
}
}

let (ctrlc_ping, ctrl_ping_source) = calloop::ping::make_ping()?;
let should_exit = Arc::new(AtomicBool::new(false));
let should_exit_clone = should_exit.clone();
// Handle SIGINT, SIGTERM, and SIGHUP, so that the application can stop nicely
ctrlc::set_handler(move || {
// Just wake up the event loop. The actual exit will be handled by the main loop
// The event loop callback will set should_exit to true
ctrlc_ping.ping();
})
.expect("Error setting Ctrl-C handler");
event_loop
.handle()
.insert_source(ctrl_ping_source, move |_, _, _| {
should_exit_clone.store(true, Ordering::Release);
})
.map_err(|e| anyhow!("inserting the filelist event listener in the event loop: {e}"))?;

loop {
if should_exit.load(Ordering::Acquire) {
break Ok(());
}

// If the config has been modified, this value will return true
if wpaperd
.config
Expand Down

0 comments on commit 7865433

Please sign in to comment.