Skip to content

cmd/codeaf: the soft memory limit is the smallest real bound, not the host s memory - #1162

Merged
santoshkumarradha merged 4 commits into
santos/devfrom
task/c207-cgroup-limit
Sep 18, 2026
Merged

santoshkumarradha merged 4 commits into
santos/devfrom
task/c207-cgroup-limit

Conversation

@santoshkumarradha

Copy link
Copy Markdown
Member

#1158 gave a surface a soft memory limit of half the machine's physical memory. Inside a container
that is the host's memory, so the limit lands far above what the process may use: it never binds, and
the kernel OOM-kills instead of the collector working — the exact failure GOMEMLIMIT exists to avoid.
This takes the smallest real bound instead.

What changed

surfaceMemoryLimit now takes the smallest finite bound it can read of: physical memory, the
cgroup v2 memory.max for this process, and the cgroup v1 memory.limit_in_bytes. A bound that is
absent, unreadable, the literal max, non-numeric, or the v1 sentinel is not a bound — it does not
participate, and it does not become a zero either. If none is readable the surface behaves exactly as
it did before #1158's floor rule, which is unchanged: if the smallest bound halved falls under 512 MiB,
nothing is set at all.

The reader walks the whole path, not just the leaf. The process's own cgroup is the third field of
its /proc/self/cgroup line; the reader walks from there up to the hierarchy root and takes the
minimum, because a container often puts the real bound on a slice above the one the process sits in, or
leaves the leaf at max while a parent is bounded. Walking can only tighten the answer, never loosen
it, and it costs a few stats at startup. Both generations are read: v2 has an empty controller list on
the 0:: line with the bound in <path>/memory.max; v1 names memory among its controllers with the
bound in memory/<path>/memory.limit_in_bytes.

hostCgroupMemoryLimit is the reader seam, the same shape as hostTotalMemory, so the tests stand a
hierarchy in front of the derivation without one existing.

Why the naive version would have been dead code

Measured on the machine this was written on, and these went into the brief for exactly that reason:

  • the box is cgroup v2 (stat -fc %T /sys/fs/cgroup → cgroup2fs);
  • /sys/fs/cgroup/memory.max does not exist — the root cgroup has no memory.max, so the obvious
    path always fails. The file for a process is under its own cgroup path, here
    /sys/fs/cgroup/user.slice/user-1001.slice/session-….scope/memory.max;
  • every level on this box reads the literal max, so this change must not lower the limit here,
    and no test on this box can see a real cgroup bound — which is why the tests read fake files;
  • cgroup v1's "no limit" is a huge sentinel (9223372036854771712), not a word.

Tests

Against fake files only — a t.TempDir() standing in for /sys/fs/cgroup and a stand-in for
/proc/self/cgroup. No test reads the real hierarchy. Covered: no cgroup files at all; max; the v1
sentinel; a real number smaller than physical memory (the cgroup wins); a real number larger (physical
wins); a bound whose half is under the floor (nothing is set); and a parent slice tighter than the leaf.

Evidence

Not claimed

Nothing about whether it helps. No benchmark, no RSS figure. The claim is that the limit is now the
smallest real bound rather than the host's memory, and that an explicit GOMEMLIMIT still decides.

🤖 Generated with Claude Code

santoshkumarradha and others added 4 commits September 18, 2026 01:01
A surface's soft GOMEMLIMIT was half of physical memory. Inside a container
that is the host's memory, so the limit landed far above what the process
could use: it never bound, and the kernel OOM-killed instead of the collector
working — the exact failure GOMEMLIMIT exists to avoid.

surfaceMemoryLimit now takes the smallest finite bound of every one it can
read — physical memory, the cgroup v2 memory.max for the process, and the
cgroup v1 memory.limit_in_bytes. A bound that is absent, unreadable, `max`,
non-numeric, or the v1 sentinel is not a bound and does not participate; if
none is readable the surface behaves exactly as before, floor rule unchanged.

The cgroup reader walks from the process's own level up to the hierarchy root
and takes the minimum, because a parent slice can be tighter than the leaf.
hostCgroupMemoryLimit is the reader seam, the same shape as hostTotalMemory.

Co-Authored-By: codeaf <[email protected]>
The cgroup reader is held down with a t.TempDir() standing in for
/sys/fs/cgroup and a written stand-in for /proc/self/cgroup, never the real
hierarchy — on this machine every level reads `max`, so the real files cannot
show a bound. Coverage: no cgroup files, `max`, the v1 sentinel, an
unparsable value, the process's own v2 bound, walking up to a tighter
ancestor, a v1 bound, and the root level.

The derivation and the tuner are tested through the hostCgroupMemoryLimit
seam: the smallest bound wins, an unread bound does not participate, a
tighter cgroup under the floor still refuses, and the tuner follows the
cgroup when it is the tighter bound. standInForMachine now stands the cgroup
reader in as well, so no test reads the real /sys/fs/cgroup.

Co-Authored-By: codeaf <[email protected]>