fix(appd): handle SIGTERM for clean shutdown under systemd

run() now registers a SIGTERM handler (unix-only, cfg-gated) alongside
the existing SIGINT handler. Both break the accept loop and allow the
Unix socket to be removed before exit.

On non-Unix targets the SIGTERM arm uses std::future::pending so the
select! shape is unchanged at the type level.
This commit is contained in:
Marco Allegretti 2026-03-11 11:06:01 +01:00
parent 01a4969883
commit a409b954ab

View file

@ -132,9 +132,18 @@ async fn run() -> anyhow::Result<()> {
let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]); let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]);
let mut shutdown = std::pin::pin!(tokio::signal::ctrl_c()); #[cfg(unix)]
let mut sigterm = {
use tokio::signal::unix::{SignalKind, signal};
signal(SignalKind::terminate()).context("SIGTERM handler")?
};
loop { loop {
#[cfg(unix)]
let term = sigterm.recv();
#[cfg(not(unix))]
let term = std::future::pending::<Option<()>>();
tokio::select! { tokio::select! {
result = listener.accept() => { result = listener.accept() => {
let (stream, _) = result.context("accept")?; let (stream, _) = result.context("accept")?;
@ -155,8 +164,12 @@ async fn run() -> anyhow::Result<()> {
} }
}); });
} }
_ = &mut shutdown => { _ = tokio::signal::ctrl_c() => {
tracing::info!("shutting down"); tracing::info!("SIGINT received; shutting down");
break;
}
_ = term => {
tracing::info!("SIGTERM received; shutting down");
break; break;
} }
} }