From 86471990bb00959f737d550ab87b69476a9ea493 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 5 Sep 2022 21:56:15 -0400 Subject: [PATCH] Clamp JSON CPU count to available CPU count --- backend/Cargo.lock | 2 +- backend/src/settings/general.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 3c530c9..2f30d12 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -567,7 +567,7 @@ dependencies = [ [[package]] name = "powertools-rs" -version = "1.0.0-rc2" +version = "1.0.0" dependencies = [ "log", "serde", diff --git a/backend/src/settings/general.rs b/backend/src/settings/general.rs index 1ea88bf..8fe0975 100644 --- a/backend/src/settings/general.rs +++ b/backend/src/settings/general.rs @@ -101,9 +101,24 @@ impl Settings { fn convert_cpus(mut cpus: Vec, version: u64) -> Vec { let mut result = Vec::with_capacity(cpus.len()); + let max_cpus = Cpu::cpu_count(); for (i, cpu) in cpus.drain(..).enumerate() { + // prevent having more CPUs than available + if let Some(max_cpus) = max_cpus { + if i == max_cpus { + break; + } + } result.push(Cpu::from_json(cpu, version, i)); } + if let Some(max_cpus) = max_cpus { + if result.len() != max_cpus { + let mut sys_cpus = Cpu::system_default(); + for i in result.len()..sys_cpus.len() { + result.push(sys_cpus.remove(i)); + } + } + } result }