From 439ca0beeb29269c51525971e11c350f271e0918 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Tue, 24 Nov 2026 11:18:01 +0100 Subject: [PATCH] fix(daemon): the last container that stops takes its load along When no container ran any more, the container sampler returned before it marked the stopped ones idle, so their last load stayed: a group and the lane card showed gigabytes after `down`, and the lane's estimate for the pool counted them. `docker stats` with no names runs nothing, so the round now always ends with the stopped containers idle. Bug-Class: early-return-skips-reset Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 ++++ crates/devstack-daemon/src/usage.rs | 68 +++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c22d31f..da8e9acb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,14 @@ and listed with their assets on the ## [Unreleased] +### Fixed + +- **A stopped stack no longer shows the memory of its containers.** When + the last container stopped, the daemon kept its last load, so a group and + the lane card showed gigabytes after `down` (and the lane's estimate for + the pool counted them) until the next container started. Now every + stopped container is idle at the next round, the last one too. + ## [0.53.0] - 2026-11-23 The dashboard pushes a branch that is ahead of its upstream, and the start diff --git a/crates/devstack-daemon/src/usage.rs b/crates/devstack-daemon/src/usage.rs index a126c217..63892c31 100644 --- a/crates/devstack-daemon/src/usage.rs +++ b/crates/devstack-daemon/src/usage.rs @@ -352,18 +352,30 @@ impl Sampler { } } } - let running_keys: Vec = running - .values() - .map(|(group, service)| container_key(group, service)) - .collect(); if let Some(file) = sup.external_infra() { + let running_keys: Vec = running + .values() + .map(|(group, service)| container_key(group, service)) + .collect(); return self.sample_external(&file, &running_keys); } let names: Vec = running.keys().cloned().collect(); - if names.is_empty() { - return; - } - let stats = sup.docker().stats(&names); + self.record_docker(&running, || sup.docker().stats(&names)); + } + + /// What `docker stats` measured for the running containers (`name` -> + /// group and service). Also with none running: the ones that stopped + /// lose their load here. + fn record_docker( + &self, + running: &BTreeMap, + stats: impl FnOnce() -> devstack_compose::Result>, + ) { + let running_keys: Vec = running + .values() + .map(|(group, service)| container_key(group, service)) + .collect(); + let stats = stats(); let mut inner = self.inner.lock().unwrap(); let mut seen = Vec::new(); match stats { @@ -774,6 +786,46 @@ mod tests { assert_eq!(inner.series[&key].load(), Load::default()); } + #[test] + fn the_last_container_that_stops_takes_its_load_along() { + let sampler = Sampler { + inner: Mutex::new(Inner::default()), + tree: Mutex::new(None), + }; + let one = |name: &str| (name.to_string(), ("app".to_string(), name.to_string())); + let measured = |name: &str, memory| devstack_compose::ContainerStats { + name: name.into(), + cpu: 1.0, + memory, + }; + let load = |s: &Sampler, service: &str| { + s.inner.lock().unwrap().series[&container_key("app", service)].load() + }; + let running = BTreeMap::from([one("db"), one("search")]); + sampler.record_docker(&running, || { + Ok(vec![measured("db", 1 << 30), measured("search", 2 << 30)]) + }); + assert_eq!(load(&sampler, "search").memory, 2 << 30); + // One stops, then the other: none is left with a load. + sampler.record_docker(&BTreeMap::from([one("db")]), || { + Ok(vec![measured("db", 1 << 30)]) + }); + assert_eq!(load(&sampler, "search"), Load::default()); + assert_eq!(load(&sampler, "db").memory, 1 << 30, "still running"); + sampler.record_docker(&BTreeMap::new(), || Ok(Vec::new())); + assert_eq!( + load(&sampler, "db"), + Load::default(), + "the last one stopped" + ); + let inner = sampler.inner.lock().unwrap(); + assert_eq!( + inner.series[&container_key("app", "db")].history.len(), + 2, + "its history stays" + ); + } + #[test] fn disk_size_counts_files_below() { let d = tempfile::tempdir().unwrap();