Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/daemon-supervisor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: std::future::Future>(&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()
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions crates/daemon-transport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
19 changes: 14 additions & 5 deletions crates/daemon-transport/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions crates/serverosd/src/daemon/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,17 @@ async fn updates(app: Arc<App>) {
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 {
Expand Down
Loading