use std::sync::Arc;
use shep_core::paths::ShepPaths;
#[cfg(unix)]
use shep_core::selector::ProcessSelector;
#[cfg(unix)]
use tokio::signal::unix::{SignalKind, signal};
use tokio::sync::{oneshot, watch};
use tokio::task::JoinHandle;
use super::BootError;
#[cfg(unix)]
use super::handover::{HandoverSeam, hand_over_now};
use crate::supervisor::SupervisorHandle;
#[cfg(unix)]
use crate::supervisor::SupervisorError;
#[derive(Debug)]
pub(super) struct SignalTasks {
tasks: Vec<JoinHandle<()>>,
}
impl Drop for SignalTasks {
fn drop(&mut self) {
for task in &self.tasks {
task.abort();
}
}
}
#[cfg(windows)]
pub(super) fn install_signals(
shutdown: Arc<watch::Sender<bool>>,
paths: ShepPaths,
) -> Result<(SignalTasks, oneshot::Sender<SupervisorHandle>), BootError> {
use tokio::signal::windows;
let mut signals = SignalTasks {
tasks: Vec::with_capacity(4),
};
macro_rules! listen {
($ctor:path, $name:literal) => {{
let mut stream = $ctor().map_err(|source| BootError::Io {
path: paths.home.clone(),
source,
})?;
let shutdown = Arc::clone(&shutdown);
signals.tasks.push(tokio::spawn(async move {
let mut already_shutting_down = false;
while stream.recv().await.is_some() {
if already_shutting_down {
tracing::warn!(
signal = $name,
"received a repeat shutdown event while teardown is already \
underway; teardown continues unchanged"
);
} else {
already_shutting_down = true;
}
let _ = shutdown.send(true);
}
}));
}};
}
listen!(windows::ctrl_c, "CTRL_C");
listen!(windows::ctrl_break, "CTRL_BREAK");
listen!(windows::ctrl_close, "CTRL_CLOSE");
listen!(windows::ctrl_shutdown, "CTRL_SHUTDOWN");
let (connect_supervisor, _supervisor_rx) = oneshot::channel::<SupervisorHandle>();
Ok((signals, connect_supervisor))
}
#[cfg(unix)]
pub(super) fn install_signals(
shutdown: Arc<watch::Sender<bool>>,
paths: ShepPaths,
) -> Result<InstalledSignals, BootError> {
let mut signals = SignalTasks {
tasks: Vec::with_capacity(4),
};
for kind in [
SignalKind::terminate(),
SignalKind::interrupt(),
SignalKind::quit(),
] {
let mut stream = signal(kind).map_err(|source| BootError::Io {
path: paths.home.clone(),
source,
})?;
let shutdown = Arc::clone(&shutdown);
signals.tasks.push(tokio::spawn(async move {
let mut already_shutting_down = false;
while stream.recv().await.is_some() {
if already_shutting_down {
tracing::warn!(
?kind,
"received a repeat shutdown signal while teardown is already \
underway; teardown continues unchanged (SIGKILL forces an \
immediate exit)"
);
} else {
already_shutting_down = true;
}
let _ = shutdown.send(true);
}
}));
}
let mut hup = signal(SignalKind::hangup()).map_err(|source| BootError::Io {
path: paths.home.clone(),
source,
})?;
let (connect_handover, handover_rx) = oneshot::channel::<Option<HandoverSeam>>();
let hup_shutdown = Arc::clone(&shutdown);
signals.tasks.push(tokio::spawn(async move {
let seam = handover_rx.await.ok().flatten();
if hup.recv().await.is_some() {
let refusal = match &seam {
Some(seam) => match hand_over_now(seam).await {
Ok(never) => match never {},
Err(refusal) => refusal,
},
None => "this shepherd was not booted with the handover armed".to_string(),
};
tracing::warn!(
%refusal,
"SIGHUP: this flock could not be handed to a successor; stopping gracefully \
instead. This line may be the only record of the reason: a signal carries no \
sender, and the case this gate exists for is a flock that changed between a \
client's question and the signal, where that client was told nothing"
);
let _ = hup_shutdown.send(true);
}
}));
let mut usr2 = signal(SignalKind::user_defined2()).map_err(|source| BootError::Io {
path: paths.home.clone(),
source,
})?;
let (connect_supervisor, supervisor_rx) = oneshot::channel::<SupervisorHandle>();
signals.tasks.push(tokio::spawn(async move {
let Ok(supervisor) = supervisor_rx.await else {
return;
};
while usr2.recv().await.is_some() {
match supervisor.reopen(ProcessSelector::All).await {
Ok(reopened) => tracing::info!(
reopened = reopened.len(),
"SIGUSR2: every sheep's log files reopened"
),
Err(SupervisorError::NotFound) => {
tracing::info!("SIGUSR2: no sheep to reopen");
}
Err(err) => tracing::warn!(%err, "SIGUSR2: log reopen failed"),
}
}
}));
Ok((signals, connect_supervisor, connect_handover))
}
#[cfg(unix)]
type InstalledSignals = (
SignalTasks,
oneshot::Sender<SupervisorHandle>,
oneshot::Sender<Option<HandoverSeam>>,
);
#[cfg(all(test, unix))]
mod tests {
use super::*;
use crate::boot::{BootOptions, SIGNAL_TEST_LOCK, boot_with_idle_sweep, init_dirs};
use crate::fake::{ProcScript, ScriptedRunner};
use crate::testing::{SharedRunner, test_paths};
use shep_core::config::{AppConfig, normalize};
use std::time::Duration;
#[tokio::test]
async fn sigterm_triggers_the_same_graceful_shutdown() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![]),
paths.clone(),
BootOptions::default(),
)
.await
.unwrap();
let run = tokio::spawn(daemon.run());
nix::sys::signal::raise(nix::sys::signal::Signal::SIGTERM).unwrap();
tokio::time::timeout(Duration::from_secs(5), run)
.await
.unwrap()
.unwrap()
.unwrap();
assert!(!paths.socket.exists());
}
#[tokio::test]
async fn sighup_triggers_the_same_graceful_shutdown() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![]),
paths.clone(),
BootOptions::default(),
)
.await
.unwrap();
let run = tokio::spawn(daemon.run());
nix::sys::signal::raise(nix::sys::signal::Signal::SIGHUP).unwrap();
tokio::time::timeout(Duration::from_secs(5), run)
.await
.unwrap()
.unwrap()
.unwrap();
assert!(!paths.socket.exists());
}
#[tokio::test]
async fn a_repeat_sigterm_is_observed_not_swallowed() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let (shutdown, mut shutdown_rx) = watch::channel(false);
let shutdown = Arc::new(shutdown);
let (signals, _connect_supervisor, _connect_handover) =
install_signals(shutdown, paths).unwrap();
nix::sys::signal::raise(nix::sys::signal::Signal::SIGTERM).unwrap();
tokio::time::timeout(Duration::from_secs(5), shutdown_rx.changed())
.await
.unwrap()
.unwrap();
assert!(*shutdown_rx.borrow());
assert!(
!signals.tasks[0].is_finished(),
"the SIGTERM listener must still be polling after its first signal, not have exited"
);
nix::sys::signal::raise(nix::sys::signal::Signal::SIGTERM).unwrap();
tokio::time::timeout(Duration::from_secs(5), shutdown_rx.changed())
.await
.unwrap()
.unwrap();
assert!(
!signals.tasks[0].is_finished(),
"the SIGTERM listener must still be armed after a SECOND signal too"
);
drop(signals); }
#[tokio::test]
async fn sigusr2_reopens_every_sheeps_log_files() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let runner = Arc::new(ScriptedRunner::new(vec![ProcScript::never_exits(); 2]));
let daemon = boot_with_idle_sweep(
SharedRunner(Arc::clone(&runner)),
paths.clone(),
BootOptions::default(),
)
.await
.unwrap();
let ctx = daemon.context();
let run = tokio::spawn(daemon.run());
let mut web = AppConfig::minimal("web", "./srv");
web.instances = 2;
ctx.supervisor
.start(vec![normalize(web).unwrap()])
.await
.unwrap();
for instance in 0..2 {
assert!(
runner.log_ctl_live(instance),
"instance {instance} must have a live log pump before the signal, or this \
case proves nothing"
);
assert_eq!(
runner.reopens(instance),
0,
"instance {instance} must not have been reopened before the signal"
);
}
nix::sys::signal::raise(nix::sys::signal::Signal::SIGUSR2).unwrap();
let both_reopened = async {
while runner.reopens(0) == 0 || runner.reopens(1) == 0 {
tokio::time::sleep(Duration::from_millis(10)).await;
}
};
tokio::time::timeout(Duration::from_secs(10), both_reopened)
.await
.expect("SIGUSR2 must reopen every sheep's log files");
ctx.shutdown();
tokio::time::timeout(Duration::from_secs(10), run)
.await
.unwrap()
.unwrap()
.unwrap();
}
}