Skip to content

Commit 48545da

Browse files
Fix cgroup v2 memory stat related panics
When containers are terminated, cgroup v2 memory metrics under /sys/fs/cgroup may disappear. Previously, kata-agent assumed these metrics always exist, leading to panics as reported in #138. This commit returns default value (0) when memory metric files are missing. This behaviour aligns with cgroup v1, which also defaults to 0 memory metric files are missing: - Memory.limit_in_bytes which maps to m.max https://github.com/kata-containers/cgroups-rs/blob/main/src/memory.rs#L635 - Memory.soft_limit_in_bytes which maps to m.low https://github.com/kata-containers/cgroups-rs/blob/main/src/memory.rs#L661 - MemSwap.fail_cnt: https://github.com/kata-containers/cgroups-rs/blob/main/src/memory.rs#L631 Submitting a new PR because #116 does not handle MemSwap.fail_cnt, which will also cause panic. Signed-off-by: Alex Man <[email protected]>
1 parent eb3e37a commit 48545da

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Diff for: src/memory.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,18 @@ impl MemController {
574574
// for v2
575575
pub fn get_mem(&self) -> Result<SetMemory> {
576576
let mut m: SetMemory = Default::default();
577-
self.get_max_value("memory.high")
578-
.map(|x| m.high = Some(x))?;
579-
self.get_max_value("memory.low").map(|x| m.low = Some(x))?;
580-
self.get_max_value("memory.max").map(|x| m.max = Some(x))?;
581-
self.get_max_value("memory.min").map(|x| m.min = Some(x))?;
582-
577+
m.high = self
578+
.get_max_value("memory.high")
579+
.map_or(Some(MaxValue::Value(0)), |x| Some(x));
580+
m.low = self
581+
.get_max_value("memory.low")
582+
.map_or(Some(MaxValue::Value(0)), |x| Some(x));
583+
m.max = self
584+
.get_max_value("memory.max")
585+
.map_or(Some(MaxValue::Value(0)), |x| Some(x));
586+
m.min = self
587+
.get_max_value("memory.min")
588+
.map_or(Some(MaxValue::Value(0)), |x| Some(x));
583589
Ok(m)
584590
}
585591

@@ -730,7 +736,7 @@ impl MemController {
730736
.open_path("memory.swap.events", false)
731737
.and_then(flat_keyed_to_hashmap)
732738
.map(|x| *x.get("fail").unwrap_or(&0) as u64)
733-
.unwrap(),
739+
.unwrap_or(0),
734740
limit_in_bytes: self
735741
.open_path("memory.swap.max", false)
736742
.and_then(read_i64_from)

0 commit comments

Comments
 (0)