fix(engine): read only the host's own cgroup layout for memory and CPU limits (#8803)

This commit is contained in:
Miguel Ángel
2026-11-25 04:25:54 +00:00
committed by GitHub
parent 3339cb8dda
commit 182716eaa6
2 changed files with 91 additions and 19 deletions
@@ -15,11 +15,15 @@ import {
const BYTES_PER_MIB = 1024 * 1024;
const CGROUP_V2_MEMORY_MAX_PATH = "/sys/fs/cgroup/memory.max";
const CGROUP_V1_MEMORY_LIMIT_PATH = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
const CGROUP_V2_CONTROLLERS_PATH = "/sys/fs/cgroup/cgroup.controllers";
const CGROUP_V1_CPU_QUOTA_PATH = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us";
const CGROUP_V1_CPU_PERIOD_PATH = "/sys/fs/cgroup/cpu/cpu.cfs_period_us";
type SystemMemoryModule = typeof import("./systemMemory.js");
type MockSystemMemoryOptions = {
availableParallelism?: number;
cgroupVersion?: 1 | 2;
hostCpus?: number;
files?: Record<string, string>;
hostTotalMb?: number;
@@ -46,14 +50,20 @@ async function withSystemMemoryMocks(
): Promise<void> {
const {
availableParallelism = 32,
cgroupVersion = 2,
hostCpus = 32,
files = {},
files: layoutFiles = {},
hostTotalMb = 32768,
platform = "linux",
readErrors = {},
onRead,
throwOnFileRead = false,
} = options;
// The same object, so a test can change a file between probes.
const files =
cgroupVersion === 2
? Object.assign(layoutFiles, { [CGROUP_V2_CONTROLLERS_PATH]: "cpu memory" })
: layoutFiles;
const restorePlatform = stubPlatform(platform);
vi.resetModules();
@@ -198,6 +208,58 @@ describe("getCgroupCpuLimit", () => {
expect(getCgroupCpuLimit()).toBeNull(),
);
});
it("reads the quota over its period on a cgroup v1 host", async () => {
await withSystemMemoryMocks(
{
cgroupVersion: 1,
files: { [CGROUP_V1_CPU_QUOTA_PATH]: "200000", [CGROUP_V1_CPU_PERIOD_PATH]: "100000" },
},
({ getCgroupCpuLimit }) => expect(getCgroupCpuLimit()).toBe(2),
);
});
});
describe("a cgroup v2 host with no limit at its root", () => {
// On v2, /sys/fs/cgroup/cpu and /sys/fs/cgroup/memory are child cgroups, not v1 controllers.
const unreadable = Object.assign(new Error("permission denied"), { code: "EACCES" });
it("never reads v1 paths, so finds no limit and warns about nothing", async () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const readCalls: string[] = [];
await withSystemMemoryMocks(
{
readErrors: {
[CGROUP_V1_CPU_QUOTA_PATH]: unreadable,
[CGROUP_V1_CPU_PERIOD_PATH]: unreadable,
[CGROUP_V1_MEMORY_LIMIT_PATH]: unreadable,
},
onRead: (path) => readCalls.push(path),
},
({ getCgroupCpuLimit, getCgroupMemoryLimitMb }) => {
expect(getCgroupCpuLimit()).toBeNull();
expect(getCgroupMemoryLimitMb()).toBeNull();
expect(readCalls).not.toContain(CGROUP_V1_CPU_QUOTA_PATH);
expect(readCalls).not.toContain(CGROUP_V1_MEMORY_LIMIT_PATH);
expect(warn).not.toHaveBeenCalled();
},
);
});
it("names the CPU file it could not read, not a memory limit", async () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
await withSystemMemoryMocks(
{ readErrors: { "/sys/fs/cgroup/cpu.max": unreadable } },
({ getCgroupCpuLimit }) => {
expect(getCgroupCpuLimit()).toBeNull();
expect(warn).toHaveBeenCalledTimes(1);
expect(warn.mock.calls[0]?.[0]).toContain(
"[SystemMemory] Unable to read /sys/fs/cgroup/cpu.max (EACCES",
);
expect(warn.mock.calls[0]?.[0]).not.toContain("memory");
},
);
});
});
describe("usableCpuCount under a CPU quota", () => {
@@ -305,13 +367,18 @@ describe("getSystemTotalMb", () => {
({ _resetCgroupLimitCacheForTests, getSystemTotalMb }) => {
expect(getSystemTotalMb()).toBe(4096);
expect(getSystemTotalMb()).toBe(4096);
expect(readCalls).toEqual([CGROUP_V2_MEMORY_MAX_PATH]);
expect(readCalls).toEqual([CGROUP_V2_CONTROLLERS_PATH, CGROUP_V2_MEMORY_MAX_PATH]);
files[CGROUP_V2_MEMORY_MAX_PATH] = `${2048 * BYTES_PER_MIB}`;
_resetCgroupLimitCacheForTests();
expect(getSystemTotalMb()).toBe(2048);
expect(readCalls).toEqual([CGROUP_V2_MEMORY_MAX_PATH, CGROUP_V2_MEMORY_MAX_PATH]);
expect(readCalls).toEqual([
CGROUP_V2_CONTROLLERS_PATH,
CGROUP_V2_MEMORY_MAX_PATH,
CGROUP_V2_CONTROLLERS_PATH,
CGROUP_V2_MEMORY_MAX_PATH,
]);
},
);
});
@@ -333,6 +400,7 @@ describe("getSystemTotalMb", () => {
it("honors cgroup v1 numeric limits when cgroup v2 is absent", async () => {
await withSystemMemoryMocks(
{
cgroupVersion: 1,
files: {
[CGROUP_V1_MEMORY_LIMIT_PATH]: `${6144 * BYTES_PER_MIB}`,
},
@@ -347,6 +415,7 @@ describe("getSystemTotalMb", () => {
it("uses the host total when cgroup v1 reports a no-limit sentinel", async () => {
await withSystemMemoryMocks(
{
cgroupVersion: 1,
files: {
[CGROUP_V1_MEMORY_LIMIT_PATH]: "9223372036854771712",
},
@@ -381,7 +450,7 @@ describe("getSystemTotalMb", () => {
expect(isLowMemorySystem()).toBe(false);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn.mock.calls[0]?.[0]).toContain(
"[SystemMemory] Unable to read cgroup memory limit",
`[SystemMemory] Unable to read ${CGROUP_V2_MEMORY_MAX_PATH} (`,
);
expect(warn.mock.calls[0]?.[0]).toContain("EACCES");
},
@@ -440,6 +509,7 @@ describe("getSystemTotalMb", () => {
async (content) => {
await withSystemMemoryMocks(
{
cgroupVersion: 1,
files: {
[CGROUP_V1_MEMORY_LIMIT_PATH]: content,
},
+17 -15
View File
@@ -22,6 +22,7 @@ const BYTES_PER_MIB_BIGINT = BigInt(BYTES_PER_MIB);
// slice (e.g. /sys/fs/cgroup/user.slice/.../memory.max) that these root paths
// don't see, and that's acceptable — bare hosts are covered by total-RAM
// detection, and chasing nested slices adds fragility for no container gain.
const CGROUP_V2_CONTROLLERS_PATH = "/sys/fs/cgroup/cgroup.controllers";
const CGROUP_V2_MEMORY_MAX_PATH = "/sys/fs/cgroup/memory.max";
const CGROUP_V1_MEMORY_LIMIT_PATH = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
// Kernel no-limit sentinel is page-rounded 2^63-1 (~9223372036854771712); >= 2^60 is implausible as a real limit.
@@ -111,15 +112,13 @@ export function _resetCgroupLimitCacheForTests(): void {
export function getCgroupCpuLimit(): number | null {
if (_cachedCgroupCpus !== undefined) return _cachedCgroupCpus;
if (process.platform !== "linux") return (_cachedCgroupCpus = null);
const v2 = readCgroupFile(CGROUP_V2_CPU_MAX_PATH);
_cachedCgroupCpus =
v2 !== null
? parseCgroupCpus(v2, null, null)
: parseCgroupCpus(
null,
readCgroupFile(CGROUP_V1_CPU_QUOTA_PATH),
readCgroupFile(CGROUP_V1_CPU_PERIOD_PATH),
);
_cachedCgroupCpus = isCgroupV2()
? parseCgroupCpus(readCgroupFile(CGROUP_V2_CPU_MAX_PATH), null, null)
: parseCgroupCpus(
null,
readCgroupFile(CGROUP_V1_CPU_QUOTA_PATH),
readCgroupFile(CGROUP_V1_CPU_PERIOD_PATH),
);
return _cachedCgroupCpus;
}
@@ -135,10 +134,9 @@ export function getCgroupMemoryLimitMb(): number | null {
return null;
}
const v2Content = readCgroupFile(CGROUP_V2_MEMORY_MAX_PATH);
const v1Content = v2Content === null ? readCgroupFile(CGROUP_V1_MEMORY_LIMIT_PATH) : null;
_cachedCgroupLimitMb = parseCgroupLimitMb(v2Content, v1Content);
_cachedCgroupLimitMb = isCgroupV2()
? parseCgroupLimitMb(readCgroupFile(CGROUP_V2_MEMORY_MAX_PATH), null)
: parseCgroupLimitMb(null, readCgroupFile(CGROUP_V1_MEMORY_LIMIT_PATH));
if (_cachedCgroupLimitMb !== null) {
// stderr, not stdout: this is a diagnostic notice, and commands like `check --json`
// write their machine-readable payload to stdout. A banner on stdout corrupts that
@@ -151,6 +149,10 @@ export function getCgroupMemoryLimitMb(): number | null {
return _cachedCgroupLimitMb;
}
function isCgroupV2(): boolean {
return readCgroupFile(CGROUP_V2_CONTROLLERS_PATH) !== null;
}
function readCgroupFile(path: string): string | null {
try {
return readFileSync(path, "utf8");
@@ -181,8 +183,8 @@ function warnCgroupReadFailure(path: string, error: unknown): void {
if (_warnedCgroupReadFailure) return;
_warnedCgroupReadFailure = true;
console.warn(
`[SystemMemory] Unable to read cgroup memory limit at ${path} ` +
`(${formatCgroupReadError(error)}); falling back to host RAM.`,
`[SystemMemory] Unable to read ${path} ` +
`(${formatCgroupReadError(error)}); using the host's values instead.`,
);
}