Skip to content

Commit e192f0b

Browse files
committed
Respond to SIGTERM so docker stop works properly
1 parent 1993076 commit e192f0b

1 file changed

Lines changed: 32 additions & 7 deletions

File tree

src/main.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,41 @@ async fn main() -> Result<()> {
174174
let http_server_handle = http_server.handle();
175175
tokio::spawn(http_server);
176176

177-
let ctrl_c = tokio::signal::ctrl_c();
177+
#[cfg(unix)]
178+
let mut sig_term_signal_stream = {
179+
use tokio::signal::unix::{signal, SignalKind};
180+
181+
signal(SignalKind::terminate())?
182+
};
183+
let sig_term = {
184+
#[cfg(unix)]
185+
{
186+
sig_term_signal_stream.recv().fuse()
187+
}
188+
#[cfg(not(unix))]
189+
{
190+
use futures::future::pending;
191+
192+
// On non-Unix systems, just use a dummy never-ready future
193+
// to make the compiler happy below.
194+
pending::<Option<()>>()
195+
}
196+
};
197+
pin_mut!(sig_term);
198+
199+
let ctrl_c = tokio::signal::ctrl_c().fuse();
200+
pin_mut!(ctrl_c);
178201
if shutdown_futures.is_empty() {
179-
ctrl_c.await.context("tokio::signal::ctrl_c failed")?;
202+
futures::select! {
203+
sigint = ctrl_c => sigint.context("tokio::signal::ctrl_c failed")?,
204+
sigterm = sig_term => sigterm.context("tokio::signal::terminate failed")?,
205+
}
180206
} else {
181-
let ctrl_c = ctrl_c.fuse();
182-
pin_mut!(ctrl_c);
183207
#[allow(clippy::mut_mut)]
184208
{
185209
futures::select! {
186-
signal = ctrl_c => signal.context("tokio::signal::ctrl_c failed")?,
210+
sigint = ctrl_c => sigint.context("tokio::signal::ctrl_c failed")?,
211+
sigterm = sig_term => sigterm.context("tokio::signal::terminate failed")?,
187212
either = shutdown_futures.next().fuse() => match either {
188213
Some(Either::Right(result)) => result.context("Background service crashed")?,
189214
_ => unreachable!()
@@ -192,8 +217,8 @@ async fn main() -> Result<()> {
192217
}
193218
}
194219

195-
// Receivers should be live at this point, although both services may have crashed,
196-
// so let's not really assert anything about it.
220+
// Receivers should be live at this point, although either or both of the
221+
// services may have crashed, so let's not really assert anything about it.
197222
info!("Shutting down HTTP server!");
198223
let _ = shutdown_sender.send(());
199224

0 commit comments

Comments
 (0)