From 6c10d3a22022a63487589eaa56d7d26440eca4bd Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 13 Nov 2026 12:40:54 +0100 Subject: [PATCH] fix(cc): raise oom_score_adj to 500 inside the session (BLK-044) sshd's systemd override (OOMScoreAdjust=-1000) is inherited by tmux, dtach and every claude started via cc, so the OOM killer never picks a runaway claude; systemd-run --scope rejects the property. The inner bash raises /proc/self/oom_score_adj to 500 (raise-only) before exec claude. T7-oom asserts it through the stubbed chain. --- lib/cc.sh | 4 ++++ lib/tests/cc.test.sh | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/cc.sh b/lib/cc.sh index 3ce9770..a82a94c 100644 --- a/lib/cc.sh +++ b/lib/cc.sh @@ -16,6 +16,8 @@ # flag overrides; to change packs, start another `cc `. # Without a profile: manifest defaults + local settings, as before. # TMPDIR goes to ~/.cache/claude-tmp either way (BLK-021). +# oom_score_adj is raised to 500 inside the session (BLK-044: sshd's -1000 +# is inherited), raise-only. # ============================================================ _cc_name_ok() { [[ "$1" =~ ^[a-z0-9][a-z0-9-]*$ ]]; } @@ -45,6 +47,8 @@ cc() { dtach -c "$HOME/.dtach/${name}" -e '^\' \ bash -c 'set -a; [ -f "$HOME/.claude/.env" ] && . "$HOME/.claude/.env" set +a + [ "$(cat /proc/self/oom_score_adj 2>/dev/null)" -lt 500 ] \ + 2>/dev/null && echo 500 2>/dev/null > /proc/self/oom_score_adj if [ -n "$CLAUDE_PROFILE_SETTINGS" ]; then exec claude --settings "$CLAUDE_PROFILE_SETTINGS" fi diff --git a/lib/tests/cc.test.sh b/lib/tests/cc.test.sh index d19b47d..057cee9 100644 --- a/lib/tests/cc.test.sh +++ b/lib/tests/cc.test.sh @@ -3,7 +3,8 @@ # list + rc 2, no session; a profile → claude --settings with # CLAUDE_PROFILE / CLAUDE_PROFILE_SETTINGS / TMPDIR exported through # systemd-run + dtach; no profile → plain claude, TMPDIR still set; .env -# sourced. Hermetic: PATH shims, HOME in a fixture holding .claude/lib. +# sourced; oom_score_adj raised to 500 (raise-only). +# Hermetic: PATH shims, HOME in a fixture holding .claude/lib. set -u unset CLAUDE_PROFILE CLAUDE_PROFILE_SETTINGS ROOT="$(cd "$(dirname "$0")/../.." && pwd)" @@ -31,7 +32,8 @@ cat > "$FX/bin/claude" <<'EOT' #!/usr/bin/env bash { echo "claude argv: $*"; echo "CLAUDE_PROFILE=${CLAUDE_PROFILE-unset}" echo "CLAUDE_PROFILE_SETTINGS=${CLAUDE_PROFILE_SETTINGS-unset}" - echo "TMPDIR=${TMPDIR-unset}"; echo "canary=${CC_ENV_CANARY-unset}"; } >> "$CC_LOG" + echo "TMPDIR=${TMPDIR-unset}"; echo "canary=${CC_ENV_CANARY-unset}" + echo "oom=$(cat /proc/self/oom_score_adj 2>/dev/null)"; } >> "$CC_LOG" EOT chmod +x "$FX/bin/"* # shellcheck source=lib/tests/claude-stub.sh @@ -72,5 +74,7 @@ done printf 'CC_ENV_CANARY=1\n' > "$FX/home/.claude/.env" : > "$CC_LOG"; cc s4 webx; check_has T4-env "$(cat "$CC_LOG")" "canary=1" +want=$(cat /proc/self/oom_score_adj); [ "$want" -lt 500 ] && want=500 +check_has T7-oom "$(cat "$CC_LOG")" "oom=$want" check T5-shellcheck "$(shellcheck "$ROOT/lib/cc.sh" >/dev/null 2>&1 && echo ok)" ok printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"; [ "$fail" -eq 0 ]