Repository navigation
cmd/codeaf: the soft memory limit is the smallest real bound, not the host s memory - #1162
Merged
Merged
Conversation
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]>
#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
GOMEMLIMITexists to avoid.This takes the smallest real bound instead.
What changed
surfaceMemoryLimitnow takes the smallest finite bound it can read of: physical memory, thecgroup v2
memory.maxfor this process, and the cgroup v1memory.limit_in_bytes. A bound that isabsent, unreadable, the literal
max, non-numeric, or the v1 sentinel is not a bound — it does notparticipate, 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/cgroupline; the reader walks from there up to the hierarchy root and takes theminimum, because a container often puts the real bound on a slice above the one the process sits in, or
leaves the leaf at
maxwhile a parent is bounded. Walking can only tighten the answer, never loosenit, 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 namesmemoryamong its controllers with thebound in
memory/<path>/memory.limit_in_bytes.hostCgroupMemoryLimitis the reader seam, the same shape ashostTotalMemory, so the tests stand ahierarchy 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:
stat -fc %T /sys/fs/cgroup→cgroup2fs);/sys/fs/cgroup/memory.maxdoes not exist — the root cgroup has nomemory.max, so the obviouspath 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;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;
9223372036854771712), not a word.Tests
Against fake files only — a
t.TempDir()standing in for/sys/fs/cgroupand a stand-in for/proc/self/cgroup. No test reads the real hierarchy. Covered: no cgroup files at all;max; the v1sentinel; 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
go test ./cmd/codeaf ./internal/guard ./internal/namelaw -count=1on the Spark cluster over sshfrom a fresh clone of this head; log path in a comment below, HEAD on its first line.
its three.
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
GOMEMLIMITstill decides.🤖 Generated with Claude Code