Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: a stop signal ends the startup secret-store reads

The queue client secret and the webhook secret are read at start through
a retry schedule of about a minute. Neither heard SIGTERM, because the
shutdown signal was only installed once the controller began serving, so
a stop during a store outage waited out the schedule or the unit's stop
timeout.

main installs the signal first and passes it to queue_identity::init and
webhook::load. A stop that arrives first ends the read and exits 0.
This commit is contained in:
maia 2026-11-19 09:06:48 +01:00 • committed by mara
commit 96cb25a040

View file

@ -11,6 +11,7 @@
//!
//! [`init`] runs once in `main`, before anything calls [`get`].
use std::future::Future;
use std::path::PathBuf;
use std::sync::OnceLock;
@ -48,28 +49,44 @@ impl Coordinates {
}
}
/// Resolve the identity and hold it for [`get`].
/// Resolve the identity and hold it for [`get`], giving up at once when `stop`
/// resolves while the store is still being waited for.
///
/// `Some(())` once the identity is held; `None` when `stop` came first and the
/// daemon should exit.
///
/// # Errors
/// A half-set environment, which is a deployment bug and not an absent queue;
/// a secret the store still would not give up after the startup retries; or a
/// second call.
pub async fn init() -> Result<()> {
pub async fn init(stop: impl Future<Output = ()>) -> Result<Option<()>> {
let identity = match coordinates_from(|name| std::env::var(name).ok())? {
None => None,
Some(coordinates) => {
let secret = swarm_secret_client::read_with_startup_retry(
"fetching the queue client secret from the swarm secret store",
fetch_secret,
)
.await
.context("fetching the queue client secret from the swarm secret store")?;
Some(coordinates.with_secret(secret))
}
Some(coordinates) => match startup_secret(stop, fetch_secret).await? {
Some(secret) => Some(coordinates.with_secret(secret)),
None => return Ok(None),
},
};
IDENTITY
.set(identity)
.map_err(|_| anyhow::anyhow!("the queue identity was initialised twice"))
.map_err(|_| anyhow::anyhow!("the queue identity was initialised twice"))?;
Ok(Some(()))
}
/// The client secret, retried through a store that is still coming up, or
/// `None` when `stop` resolves first.
async fn startup_secret<F, Fut>(stop: impl Future<Output = ()>, fetch: F) -> Result<Option<String>>
where
F: FnMut() -> Fut,
Fut: Future<Output = Result<String>>,
{
swarm_secret_client::read_with_startup_retry_or_stop(
"fetching the queue client secret from the swarm secret store",
stop,
fetch,
)
.await
.context("fetching the queue client secret from the swarm secret store")
}
/// The identity, or `None` when this deployment wired no queue up.
@ -126,6 +143,29 @@ mod tests {
use super::*;
/// A secret read that always fails, as a store that is down does.
async fn store_down() -> Result<String> {
bail!("store is down")
}
#[tokio::test(start_paused = true)]
async fn a_stop_while_the_store_is_down_ends_the_wait() {
let stop = tokio::time::sleep(std::time::Duration::from_secs(2));
let started = tokio::time::Instant::now();
let got = startup_secret(stop, store_down).await.unwrap();
assert_eq!(got, None);
assert_eq!(started.elapsed(), std::time::Duration::from_secs(2));
}
#[tokio::test(start_paused = true)]
async fn a_store_that_answers_gives_the_secret_and_ignores_a_later_stop() {
let stop = tokio::time::sleep(std::time::Duration::from_mins(1));
let got = startup_secret(stop, || async { Ok("s3cret".to_owned()) })
.await
.unwrap();
assert_eq!(got.as_deref(), Some("s3cret"));
}
fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
let map: HashMap<String, String> = pairs
.iter()