Skip to content

Commit

Permalink
Exit gracefully on termination signals (helix-editor#7236)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis authored and Schuyler Mortimer committed Jul 10, 2024
1 parent 7362f2d commit 17e3a3f
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,14 @@ impl Application {
#[cfg(windows)]
let signals = futures_util::stream::empty();
#[cfg(not(windows))]
let signals = Signals::new([signal::SIGTSTP, signal::SIGCONT, signal::SIGUSR1])
.context("build signal handler")?;
let signals = Signals::new([
signal::SIGTSTP,
signal::SIGCONT,
signal::SIGUSR1,
signal::SIGTERM,
signal::SIGINT,
])
.context("build signal handler")?;

let app = Self {
compositor,
Expand Down Expand Up @@ -322,7 +328,9 @@ impl Application {
biased;

Some(signal) = self.signals.next() => {
self.handle_signals(signal).await;
if !self.handle_signals(signal).await {
return false;
};
}
Some(event) = input_stream.next() => {
self.handle_terminal_events(event).await;
Expand Down Expand Up @@ -446,10 +454,12 @@ impl Application {

#[cfg(windows)]
// no signal handling available on windows
pub async fn handle_signals(&mut self, _signal: ()) {}
pub async fn handle_signals(&mut self, _signal: ()) -> bool {
true
}

#[cfg(not(windows))]
pub async fn handle_signals(&mut self, signal: i32) {
pub async fn handle_signals(&mut self, signal: i32) -> bool {
match signal {
signal::SIGTSTP => {
self.restore_term().unwrap();
Expand Down Expand Up @@ -503,8 +513,14 @@ impl Application {
self.refresh_config();
self.render().await;
}
signal::SIGTERM | signal::SIGINT => {
self.restore_term().unwrap();
return false;
}
_ => unreachable!(),
}

true
}

pub async fn handle_idle_timeout(&mut self) {
Expand Down

0 comments on commit 17e3a3f

Please sign in to comment.