From 87c6275d0d833009b7c585d382311e465e27979c Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 13 Nov 2026 16:04:15 +0100 Subject: [PATCH 1/2] Keep a service's own lower OOM score Debian's PostgreSQL unit sets OOMScoreAdjust=-900; genco's drop-in replaced that with -600 at its next start (seen on gs3 after 0.13.0), less protection than before. A unit whose own file sets -600 or lower gets no drop-in now, an old one of genco's is removed, and its running processes keep (or get back) its own score. Co-Authored-By: Claude Opus 5.5 --- server/src/apply/memory.rs | 64 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/server/src/apply/memory.rs b/server/src/apply/memory.rs index a51d55b..b11284a 100644 --- a/server/src/apply/memory.rs +++ b/server/src/apply/memory.rs @@ -34,7 +34,11 @@ pub(super) fn memory_files(cap_mb: u32) -> Vec { content: crate::limits::groups_slice_unit(cap_mb), private_to: None, }]; - for (unit, _) in protected_units() { + // a unit that sets a lower score itself (PostgreSQL's -900) keeps it + for (unit, _) in protected_units() + .into_iter() + .filter(|(u, _)| own_score(u).is_none_or(|s| s > OOM_SCORE)) + { files.push(File { path: Path::new("/etc/systemd/system") .join(format!("{unit}.d")) @@ -105,6 +109,13 @@ pub(super) async fn memory( } } for (unit, cgroup) in protected_units() { + let score = match own_score(&unit) { + Some(own) if own <= OOM_SCORE => { + drop_old_dropin(r, &unit).await; + own + } + _ => OOM_SCORE, + }; let procs = cgroup.join("cgroup.procs"); let pids = std::fs::read_to_string(procs).unwrap_or_default(); for pid in pids.lines() { @@ -112,8 +123,8 @@ pub(super) async fn memory( let now: Option = std::fs::read_to_string(&path) .ok() .and_then(|s| s.trim().parse().ok()); - if now.is_some_and(|n| n > OOM_SCORE) - && let Err(e) = std::fs::write(&path, OOM_SCORE.to_string()) + if now.is_some_and(|n| n > score) + && let Err(e) = std::fs::write(&path, score.to_string()) { r.info(format!("warning: OOM score of {unit} ({pid}): {e}")); } @@ -121,6 +132,45 @@ pub(super) async fn memory( } } +/// The OOM score a unit's own file sets (its template's for an +/// instance like `postgresql@17-main.service`), not genco's drop-in. +fn own_score(unit: &str) -> Option { + let name = match unit.split_once('@') { + Some((prefix, _)) => format!("{prefix}@.service"), + None => unit.to_string(), + }; + let file = [ + "/etc/systemd/system", + "/usr/lib/systemd/system", + "/lib/systemd/system", + ] + .iter() + .find_map(|dir| std::fs::read_to_string(Path::new(dir).join(&name)).ok())?; + score_in(&file) +} + +/// The last `OOMScoreAdjust=` of a unit file. +fn score_in(file: &str) -> Option { + file.lines() + .rev() + .find_map(|l| l.trim().strip_prefix("OOMScoreAdjust="))? + .trim() + .parse() + .ok() +} + +/// genco's drop-in of a unit that protects itself better, from before +/// genco looked: it would raise the unit's own score at its next start. +async fn drop_old_dropin(r: &mut Report, unit: &str) { + let path = Path::new("/etc/systemd/system") + .join(format!("{unit}.d")) + .join("genco-memory.conf"); + if path.exists() && std::fs::remove_file(&path).is_ok() { + r.run(&["systemctl", "daemon-reload"]).await; + r.info(format!("{unit} keeps its own OOM score")); + } +} + #[cfg(test)] mod tests { use super::*; @@ -136,4 +186,12 @@ mod tests { assert!(!protected("postgresql.service.d")); assert!(!protected("cron.service")); } + + #[test] + fn a_unit_s_own_score_is_read() { + let unit = "[Service]\nOOMScoreAdjust=-100\nType=forking\n\ + OOMScoreAdjust=-900\n"; + assert_eq!(score_in(unit), Some(-900)); + assert_eq!(score_in("[Service]\nType=simple\n"), None); + } } From 78f04e568b1d234392ed0b5829aef1ad5addc2b0 Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 13 Nov 2026 16:11:40 +0100 Subject: [PATCH 2/2] Leave raised OOM scores of self-protected units alone A unit that sets its own lower score (Debian's PostgreSQL) only gets it back on processes still at genco's old -600: its backends reset themselves to 0 so a runaway query is killed first, and pushing them to -900 would make the kernel pick nginx or mail instead. Unit files with spaces around the = are read too, and a drop-in that can't be removed is reported. Co-Authored-By: Claude Opus 5.5 --- server/src/apply/memory.rs | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/server/src/apply/memory.rs b/server/src/apply/memory.rs index b11284a..c65c153 100644 --- a/server/src/apply/memory.rs +++ b/server/src/apply/memory.rs @@ -109,13 +109,11 @@ pub(super) async fn memory( } } for (unit, cgroup) in protected_units() { - let score = match own_score(&unit) { - Some(own) if own <= OOM_SCORE => { - drop_old_dropin(r, &unit).await; - own - } - _ => OOM_SCORE, - }; + let own = own_score(&unit).filter(|s| *s <= OOM_SCORE); + if own.is_some() { + drop_old_dropin(r, &unit).await; + } + let procs = cgroup.join("cgroup.procs"); let pids = std::fs::read_to_string(procs).unwrap_or_default(); for pid in pids.lines() { @@ -123,9 +121,15 @@ pub(super) async fn memory( let now: Option = std::fs::read_to_string(&path) .ok() .and_then(|s| s.trim().parse().ok()); - if now.is_some_and(|n| n > score) - && let Err(e) = std::fs::write(&path, score.to_string()) - { + // a unit with its own score gets it back only where genco's + // old drop-in set -600: processes that raise theirs + // (PostgreSQL's backends to 0) keep it + let score = match (now, own) { + (Some(n), Some(own)) if n == OOM_SCORE => own, + (Some(n), None) if n > OOM_SCORE => OOM_SCORE, + _ => continue, + }; + if let Err(e) = std::fs::write(&path, score.to_string()) { r.info(format!("warning: OOM score of {unit} ({pid}): {e}")); } } @@ -153,7 +157,9 @@ fn own_score(unit: &str) -> Option { fn score_in(file: &str) -> Option { file.lines() .rev() - .find_map(|l| l.trim().strip_prefix("OOMScoreAdjust="))? + .filter_map(|l| l.split_once('=')) + .find(|(key, _)| key.trim() == "OOMScoreAdjust")? + .1 .trim() .parse() .ok() @@ -165,9 +171,13 @@ async fn drop_old_dropin(r: &mut Report, unit: &str) { let path = Path::new("/etc/systemd/system") .join(format!("{unit}.d")) .join("genco-memory.conf"); - if path.exists() && std::fs::remove_file(&path).is_ok() { - r.run(&["systemctl", "daemon-reload"]).await; - r.info(format!("{unit} keeps its own OOM score")); + match std::fs::remove_file(&path) { + Ok(()) => { + r.run(&["systemctl", "daemon-reload"]).await; + r.info(format!("{unit} keeps its own OOM score")); + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => r.error(format!("removing {}: {e}", path.display())), } } @@ -192,6 +202,7 @@ mod tests { let unit = "[Service]\nOOMScoreAdjust=-100\nType=forking\n\ OOMScoreAdjust=-900\n"; assert_eq!(score_in(unit), Some(-900)); + assert_eq!(score_in("OOMScoreAdjust = -900\n"), Some(-900)); assert_eq!(score_in("[Service]\nType=simple\n"), None); } }