diff --git a/crates/daemon-supervisor/src/lib.rs b/crates/daemon-supervisor/src/lib.rs index b43c82c..893b8d0 100644 --- a/crates/daemon-supervisor/src/lib.rs +++ b/crates/daemon-supervisor/src/lib.rs @@ -52,6 +52,22 @@ impl Supervisor { } } + /// Runs one long step of a worker (a download, say) and keeps the + /// worker's heartbeat going while it does, so a slow step that is still + /// making progress isn't taken for a stall. The step needs a time limit + /// of its own: this never ends one. + pub async fn during(&self, worker: &'static str, step: F) -> F::Output { + tokio::pin!(step); + let mut heartbeat = tokio::time::interval(WORKER_STALL / 4); + + loop { + tokio::select! { + done = &mut step => return done, + _ = heartbeat.tick() => self.tick(worker), + } + } + } + pub fn worker_health(&self) -> BTreeMap<&'static str, WorkerHealth> { self.ticks .lock() @@ -195,6 +211,28 @@ mod tests { assert_eq!(supervisor.assess(0), Verdict::Fine); } + #[tokio::test] + async fn a_long_step_keeps_its_worker_ticking_and_gives_its_answer() { + let supervisor = Supervisor::new(); + supervisor.ticks.lock().unwrap().insert( + "updates", + Instant::now() - WORKER_STALL - Duration::from_secs(1), + ); + + let answer = supervisor + .during("updates", async { + tokio::time::sleep(Duration::from_millis(20)).await; + 7 + }) + .await; + + assert_eq!(answer, 7); + assert_eq!( + supervisor.worker_health().get("updates"), + Some(&WorkerHealth::Ok) + ); + } + #[test] fn memory_over_the_ceiling_asks_for_a_process_restart() { let supervisor = Supervisor::new(); diff --git a/crates/daemon-transport/src/lib.rs b/crates/daemon-transport/src/lib.rs index d7b297c..62907c2 100644 --- a/crates/daemon-transport/src/lib.rs +++ b/crates/daemon-transport/src/lib.rs @@ -10,6 +10,10 @@ use std::time::Duration; pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(10); pub const LIVENESS_TIMEOUT: Duration = Duration::from_secs(45); +/// How long one message may take to write. A connection that has gone dead +/// without closing accepts nothing: past this the link is dropped and +/// dialled again, rather than everything that sends waiting behind it. +pub const WRITE_TIMEOUT: Duration = Duration::from_secs(30); pub const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(15); diff --git a/crates/daemon-transport/src/link.rs b/crates/daemon-transport/src/link.rs index fc560c1..ba2bedc 100644 --- a/crates/daemon-transport/src/link.rs +++ b/crates/daemon-transport/src/link.rs @@ -11,7 +11,9 @@ use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::Connector; use tracing::{debug, info, warn}; -use crate::{tls, CONNECT_TIMEOUT, HANDSHAKE_TIMEOUT, HEARTBEAT_INTERVAL, LIVENESS_TIMEOUT}; +use crate::{ + tls, CONNECT_TIMEOUT, HANDSHAKE_TIMEOUT, HEARTBEAT_INTERVAL, LIVENESS_TIMEOUT, WRITE_TIMEOUT, +}; #[derive(Debug, Error)] pub enum LinkError { @@ -167,13 +169,20 @@ impl Link { } }; - if let Err(e) = sink.send(Message::Text(text)).await { - debug!(error = %e, "outbound pump ending"); - break; + match tokio::time::timeout(WRITE_TIMEOUT, sink.send(Message::Text(text))).await { + Ok(Ok(())) => {} + Ok(Err(e)) => { + debug!(error = %e, "outbound pump ending"); + break; + } + Err(_) => { + warn!(after = ?WRITE_TIMEOUT, "the panel stopped taking messages; dropping the connection"); + break; + } } } - let _ = sink.close().await; + let _ = tokio::time::timeout(Duration::from_secs(5), sink.close()).await; }); let read_driver = Arc::clone(&driver); diff --git a/crates/serverosd/src/daemon/workers.rs b/crates/serverosd/src/daemon/workers.rs index 55ceb02..d4deddb 100644 --- a/crates/serverosd/src/daemon/workers.rs +++ b/crates/serverosd/src/daemon/workers.rs @@ -304,15 +304,17 @@ async fn updates(app: Arc) { note: None, }); - match install( + // A download on a slow line takes minutes (it has ten of its own): + // the heartbeat goes on, or the daemon would restart mid-download + // and never finish updating. + let installing = install( &app.paths.binary, &app.paths.state_dir, app.build.version, &candidate, |host| updates.permits_host(&panel_host, host), - ) - .await - { + ); + match app.supervisor.during("updates", installing).await { Ok(installed) => { *app.pending_update.lock().unwrap() = None; let _ = app.audit.record(daemon_audit::Entry {